Linux Kernel Analysis--sixth week study Note 20135308

Source: Internet
Author: User

The description of the week process and the creation of the process the process descriptor TASK_STRUCT data structure 1. Operating system three major functions
    • Process Management
    • Memory management
    • File system
2. Process Control block Pcb--task_struct

also called the process descriptor, in order to manage the process, the kernel needs to describe each process, which provides the process information that the kernel needs to understand.

struct TASK_STRUCT data structure is very large, 1235 rows ~1644 rows

3.Linux process Status

The state of the Linux process differs from the process state described in the operating system principle

Operating System Status:

    • Ready state
    • Operating State
    • Blocking states

Linux process Status:

4. Understanding TASK_STRUCT Data Structures

Ii. creation of the process 1. Overview of process creation and the user-state code of a process (1) Review of the origin of the process

Start_kernel's last Rest_init created two kernel_thread (kernel threads)

    • Kernel_init User-State process
    • Kthreadd ancestor of all kernel threads

The above creation process is the same as when we started a process under the shell command line, the creation process is essentially the same.

(2) Copying a copy of the process descriptor

In fact, the NO. 0 process is manually written to its process descriptor data, process 1th is created to replicate the No. 0 process of the PCB, according to the needs of process 1th, modify the PID, loading an init executable program.

How is the process created?

Fork the code of a child process

#include <stdio.h>#include<stdlib.h>#include<unistd.h>intMainintargcChar*argv[]) {    intpid; /*Fork Another process*/PID=Fork (); System call for creating a child process in the user stateif(PID <0)//error handling, after which none of the two will execute {/*error occurred*/fprintf (stderr,"Fork failed!"); Exit (-1); }     Else if(PID = =0)//Here Pid=0, the following two will be executed. Because the fork system call returns each time in the parent process and the child process {/*Child Process*/printf ("This is the child process!\n"); }     Else     {          /*Parent Process*/printf ("This is the Parent process!\n"); /*parent would wait for the*/Wait (NULL); printf ("Child complete!\n"); }}
2. Understanding the process creation process complex code method (1) System call review

(2) Chong build 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); Copy the data structure of the parent process in this function
    • To assign a new kernel stack to the new process
$ ti = alloc_thread_info_node (tsk, node); $ tsk->stack =// here just copy thread_info, not copy kernel stack
    • To modify the copied process data, such as PID, process chain list and so on to change, see copy_process inside.
    • Look at the fork () from the user-state 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. So where does it start executing in the process of system call processing? 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
    // Copy the kernel stack    0 // Why the fork of a subprocess returns 0, here's why      $ plong// top of kernel stack when dispatched to child process    Long // the address of the first instruction when dispatched to a child process
Third, the experiment

1. Delete Menu,clone a new menu, overwrite test_fork.c and test.c, and re-execute make Rootfs

We find that the fork function is more

2. Same as before, start GDB

3. Because the fork actually executes clone, the breakpoint is set here in Sys_clone, as well as other key places

4. Continue execution, stop at Do_fork's location

5.next ... Next is some error handling, until copy_process

6. Continue stepping, the program stopped at the Dup_task_struct function, and now the parent process of the PCB, that is, the TASK_STRUCT data structure copied over, that is, the P point to the sub-process of the PCB

Go in and find out

7. In the Copy_thread function, next, from here can be seen from the child process PID, that is, the location of the kernel stack, found the stack space, save_all some content, Save_all address

Find the address of its press stack

8. The values in the kernel stack register of the current process are copied into the child process

9. Set the starting point for the child process to be dispatched

10. For Ret_from_fork to continue to perform single-step debugging, the current system is executing assembly code.

.

11. When the program jumps to Syscall_exit, it is no longer possible to continue GDB tracking debugging.

Iv. Summary

1.Linux creates a new process by copying the parent process, by calling Do_fork to implement

2.Linux assigns a structure dynamically to each newly created process task_struct .

3. In order to organize all the processes in the kernel, Linux provides several organizational ways, in which the hash table and the two-way circular list are targeted at all processes in the system (including kernel threads), while the run queue and wait queue are organized in the same state process.

The 4.fork () function is called once, but returns two times

To create a new process in the kernel execution process
    • Whether you use fork or vfork to create a process, you end up with the Do_fork () method.
    • call copy_process, copy a pcb--task_struct, assign a new kernel stack to the new process
    • Modify the copied process data, such as PID, process chain list and so on to change.

    • The fork () function returns each time in a parent-child process,

Linux Kernel Analysis--sixth week study Note 20135308

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.