How do 2 or more fork system calls work?

后端 未结 4 418
天命终不由人
天命终不由人 2020-12-12 06:58

Here\'s a code where I use 2 fork() system calls one after another - How does it actually work?

 #include 
 #include 
 usi         


        
4条回答
  •  天命终不由人
    2020-12-12 07:37

    when a fork() is called, a child process is created and ran. So you are getting the following statement executed once in the child process:

    cout << "1. I am process " << getpid() << endl;
    

    Further, when another fork is called, another child process is created which runs the next 'cout' statement. However, the parent process is also ran. This happens for the third fork() also.

    All this takes place in the child process of the first fork(). After this the parent process for the 1st fork runs as well to show your output.

提交回复
热议问题