Analyzing the process of creating a new process for the Linux kernel

Source: Internet
Author: User

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:

  1. Copy a pcb--task_struct
  2. 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,而非复制内核堆栈
  3. 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:

    1. Reading comprehension task_struct data structure http://codelab.shiyanlou.com/xref/linux-3.18.6/include/linux/sched.h#1235;

    2. 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;

    3. 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.

    4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.