Linux kernel-The process of analyzing the Linux kernel creating a new process 20135311 Fu Dong

Source: Internet
Author: User

20135311 Fu Dong

Analyzing the process of creating a new process in the Linux kernel learning content

Process Control block--PCB TASK_STRUCT data structure

The PCB task_struct contains:

进程状态、进程打开的文件、进程优先级信息

操作系统管理的三个功能:

1.进程管理 2.内存管理 3.文件系统

Status of the Linux process:

Process State Analysis:

The long state is the running status of the process, 1 is not executed, 0 is in execution, and greater than 0 is the pause;

*stack is to build a kernel stack;

Flags is the identifier that defines each process;

List_head tasks This part is to define the doubly linked list;

Mm_struct *MM This part is defined in memory management, the process of the address space;

The above part of the code also belongs to the process task stack;

Defines the PID identifier of the process;

Define the parent-child inheritance relationship between processes;

Definition of CPU-related threads (in Linux systems, the system treats threads as lightweight processes, essentially treats processes and threads equally)

This section defines the data structure, file structure, signal volume, etc. of the thread;

Process Creation Analysis:

Most processes are created by the fork () function, whereas calling the fork () function actually invokes clone ();

Fork () function code and Analysis:

#include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 int main (int argc, char * argv[]) 5 {6     int pid; 7     /* Fork Another process *     /8 pid = fork ();//Call fork to create a process; 9     if (PID < 0)         */ Error occurred */12         fprintf (stderr, "Fork failed!");         ( -1); (+)     if (pid = = 0)-{+     */child         Process *         /printf ("This is Chil D process!\n ");//create child process  Fork system call returns once for each parent process and child process
\ N/* Parent process */23 printf ("This is the parent process!\n"); 24 / * Parent'll wait for the child to COMPLETE*/25 Wait (NULL); --printf ("Child complete!\n"); }28 }


fork、vfork和clone三个系统调用都可以创建一个新进程,而且都是通过调用do_fork来实现进程的创建。

The creation of a new process is achieved by replicating the current process. The do_fork primary is to replicate the parent process task_struct , and then modify the necessary information to get the child process task_struct . 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);

这里只是复制thread_info,而非复制内核堆栈To modify the copied process data, such as PID, process chain list and so on.

Second, the experimental process

Iii. Summary of Learning

1.Linux implements Fork () via Clone () system call

2.fork (), Vfork (), and clone () library functions call Clone () according to their desired parameter flags, and then call Do_fork by Clone (). Where the Do_fork function calls the Copy_process () function, and then lets the process execute Linux to create a new process by copying the parent process, fork, vfork, clone are created by Do_exit implementation of the process.

3. What the process creates:

Copy a PCB struct

Assigning a new kernel stack to a new process

Initializes the child process to modify the replicated data.

4. The parent and child processes are returned one at a time.

Linux kernel-The process of analyzing the Linux kernel creating a new process 20135311 Fu Dong

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.