"Linux Kernel Analysis" (vi)--linux system process creation __linux

Source: Internet
Author: User

Author: Sandy Original works reproduced please indicate the source
"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000 "
Experimental environment: C+linux64 bit (32-bit system may result in different)
In accordance with the academic integrity of the terms, I guarantee that the answer for my original, all the references to the external materials have been marked by provenance. One, about the Linux process

The system allows one process to create a new process, which is a subprocess, and a child process can create a new subprocess to form a process tree structure model. All processes of the entire Linux system are also a tree-shaped structure. The root is a system that is constructed automatically, that is, the No. 0 process executed in the kernel state, which is the ancestor of all processes. The 1th process (kernel state) is created by process No. 0, and number 1th is responsible for performing part of the initialization and system configuration of the kernel and creating several kernel threads for caching and virtual main memory management. Subsequently, the 1th process calls Execve () to run the executable program init and to evolve into the user state 1th process, the init process. It completes the system startup work according to the/etc/initab of the configuration file, creating number 1th, 2nd ... A number of terminal registration processes are Getty. Each Getty process Sets its Process group identification number and monitors the interface lines configured to the system terminal. When the connection signal from the terminal is detected, the Getty process will execute the registration procedure through the function execve () login, at which time the user can enter the registration name and password to enter the login process, if successful, by the login program through the function execv () execution shell, The shell process receives the PID of the Getty process and replaces the original Getty process. Another process is generated directly or indirectly by the shell.

The above process can be described as: Process number NO. 0->1 kernel process->1 number kernel thread->1 number user process (init process)->getty process->shell Process

Note that the above process description mentions that the 1th kernel process call executes Init and becomes the 1th User state process (the INIT process), where Init is a function and the latter is a process. The two are easy to confuse, the difference is as follows:

The 1.init () function runs in the kernel state, which is the kernel code

The 2.init process is the first user process that the kernel starts and runs in user state.

The 3.init () function call EXECVE () loads the executable from the file/etc/inittab init and executes, and the process does not use call Do_fork (), so two processes are 1th processes.

Excerpt from article http://www.xuebuyuan.com/1163044.html

There are four main components of the process: Process Control block (PCB): Process flag Process block: Process block can be shared with other processes: Exclusive space for private data and separate space for stack space, 32G in 32-bit Linux (if no one is considered a thread) , Process creation Process

The general process in Linux is created by an existing process, which is what we call the parent process, the child process. The specific creation is implemented through fork (). Let's take a look at the general working process of the 0.11 Core fork ():
1 in memory to request a page of memory storage Process Control block task_struct, and return the process number NR, and in the task array of NR store task_struct pointer, but also the task's current pointer to the NR place;
2 Copy the Task_struct content of the parent process into the task_struct of the new process as a template
3 The information in the task_struct, mainly for the work: set the parent process, clear the signal bitmap, time slice, run time, according to the current environment to set the TSS (kernel state pointer esp0 point to the top of the Task_struct page), set LDT selector, etc. ( Point to the corresponding LDT descriptor in GDT according to NR).
4 Set the code snippet for the new process, the base address of the data segment, and the section length: Update the code start address in Task_struct: Process number (NR) x64m, update the code snippet and data segment descriptor in the local Descriptor table in Task_struct.
5 Copy the parent process's page table catalog entries and page tables: In the page catalog table, copy the page table entries for the parent process, and the destination address is computed by the linear address of the new process; Request a free page for each corresponding page table catalog entry, update the page table entries with the page table address, and finally copy the items from the parent Process page table to the new process
Should be in the page table, that is, this time, the child process shares physical memory with the parent process.
6 Update the file information in Task_struct: The number of files opened plus 1, the parent process's current directory reference number plus 1.
7 The TSS and LDT descriptor entries are set: The TSS descriptor entries for the new task and the descriptor entries for the LDT segment are set in the Global Descriptor Chart (GDT) so that the TSS descriptor entries and the LDT description Fu Xiang point to the TSS and task_struct structures of LDT respectively.
8 Set the task to a ready state and return the new process number to the current process (the parent process).

Http://www.360doc.com/content/14/0928/15/12747488_413000182.shtml Three, clone, fork, vfork implementation Way

1, fork
Fork created by the child process to replicate the resources of the father process, including the content of memory task_struct content, the old and new process using the same code snippet, copy data segments and stack segments, where the replication of the annotated Copy_on_write technology, that is, once the child process began to run, The address space of the old and new processes has been separated and the two are running independently.
2, Vfork
The child process created by the Vfork function runs entirely on the address space of the parent process, and any modifications to the virtual address space by the child are seen by the parent process. This is completely different from the fork, and the fork process is a separate space. Another difference is that after the subprocess created by Vfork, the parent process is blocked until the child process executes exec () and exit ().
3. Clone
The function is powerful, with many parameters, so the process created by him is more complex than the previous 2 methods. Clone allows you to selectively inherit resources from the parent process, you can choose to share a virtual space with the parent process like vfork, so that you create threads, you can not share them with the parent process, you can even choose to create processes and the parent process is no longer a parent-child relationship, but a brotherhood

int clone (int (FN) (void), void *child_stack, int flags, void *arg);

Here fn is the function pointer, we know the 4 elements of the process, this is the pointer to the program, is called "script", Child_stack is obviously allocated to the child process stack space (in Linux system stack space is 2 pages, is 8K of memory, which in this block of memory, A value is placed on a low address, which is the value of the Process Control block task_struct), flags are flags that describe the resources you need to inherit from the parent process, and ARG is the parameter that passes to the child process.

Summarize:
Same:
The system invokes the service routine Sys_clone, Sys_fork, sys_vfork the final call to the Do_fork function completion.
The parameters of the do_fork are similar to those of the clone system call, but there is one more regs (user-mode registers saved by the kernel stack). In fact, all the other parameters are taken with regs.
The difference is:
Clone
Clone's API Cloak, push FN, arg into the user stack, and then raise system calls. When you return to user mode, the next instruction is FN.
Sysclone:parent_tidptr, child_tidptr all passed through the parameters of Do_fork.
Sysclone: Check if there is a new stack, and if not, use the parent process stack (the start address is REGS.ESP)
Fork, Vfork:
A service routine is a direct call to Do_fork, but the parameters are slightly modified
Clone_flags:
sys_fork:sigchld|0;
sys_vfork:sigchld| (Clone_vfork | clone_vm)
User stacks: Are the stacks of the parent process.
Parent_tidptr, child_ctidptr are all null.

Http://www.xuebuyuan.com/1163044.html

Reference documents
Http://www.xuebuyuan.com/1163044.html
Http://www.360doc.com/content/14/0928/15/12747488_413000182.shtml
Http://www.cnblogs.com/LittleHann/p/3853854.html
Http://www.tucaobj.com/note/os/201407191030272928.jhtml
Http://www.cnblogs.com/LittleHann/p/3853854.html

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.