After the fork()
, the process (child or parent) which runs next is essentially random. The if
is true for all parent processes and half the child processes.
The randomness persists for each child when it becomes a parent.
Boo 3
Boo 3
Boo 2
Boo 2
Boo 1
Boo 1
Boo 1
Boo 1
Boo 1
Boo 1
Boo 1
Boo 1
This seems pretty logical. But on another run, I got this:
Boo 3
Boo 2
Boo 1
Boo 3
Boo 2
Boo 1
Boo 1
Boo 1
Boo 1
Boo 1
Boo 1
Boo 1
In this case, one of the first processes didn't get to run until after a great ancestor of the other.
There is no predictability to process scheduling. That is simply the nature of operating systems. If you need some sort of sequence control, extra code must be added. Perhaps some timers, or mutexes, or master scheduling.