Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
Topic self-proposed, the content revolves about how the Linux system creates a new process;
Can combine experiment, draw stack state to execute flowchart, etc.;
The blog content needs to carefully analyze the new process start point and the corresponding stack state.
Summary section need to clarify your understanding of the "Linux system to create a new process"
Lab Report:
Reading comprehension task_struct data structure http://codelab.shiyanlou.com/xref/linux-3.18.6/include/linux/sched.h#1235;
- State: Running Status
- Stack: Kernel stack
- tasks: Process Chain List
- MM: Memory Management
- Task_state: Status of the task
- PID: Process PID
- Real_parent Children: Parent-child relationship of a process
- Files: List of file descriptors
- Signal: Signal Processing related
- Splice_pipe: Pipeline Related
Analyze the kernel processing process of the fork function Sys_clone, understand how to create a new process and how to create and modify task_struct data structure;
- Fork, Vfork, and clone three system calls can create a new process, all by calling Do_fork to implement the process creation
- Creating a new process requires copying a pcb:task_struct first
- Assign a new kernel stack to the new process
TI = Alloc_thread_info_node (tsk, node); tsk->stack = Ti;setup_thread_stack (tsk, orig); This is just a copy of the Thread_info, not the kernel stack.
- And then modify the copied process data, such as PID, process chain list and so on, see copy_process internal
- See Fork () from the code of the user state; the function returned two times, that is, each time it is returned in a parent-child process
1 *childregs = *current_pt_regs (); Copy kernel stack 2 childregs->ax = 0; Why the fork of the subprocess returns 0, here is the reason! 3 4 p->thread.sp = (unsigned long) childregs;//the kernel stack Top 5 P->thread.ip = (unsigned long) ret_from_fork;//dispatch to child process The first instruction address to the child process
Do_fork completes most of the work in the creation, which calls the Copy_process () function and then lets the process begin running. The copy_process () function works as follows:
- 1. Call Dup_task_struct () to create a kernel stack, thread_info structure, and task_struct for the new process with the same values as the current process
- 2. Check
- 3. The child process begins to differentiate itself from the parent process. Many members within the process descriptor are cleared 0 or set to the initial value.
- 4, sub-process status is set to Task_uninterruptible to ensure that it will not be put into operation
- 5, Copy_process () calls Copy_flags () to update the flags member of the TASK_STRUCT. The PF_SUPERPRIV flag that indicates whether the process has superuser privileges is cleared 0. Indicates that the process has not yet called the EXEC () function's pf_forknoexec flag to be set
- 6. Call Alloc_pid () to assign a valid PID to the new process
- 7. Copy_process () copies or shares open files, file system information, signal processing functions, process address space, and namespaces, depending on the parameter flags passed to clone ()
- 8. Finally, Copy_process () does the finishing work and returns a pointer to the child process
Use GDB trace to analyze a fork system call kernel handler function Sys_clone to verify your understanding of creating a new process for Linux systems
- Function under breakpoint: Sys_clone do_fork dup_task_struct copy_struct copy_process copy_thread ret_from_fork
- Dup_task_struct () Create a kernel stack for the new process
copy_process()主要完成进程数据结构,各种资源的初始化
Paying special attention to where the new process starts? Why does it go smoothly? That is, the execution starting point is consistent with how the kernel stack is guaranteed.
ret_from_fork;Determines the first instruction address of the new process
- The statement in the Copy_thread () function
p->thread.ip = (unsigned long) ret_from_fork; determines the address of the first instruction of the new process
- Before Ret_from_fork, that sentence in the Copy_thread () function
*childregs = *current_pt_regs(); assigns the parent process's regs parameter to the child process's kernel stack
- The *childregs type is Pt_regs, which stores the parameters of the save all in the stack, so that it can be executed smoothly after the restore all.
Analyzing the process of creating a new process for the Linux kernel