Reputation: 3408
I have the following code:
#include <unistd.h>
#include <stdio.h>
main()
{
int i;
for(i=0;i<3;i++)
if(i%2==0)
fork();
else{
fork();
fork();
}
}
I understand that it has 16 processes in the end (or at least I think it does), but what I don't understand is which processes are the parent processes and which are the child processes. So let's just say that P1 is process 1, P2 is process 2, etc. Is P1 the parent of P2, P3, P5, and P9 (I drew this out in a tree but I'm not sure how accurate my tree is), while P2 is the parent of P4, etc.? Or does P1 become the parent of P2, P3, P4, and something else happens that I don't realize? I'm just kind of stuck.
Upvotes: 0
Views: 156
Reputation: 182619
I second Jonathon Reinhart's sugestion but instead of ps
you might be better served by pstree -p
. A quick try shows:
─try(A)─┬─try(B1)─┬─try(C2)─┬─try(D1)───try(13874)
│ │ └─try(13871)
│ ├─try(C3)───try(13868)
│ └─try(13867)
├─try(B2)─┬─try(C1)───try(13875)
│ └─try(13872)
├─try(B3)───try(13873)
└─try(13866)
Back to your question, this is a classic bookkeeping exercise. The one trick is just that: computers are better at bookkeeping.
To simplify things I will present them as if all the processes take a deep breath when a loop iteration is done: this doesn't happen in practice.
In this iteration everybody takes the else
.
And the fun continues with the next iteration, where all of them will have i==2
.
Upvotes: 2
Reputation: 9819
At the end, no processes will be left because all of them do nothing but exit after forking. But while they are running, each of the processes is the direct parent of the one it forked. However, since it's undefined which process gets how much CPU time after forking, after the first fork (P1 spawns P2), it's random if P1 gets CPU time first, so P1 spawns P3, or if the child P2 runs first and spawns P3.
If you put a sleep(1000) at the end of your program and let it run a few times, and use pstree
to check them, you'll see that you'll get a different pattern every time.
Upvotes: 0
Reputation: 137398
At the end of your main()
function, add a call to getchar()
. This will keep all processes running indefinitely. Then run $ps -ef
to see the full process list. This will show each process's PID and it's parent process's PID. You will then easily be able to derive the hierarchy.
Upvotes: 0