Xin Xing analyzes basic process knowledge in Linux and a simple understanding of fork. Xin Xing fork

Source: Internet
Author: User

Xin Xing analyzes basic process knowledge in Linux and a simple understanding of fork. Xin Xing fork

A process can be considered as an execution process of a program. In Linux, each process has a unique PID to identify the process. PID is a positive integer from 1 to 32768, where 1 is usually init, other processes start from 2 and start from 2 after 32768 is used up.

In Linux, there is a table structure to store the processes currently in progress. We can use ps aux or ps ef to view the processes in progress. Processes in Linux are tree-like. init is the root node, and other processes have parent processes. The parent process of a process is the process that starts the process. This process is called the child process of the parent process.

Fork is used to copy all the data of a new process, such as variables, environment variables, and program counters, of the same process as the current process, but it is a new process and serves as a sub-process of the original process.

For efficiency considerations, Linux introduces the Replication Technology During writing, that is, only when the partition content of the process space needs to change, to copy the content of the parent process to the child process. Before the exec process after fork, the two processes use the same physical space, that is, the same memory zone. The child process code segment, data segment, and stack segment all point to the physical space of the parent process, that is to say, although the two have different virtual spaces, the corresponding physical space is the same.

When a child process modifies the corresponding segment, it allocates physical space for the corresponding segment of the child process. If it is not because of exec, the kernel allocates physical space to the data and stack segments of the sub-process. Now, both have their own process space, which does not affect each other, the code segment will continue to share the physical space of the parent process. If it is because of exec, because the code executed by the two processes is different, the child process code segment will also be allocated a separate physical space.

After fork, the kernel puts the sub-process in front of the queue to let the sub-process execute first, so as to avoid the execution of the parent process resulting in write-time replication, and then the sub-process executes the exec system call, because of meaningless replication, the efficiency is reduced.

That is to say, through the write-time replication technology, the kernel only creates virtual space structures for newly generated sub-processes. They are used to copy the virtual structure of the parent process, but do not allocate physical memory for these segments, they share the physical space of the parent process, but when the child process changes the behavior of the corresponding segment, allocate physical space for the corresponding segment of the child process.

Vfork does not create the virtual address space structure of the kernel-connected sub-process. It directly shares the virtual space of the parent process, and directly shares the physical space of the parent process.

The traditional fork call is to copy all the resources to the newly created process. This implementation is too simple and efficient, because the data it copies may not be shared, if the new process intends to execute a new image immediately, all copies will be discarded.

In Linux, fork uses a copy at write time, which can delay or even avoid copying data. In this case, the kernel does not copy the whole process address space, but shares the same copy with the child process, when writing data, the data will be copied, so that each process has its own copy. That is to say, the replication of resources is only performed when necessary. Before that, it is shared in read-only mode.

That is to say, the actual overhead of fork is to copy the unique process descriptor created by the parent process page table for the child process. Generally, an executable file will be run immediately after the process is created, this kind of optimization can avoid copying a large amount of data that is not used at all. Because UNIX emphasizes the fast execution capability of processes, this optimization is quite important.

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.