Fork and vfork

Source: Internet
Author: User

 

Article 1: fork and vfork Functions

Http://shichen515.blog.sohu.com/68965577.html

 

I. Fork
1. Call Method
# Include <sys/types. h>
# Include <unistd. h>

Pid_t fork (void );
Correct return: returns the process number of the child process in the parent process, and returns 0 in the child process.
Error returned:-1

A child process is a copy of the parent process. That is, the child process obtains copies of the Data Segment and stack segment from the parent process, which requires a new memory allocation. For read-only code segments, shared memory is usually used for access. After fork is returned, both the child process and the parent process start to execute the next statement that calls the fork function.
The difference between a parent process and a child process is that the fork return value is different. The return value in the parent process is the process number of the child process, and the return value of the child process is 0.
2. Purpose of fork function call
(1) A process wants to copy itself so that the parent and child processes can execute code of different segments at the same time.
(2) The process wants to execute another program.

Ii. vfork
1. Call Method
The function is exactly the same as the fork function.
# Include <sys/types. h>
# Include <unistd. h>

Pid_t fork (void );
Correct return: returns the process number of the child process in the parent process, and returns 0 in the child process.
Error returned:-1

2. Use of vfork function call
The process created with vfork mainly aims to execute another program using the exec function, which is the same as the second purpose of fork.

Iii. Differences between fork and vfork
1. Fork needs to copy the data segment of the parent process, while vfork does not need to completely copy the data segment of the parent process. Before the child process does not call exec or exit, the child process shares the data segment with the parent process.
2. fork does not limit the execution sequence of parent and child processes. In a vfork call, the child process runs first and the parent process hangs until the child process calls exec or exit, the execution sequence of parent and child processes is not limited.

 

-- Vfork ensures that the child process runs first. After the child process 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.

 

4. Terminate sub-Processes
To end a sub-process, use _ exit (0) instead of exit (0 ). This is because _ exit (0) does not perform any operations on the standard I/O Stream when the process ends. Exit (0) will close all standard I/O streams of the process.

 

Article 2: differences between vfork, fork, and exec Functions

Http://www.yuanma.org/data/2007/0509/article_2591.htm

 

Vfork is used to create a new process, and The purpose is to Exec A New Process,
Both vfork and fork create child processes, but it does not set the address space of the parent process
Completely copied to the sub-process, because the sub-process will immediately call exec, so it will not store the address space. However, before the child process calls exec or exit, it runs in the space of the parent process.

Another difference between vfork and fork is that vfork ensures that the sub-process runs first. Only after she calls exec or exit can the parent process 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.

After the fork function is used to create a sub-process, the sub-process usually calls one exec function to execute another program,
When a process calls an exec function, the process is completely replaced by a new program, and the new program is executed from its main function, because it does not create a new process when exec is called, so the process ID does not change. Exec only replaces the body, Data, heap, and stack segments of the current process with another new program.

 

Related Article

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.