Chen Chaojan Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000first, the experimental process
Landing Lab Building Virtual machine http://www.shiyanlou.com/courses/195
Open the shell terminal and execute the following command:
CD Linuxkernel
RM-RF Menu
git clone https://github.com/mengning/menu.git
CD Menu
MV Test_fork.c test.c
Make Rootfs
You can see that the MENUOS has already included the Fork command after the boot.
Debug mode can be opened by adding-s-s startup parameters
Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD Rootfs.img-s-S
Open GDB for remote debugging
Gdb
File Linux-3.18.6/vmlinux
Target remote:1234
Set breakpoints
b sys_clone
b do_fork
b dup_task_struct
b copy_process
b copy_thread
b ret_from_fork
second, the experimental analysis
The experiment shows that the fork () function creates a new process that is implemented by the following series of functions:
Copy_thread (), copy_process (), Dup_task_struct (), Do_fork (), Sys_clone (), fork () Ork ()
Iii. Summary of the experiment
The three main management functions of the operating system include: Process management, memory management, file system.
Fork, Vfork, clone three system calls can create a new process, and all by calling Do_fork to achieve;
Linux creates a new process by replicating the parent process;
From the user-state code, the fork () function returned two times, that is, in the parent, child process each return, where the parent process return code is a child process PID, the child process return code is 0.
A negative value is returned when the fork () function call fails, and there are 2 main reasons for the failure:
1, the current number of processes has reached the system specified upper limit, then the value of errno is set to Eagain (one-Try again);
2, the system memory is insufficient, then the value of errno is set to Enomem (out of memory).
About the init process creation:
Daosh a Start_kernel ... cpu_idle
Life two Kernel_init Kthreadd
Secondary 30 1 2
Sansheng 1th process is the ancestor of all user-state processes the 2nd process is the ancestor of all kernel threads
The sixth week of Linux kernel design learn to summarize and analyze the process of creating a new process for the Linux kernel