Let's look at this line by line. Set up the signal handler, then the plug. The child types "Ghost" and exits. The parent forces the child to type "Phantom" and exit. Then the parent types βninjaβ.
So, you really have a race condition. If the parent launches his SIGUSR1 before the child prints "Ghost", you will get Phantom Ninja or, possibly, Ninja Phantom (kills the block?)
But, if you cannot turn off the signal in time, you will receive the Ghost Ninja when the child finishes before the parent signals. I do not think the opposite is possible.
Now we can assume that the signal may be just in time to get between printf and exit , in which case Ghost will end, then Phantom , then Ninja - or vice versa again, I think.
It is really thin and sensitive to OS synchronization.
@ Total - not verified! Feel free to contradict me, but I will be interested to know why as an OP.
source share