Linux Command Fork ()

Source: Internet
Author: User
Linux Command Fork ()-General Linux technology-Linux programming and kernel information. The following is a detailed description. Fork is the meaning of fork. That is to say, it will play a "Forks" role. In Unix operating systems, it is often used to generate new processes. So what will happen when fork is called? First, the parent process shares a code segment with the child process, and the data segment and stack segment are copied to the child process by the parent process. When the generation is complete, the parent process and the child process are already two independent processes, especially the data segment, which are separated (although they are the same at the beginning, after their respective operations, is Different), does not share data.

If fork is successful, the return value of fork in the child process is 0, and the return value of fork in the parent process is the process number of the child process. If fork is not successful, the parent process returns an error. In this program code, we may have misunderstandings. When we see the if-else structure, we will divide it into three segments: pid <0, pid = 0 and pid> 0. When a certain condition is met, a program will be implemented. But this is not the case.

We can think that if there is no wait function, the parent process and the child process run in parallel and the steps are consistent (the actual operating system depends on who runs first ). After fork, they perform different work. The returned pid is only used to distinguish between parent and child processes.
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.