Linux Kernel Analysis Sixth Week study summary--description of the process and creation of the process

Source: Internet
Author: User

Linux Kernel Analysis Sixth Week study summary--description of the process and creation of the process

Zhang Yi (Original works reproduced please specify the source)

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

I. SUMMARY OF KNOWLEDGE

Description of the process

    1. Process descriptor TASK_STRUCT Data structure (i)
    2. Process descriptor TASK_STRUCT Data structure (ii)

Creation of processes

    1. Overview of process creation and user-state code for a process fork
    2. Understand how complex code is created by the process
    3. Browse key code related to process creation process
    4. Where does the new process of creation start?
    5. Using GDB to track the process of creating a new process

Second, study notes

Description of the process

1. Process descriptor TASK_STRUCT Data structure (i)

To manage the process, the kernel must have a clear description of each process, and the process descriptor provides the process information that the kernel needs to understand.

    • struct TASK_STRUCT data structure is huge
    • The state of the Linux process seems to be different from the process state described in the operating system principle, such as the readiness state and the running state are task_running, why?
    • Process-coded PID
    • All process linked list struct list_head tasks; The implementation method of the kernel's bidirectional cyclic link list-A more abbreviated two-way circular linked list
    • A process created by a program has a parent-child relationship, and it is often necessary to refer to such a parent-child relationship when programming. There are several fields in the process descriptor that are used to represent such relationships
    • Linux allocates a 8KB-sized memory area for each process to hold two different data structures for the process: thread_info and process kernel stacks

When the process is in the kernel state, it is different from the user-state stack, that is, the core stack is specified in the PCB, why is there no user-state stack in the PCB? How is the user-state stack set?

The kernel controls the path with very few stacks, so 8KB is enough for stacks and thread_info

    • struct THREAD_STRUCT thread; Cpu-specific State of this task
    • File system and file descriptors
    • Memory management-The address space of the process

Linux kernel state transition diagram:

2. Process descriptor TASK_STRUCT Data structure (ii)

The two-way circular chain chart is as follows:

Parent-child relationship visualization for a process:

Creation of processes

1. Overview of the creation of the process and the user-state code of the fork process

(1) Review of the origins of the process

    • Daosh One (Start_kernel...cpu_idle)
    • Life II (Kernel_init and Kthreadd)
    • Secondary three (i.e. the preceding 0, 1, 23 processes)
    • Sansheng all Things (process 1th is the ancestor of all user-state processes, and process 2nd is the ancestor of all kernel threads)

(2) NO. 0 Process Manual Write, 1th process copy, load init program

(3) How the shell command line starts the process

Fork the code of a child process:

1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4 intMainintargcChar*argv[])5 {6     intpid;7     /*Fork Another process*/8PID =fork ();9     if(PID <0) error handling Ten     {  One         /*error occurred*/ Afprintf (stderr,"Fork failed!"); -Exit (-1); -     }  the     Else if(PID = =0)  -     { -         /*Child Process* /Sub-process pid=0 when and else will execute a fork system call returns once on each parent process and child process  -printf"This is the child process!\n"); +     }  -     Else  +     {   A         /*Parent Process*/ atprintf"This is the Parent process!\n"); -         /*parent would wait for the*/ - Wait (NULL); -printf"Child complete!\n"); -     } -}

2. Understand how the process creates complex code

(1) Recalling the system call

(2) Where does the sub-process of fork start?

Compared to a thin kernel based on Mykernel writes.

(3) Create a new process in the kernel execution process

    • Fork, Vfork, and clone three system calls can create a new process, and all are created by calling Do_fork to implement the process;
    • Linux creates a new process by replicating the parent process, which gives us an idea of the framework that this process provides:
    • Copy a pcb--task_struct
      Err = Arch_dup_task_struct (tsk, orig);
    • 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);//This is just a copy of Thread_info, Rather than copying the kernel stack 
    • To modify the copied process data, such as PID, process chain list and so on to change it, see copy_process inside.
    • Look at the fork () from the user's code, the function returns two times, that is, each time it is returned in a parent-child process, the parent process returns from the system call is easier to understand, and the child process returns from the system call, where does it start executing in the process of system invocation? This involves the kernel stack data state of the child process and the consistency of the SP and IP in the thread record in Task_struct, where is this set? Copy_thread in Copy_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

(4) The understanding of complex things to preset a general framework.

(5) Creating a new process is done by replicating the current process.

(6) What to do in the process of creating a new process

3. Browse the key code related to the process creation process

(1) System call kernel processing function sys_fork, Sys_clone, sys_vfork

The end result is the execution of Do_fork ().

The function of the copy process in Do_fork ():

Specific:

To open a specific function for duplicating a PCB:

Open Alloc_thread_info ():

Copies the kernel stack data and specifies the first instruction address of the new process.

4. Where does the new process of creation start?

(1) When copying the kernel stack

Open Pt_regs:

The int instruction and save_all the contents of the kernel stack.

The following analysis Entry_32.s, that is, the master control program.

5. Using GDB to track the process of creating a new process

Third, after-school homework

1. Understand 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. Use GDB trace to analyze a fork system call kernel processing function Sys_clone, verify your understanding of the Linux system to create a new process, recommended in the lab Building Linux virtual Machine environment to complete the experiment.

4. Paying special attention to where the new process is being implemented? Why does it go smoothly? That is, the execution starting point is consistent with how the kernel stack is guaranteed.

5. According to the knowledge of this week, analyze the system call processing process of the fork function, write a signed blog, and in the blog article "real name (and the name of the final application certificate must be consistent) + Original works reproduced please specify the source +" Linux kernel Analysis "MOOC Course/http mooc.study.163.com/course/ustc-1000029000 ", the specific requirements of the blog content are as follows:

(1) The topic is self-proposed, and the content revolves around how to create a new process for Linux system;

(2) Can combine experiment, draw stack state to execute flowchart, etc.;

(3) The blog content needs to carefully analyze the starting point of the new process and the corresponding stack state.

(4) The summary section needs to clarify its understanding of the "Linux system to create a new process"

Linux Kernel Analysis Sixth Week study summary--description of the process and creation of the process

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.