Linux kernel source code scenario analysis (5)-about fork/clone/vfork

Source: Internet
Author: User

(Recently, I have returned to the Linux kernel. Continue .)

The process in Linux is similar to the thread. One reason for this is that they all use the same data structure, that is, task_struct. The difference is that there is an independent user space, and one is a shared user space (if there is no user space, it is a kernel thread, no need ).

Linux User processes cannot be directly created because such APIs do not exist. It can only be copied from a process, and then switched to the program file to be run through the exec API.

There are three types of replication APIs: fork, clone, and vfork.

The three APIs actually call a kernel internal function do_fork, but the parameters are different.

Vfork is actually part of the fork process to simplify and improve efficiency. Fork differs from clone. Fork is the full replication of Process resources, including the PCB of the process, the system stack of the process, the user space of the process, and the device opened by the process. In clone, only the first two items are copied, and the last two items are shared with the parent process.

In the replication of the four resources, the user space is relatively large. If the replication is complete, the efficiency will be very low. In Linux, the "Copy at write time" technology is used. That is to say, when the fork is executed, it does not actually copy all pages of the user space, but just copies the page table. In this way, no matter the parent process or child process, when a user space write operation occurs, the "write copy" operation will be triggered, so that an available user space will be allocated separately to make it completely independent. This is a very effective way to improve efficiency.

For clone, these page tables are shared with the parent process, so they are truly shared. Therefore, the protection of shared data must be ensured by upper-layer applications.

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.