Vfork and Fork Differences

Source: Internet
Author: User

Fork


An existing process can call fork to create a new process.


return value: 0 is returned in the child process, the parent process returns the child process ID, and an error returns zero.

A child process is a copy of the parent process.


An existing process can call the fork function to create a new process. A new process created by Fork is called a subprocess (child process). The fork function is called once but returns two times. The only difference of two returns is that the child process ID is returned in the parent process with a value of 0.

A child process is a copy of the parent process that obtains a copy of the parent process's data space, heap, stack, and so on. Note that the child process holds a "copy" of the above storage space, which means that these storage spaces are not shared between parent and child processes.

UNIX copies the address space content of the parent process to the child process, so that the child process has a separate address space. Under different Unix (like) systems, it is not possible to determine whether the sub-process runs first or the parent process before the fork, depending on the implementation of the system. Therefore, we should not make any assumptions about this when porting the code.

Why is fork returned two times?

Because the stack segment of the parent process is copied at copy time, two processes are stuck in the fork function, waiting to be returned. So the fork function returns two times, once in the parent process, and the other in the child process, and the return value of the two times is different. Process such as.

After the fork function finishes executing, if the new process is created successfully, there are two processes, one child process and one parent process. In the subprocess, the fork function returns 0, and in the parent process, fork returns the process ID of the newly created child process. We can determine whether the current process is a child process or a parent process by the value returned by the fork.

The value returned by the parent process's fork function points to the process ID of the child process because the child process has no child processes, so its fork function returns a value of 0.

After calling fork, the data, stack has two copies, the code is still a copy, but this code snippet becomes two processes of the shared snippet is returned from the fork function, the arrows represent the respective execution place. When a parent-child process has a want to modify data or a stack, two processes really split.

One feature of fork is that all open file descriptors of the parent process are copied into the child process. Each of the same open descriptors for a parent-child process shares a file table entry.

There are two common scenarios for file descriptors that are processed after fork:


1. The parent The process waits for the child process to complete. In this case, the parent process does not need to do any processing of its descriptors. When the child process terminates, the child process modifies the file offset to the update that was performed.


    2. Father and son


Differences between parent and child processes:


1. Return value of fork
2. ProcessID is different
3. Have a different parent processtms_utime, tms_stime , tms_ustime are set to 6. Unhandled alarms for child processes are cleared
Span style= "Line-height:0px;color:rgb (50,51,50); font-family:baskerville;font-size:12pt;" >7. The unhandled signal set of the child process is set to the empty setting

reason for Fork call failure:


1. There are too many processes in the system

2. the number of actual user processes exceeds the limit

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/84/09/wKioL1eEUlvRnS0qAAD830u-PLI319.png-wh_500x0-wm_3 -wmp_4-s_582729313.png "title=" Fork.png "alt=" Wkiol1eeulvrns0qaad830u-pli319.png-wh_50 "/>


Vfork function


for VforkTo create a new process, and the purpose of the new process is toEXEC OneA new program.VforkAndForkAll Create aA child process, but it does not copy the address space of the parent process into the subprocess because the child process immediately calls theExec, the address space is not saved. InsteadCalled in a child processExecOrexitvfork and fork Ensure that the child process runs first, in which it calls or (exit.


Vfork and Fork Differences

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.