Why does printf() in the parent almost always win the race condition after fork()?
When fork
is executed, the process executing it (the new parent) is executing (of course), and the newly created child is not. For the child to run, either the parent must be stopped and the child given the processor, or the child must be started on another processor, which takes time. Meanwhile, the parent continues execution.
Unless some unrelated event occurs, such as the parent exhausting the time slice it was given for sharing the processor, it wins the race.