Reputation: 37
**pseudocode!
fork();
printf(--print process ID);
fork();
printf(--print process ID);
fork();
printf(--print process ID);
This...I am assuming results in 8 processes today (correct me if I am wrong).
Now, if wait calls are added to this, what would happen?
fork();
wait(..);
printf(--print process ID);
fork();
wait(..);
printf(--print process ID);
fork();
wait(..);
printf(--print process ID);
I am thinking that both 1) the number of processes will be the same and 2) the printf()
prints would also look identical?
Upvotes: 2
Views: 981
Reputation: 1912
True. The wait calls will impose some constraints on the order in which the processes execute, but you will still have 8 processes. The printf statements will look the same, but the order in which they might change.
It's possible but not guaranteed in the first version that all 8 processes will be executing at the same time, in the version with waits the number of concurrent processes will certainly never reach 8.
On my system, wait() will only return when direct children complete, such that your version with wait will print deterministically: children will always complete before parents continue.
Typically, you would see
N+1 //first printf
N+2 //second printf
N+3 //third printf
N+2 //third printf
N+1 //second printf
N+4 //third printf
N+1 //third printf
N //first printf
N+5 //second printf
N+6 //third printf
N+5 //third printf
N //second printf
N+7 //third printf
N //third printf
Where N is the pid of the root process. Getting a nice block of contiguous PID numbers depends on the usual pid assignment scheme I tend to see on *nix systems, and not managing to start some other process while your toy program is running.
I'm not familiar with a sufficient variety of operating systems to say 100% that you'll never come across a wait() that will reap grandchildren, but I would be very surprised to hear about it.
Upvotes: 3