Difference between fork and vfork

Source: Internet
Author: User

# Include <unistd. h>
Pid_t fork (void );

The new process created by fork is a child process. The fork function is called once but returned twice. The only difference between the two is that the return value of the sub-process is 0, and the return value of the parent process is the ID of the new sub-process. The reason for returning the child process ID to the parent process is that there can be multiple child processes of a process, and no function allows a process to obtain the ID of all its child processes. Fork causes the sub-process to return a value of 0: A process has only one parent process, therefore, sub-processes can always call getppid to obtain the ID of the parent process (process ID 0 is always used by the kernel exchange process, so the process ID of a sub-process cannot be 0)

A child process is a copy of the parent process. The child process obtains the data space, stack, and stack copies of the parent process. The Parent and Child processes do not share these bucket parts. The Parent and Child processes share the body segment.

Because exec is often followed after fork, many implementations do not execute full replication of data segments, stacks, and stacks of a parent process. Instead, we use the copy-on-write (COW) technology. These areas are shared by the parent and child processes. If any of the Parent and Child processes tries to modify these areas, the kernel is only the memory (usually a page) of the region to be modified) make a copy.

One feature of fork is that all open file descriptors of the parent process are copied to the child process. Each identical open descriptor of the Parent and Child processes shares a file table item. Therefore, the Parent and Child processes need to synchronize file operations (this usually does not occur, because the parent process usually waits for the child process to finish running, or the parent and child processes execute different program segments, that is, the sub-process uses Exec ).

When the child process uses the exec function, the system replaces the code segment with the code of the new program, discards the original data segment and stack segment, and allocates a new data segment and stack segment for the new program, the only thing left behind is the process ID. That is to say, for the system, it is the same process, but another program has been used.

# Include <unistd. h>
Pid_t vfork (void );

Vfork is used to create a new process, which aims to Exec a new program. Both vfork and fork create a child process, but it does not completely copy the address space of the parent process to the child process, because the child process will immediately call exec (or exit ), therefore, the address space will not be stored.

On the contrary, before a child process calls exec or exit, it runs in the space of the parent process. This optimization method improves efficiency in Some UNIX page-based Virtual Memory implementations (compared to Cow Technology ).

Another difference between vfork and fork is that vfork ensures that the sub-process runs first. After it calls exec or exit, the parent process can be scheduled to run. (If the sub-process depends on the further action of the parent process before calling these two functions, a deadlock will occur .)

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.