Fork () && fork () | | Fork ()

Source: Internet
Author: User

http://blog.csdn.net/hs794502825/article/details/10242091

    1. #include <unistd.h>
    2. #include <stdio.h>
    3. int main ()
    4. {
    5. Fork (); /*****/  
    6. Fork () && fork () | | Fork (); /*****/  
    7. Fork (); /*****/  
    8. Sleep (100);
    9. return 0;
    10. }

The question is not the main process itself, how many processes did the program create?

This is one of EMC's pen questions, it is very interesting, the problem is the main test of two points of knowledge, one is the characteristics of the operation of the logical operators; second, the understanding of fork


If there is one such expression: Cond1 && Cond2 | | Cond3 How will this code be executed?


1, Cond1 is false, then do not Judge Cond2, and then Judge Cond3
2, Cond1 is true, this is also divided into two situations:
A, Cond2 is true, this does not need to judge Cond3
B, Cond2 is false, that also have to Judge Cond3


One of the wonders of fork invocation is that it is called only once, but it can return two times, and it may have three different return values:
1. In the parent process, fork returns the process ID of the newly created child process;
2, in the sub-process, fork returns 0;
3. If there is an error, fork returns a negative value (this is not considered in the dry case)


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.


With the above knowledge, let's analyze fork () && fork () | | Fork () creates several new processes

It's obvious. Fork () && fork () | | Fork () creates 4 new processes

Summarize:

The fork of the first comment line generates 1 new processes
The three fork of the second comment line is generated 4+4=8 a new process
The ORK of the third comment line generates 10 new processes (this is because there are 10 processes in front of each, calling fork to generate 10 new processes at a time)

So there's going to be a new 1+8+10=19 process.

Fork () && fork () | | fork ()

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.