Fork () function, one call, two returns

Source: Internet
Author: User

Reference: http://blog.csdn.net/dog_in_yellow/archive/2008/01/13/2041079.aspx

I used to be confused. What is one call and two responses. Search online to find out why. Now, we will record our understanding here.
Prerequisites:
The process in the memory includes three parts: executable files (namely programs), related data (including variables, memory space, buffers, etc.), and Context Environment (which is personally understood as the origin, ). We know that the CPU resources of a computer are limited, and there is only one single core, and there are not infinite multi-core resources. The number of programs currently running is always greater than the number of CPUs (this should be feasible, and no manufacturer or individual is so arrogant that it wastes CPU resources ). Therefore, under the scheduling of the operating system, a program is generally not executed from the beginning to the end without interruption. The system will determine which program occupies the current CPU according to the program execution sequence and priority level. The interrupted programs need to save the state of the interrupted time (that is, the three parts of the process must be recorded), so that the re-execution can completely recover to the past of the interrupted, if the interrupted time is short enough, it should give people a smooth feeling, that is why we can listen to music while reading this article. I guess that's why a program running in the memory is called a process.

In this way, we can start to discuss the fork function. Assume that a process contains the code P = fork ().

We call the process currently calling p = fork () as the parent process. The PID of the parent process can be obtained using getpid. Fork () returns a value to the variable P. Under normal circumstances, P should be a positive integer, indicating the PID Number of the newly generated sub-process of Fork.

The child process generated by fork () is exactly the same as the parent process (at least at the moment when the parent process calls fork ), it is also completely independent (that is, the execution sequence is completely scheduled by the operating system, and the parent process is not necessarily executed before the child process ). The fork () function is not the same as a general function. The parent process does not wait until the subprocess generated by the fork function is fully executed and then execute the code after pid = fork. Or, it can be understood that fork () is used to generate a sub-process. It does not matter whether or not the sub-process runs or how it runs.

The execution of sub-processes is discussed. As we know, since fork () is a replication parent process, the conditions before P = fork () statements should be completely consistent. In the parent process, the P variable gets the PID of the child process, but there is also such a P variable in the child process. is its value also the PID of the child process? This is the key! In fact, the value obtained by the P variable in the child process is not the PID of the child process, but 0. The PID of the sub-process can be obtained in the sub-process using getpid. This is also why fork () is called once and returns twice, that is, the final result is equivalent to: a program is called twice to form two processes, before P = fork, the two processes are exactly the same. In this sentence, the value of P variable in one process is the PID of another process, and the value of P variable in another process is 0, the two processes are separated and there is no difference between them. (Note that two processes can be caused by the same program .)

Fork () function, one call, two returns

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.