Analyzing the process of creating a new process for the Linux kernel
Writer: Yang Guangxu No.: 20135233
(* Original works reproduced please specify the source *)
(Learning course: Linux kernel Analysis MOOC course http://mooc.study.163.com/course/USTC-100)
Knowledge points and Analysis:
Linux creates a new process by copying the parent process
The fork function, the specific process:
- Copy a pcb--task_struct
To assign a new kernel stack to the new process
ti = alloc_thread_info_node(tsk, node); tsk->stack = ti; setup_thread_stack(tsk, orig); //这里只是复制thread_info,而非复制内核堆栈
To modify the copied process data, such as PID, process chain list, see copy_process inside.
//复制内核堆栈childregs->ax = 0; // 子进程的fork返回0p->thread.sp = (unsigned long) childregs; //调度到子进程时的内核栈顶p->thread.ip = (unsigned long) ret_from_fork; //调度到子进程时的第一条指令地址
System calls kernel processing function sys_fork,sys_vfrok,sys_clone, in fact, the final execution is do_fork
There are: Do_fork.
copy_process
// 复制pcb alloc_thread_info_node // 创建了一个页面,其实就是实际分配内核堆栈空间的效果。 setup_thread_stack
// 把thread_info的东西复制过来
The child process is then initialized
where does the new process of creation start--"ret_from_fork
*childregs = *current_pt_regs(); 复制内核堆栈(复制的pt_regs,是SAVE_ALL中系统调用压栈的那一部分。)childregs->ax = 0; 子进程的fork返回0 p->thread.sp = (unsigned long) childregs; 调度到子进程时的内核栈顶p->thread.ip = (unsigned long) ret_from_fork; 调度到子进程时的第一条指令地址
IP is pointing to ret_from_fork, so it starts here.
Experimental requirements:
Reading comprehension task_struct data structure http://codelab.shiyanlou.com/xref/linux-3.18.6/include/linux/sched.h#1235;
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;
Using the GDB trace to analyze a fork system calling the kernel handler Sys_clone, verifying your understanding of creating a new process for the Linux system, it is recommended to complete the experiment in the lab Building Linux virtual Machine environment.
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.
Experimental process:
Delete the original menu, and clone the new menu, with TEST_FORK.C overlay test.c
Make rootfs after new kernel boot, test fork function
Prepare for commissioning using the-s-s frozen core
Gdb
Load symbol table, configure Port
Set breakpoints
Follow the breakpoint to get the result
Analyzing the process of creating a new process for the Linux kernel