Animated Example #5

Starfork

Try to hand trace the output of this code by drawing a fork diagram, keeping track of each process and their function calls. When you're ready to check your answer, press the button below and watch the animation to see how I draw a fork diagram for this program.

Show Answer Hide Answer

*
**
**
*

The output for this program is fixed. This means that the order of the output does not change from one run to another because waitpid() essentially adds "order" to the star() calls.

Now, click play to watch an animated visualization of this program. Pay special attention to the effect that a waitpid() call has on each parent process, the star() calls, and the changing values of the variables n and i: