Fork and vfork

Source: Internet
Author: User

1. fork
[Cpp]
# Include <sys/types. h>
# Include <unistd. h>
Pid_t fork (void );

# Include <sys/types. h>
# Include <unistd. h>
Pid_t fork (void); correct return: The process number of the child process returned by the parent process; 0 returned by the child process
Error returned:-1;
A child process is a copy of the parent process. The child process obtains the data segment and stack segment from the parent process, but allocates the memory separately instead of sharing the data with the parent process. After the fork function returns, both the child process and the parent process start to execute the next statement of the fork function. Because the sub-process has nothing to do with the execution of the parent process, the parent process can run before the sub-process, or the sub-process can run before the parent process.
Fork uses the write-time replication technology in linux. It shares the data segment of the parent process at the beginning and only copies the data segment when writing.
 
 
2. vfork
[Cpp]
# Include <sys/types. h>
# Include <unistd. h>
Pid_t vfork (void );

# Include <sys/types. h>
# Include <unistd. h>
Pid_t vfork (void); Return Value same as above
The main purpose of vfork to create a new process is to use the exec function to execute another program. In fact, before exec or exit is called, the child process shares the data segment with the parent process. In a vfork call, the child process runs first, and the parent process hangs until the child process calls exec or exit. After that, the execution sequence of the Parent and Child processes is no longer limited.
 
 
 
Differences:
1. The child process created by fork () is a copy of the parent process. That is, the child process obtains the data space, heap, and stack copies of the parent process. Parent and Child processes do not share these buckets. The process created by vfork () does not completely copy the address space of the parent process to the child process. Because the child process immediately calls exec (or exit), it will not store the address space. On the contrary, before a child process calls exec or exit, it is performed in the space of the parent process.
2. Another difference between vfork () and fork () Is that vfork ensures that the sub-process runs first and shares data with the parent process before exec or exit is called, after it calls exec or exit, the parent process can be scheduled to run, while the fork Parent and Child processes are at the same level, without any restrictions. Vfork ensures that the sub-process runs first. If the sub-process depends on the further action of the parent process, a deadlock will occur.
3. instance:
A. Now P1 uses the fork () function to create a sub-process P2 for the process. kernel: (1) copy the four parts of the P1 code segment (body segment), data segment, heap, and stack. (2) Allocate physical blocks for these four parts. The P2 code segment points to the physical block of the P1 code segment, which is actually not allocated to P2, the data segment points to P2's own data segment (corresponding block is allocated to it), the heap points to P2's own heap, And the stack points to P2's own stack. As shown in: the arrow from left to right indicates copying content.



Figure 1

 

B. Write-time replication technology: the kernel only creates virtual space for newly generated sub-processes, which are copied to the virtual space of the parent process, but do not allocate physical space for these segments, they share the physical space of the parent process. When the Parent and Child processes write memory, they allocate physical space for the corresponding segments of the child process.

 



Figure 2


C. vfork (): This is more radical. The virtual address space of the sub-process does not need to be created, and the virtual space of the parent process is directly shared. This also deletes unnecessary copies for subsequent exec operations.

 

 
 

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.