Working Mechanism of fork () Functions

Source: Internet
Author: User
For details about how fork () functions work-Linux general technology-Linux programming and kernel information, see the following. When I look at the Linux source code, the fork () function is really strange. When I call it once, two different values will be returned for the child process and the parent process, which is puzzling, how can a function return two values. I finally found the answer by searching the Internet. As follows:

Fork () is not a process switch, but a copy of the current process. For example, if the initial process number is 100, after the fork () system is called, the operating system copies the process No. 100 to a new process No. 101. The so-called same refers to any information contained in processes No. 100 and No. 101, such as the value of variables, space allocation, and especially the statements being executed. At this time, your set of code will be executed in both processes at the same time, just like an EXE file is executed twice at the same time. Since the statements to be executed by the two processes in the Next Step are returned from fork (), the operating system specifically asks fork () in process 100 to return 101, while fork () in process 101 () return 0, so that a set of code can identify the process in which it is located.
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.