How do 2 or more fork system calls work?

后端 未结 4 430
天命终不由人
天命终不由人 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:27

    Just draw a tree where each root node is a fork call and leaf nodes are the code following it.

    In the first program, your cout<<"0..." is the parent program and after the fork call, the whole program after that line executes twice.Hence the two "1..." output lines.

    Now there is another fork call after this. This time there are 3 processes running( 1.The original parent you invoked, 2. The child it spawned 3. The child itself spawns another grandchild.)
    Hence the 3 "2..." output lines.

提交回复
热议问题