I\'ve recently finished Section 10 (Signals) of \"Advanced Programming in the Unix Environment\" (3rd edition) and I\'ve come across a piece of code I don\'t entirely unders
1) They are not communicating through "variable" - the sole communication facility used here is kill
function. We "tell" things by invoking kill
, we "wait" to be told with sigsuspend
. sig_flag
is not shared, it's a local state of each process, and it says whether this particular process has been "told" by the other.
2) Were the signals not blocked prior to fork
, the parent process could send the signal to the child before the child has started waiting for it. That is, the timeline could be like that:
kill
But this signal has already been delivered, and so waits indefinitely. Therefore, we must ensure the signal is not delivered to the child process before it starts the waiting loop. To this end, we block it before fork
, and atomically unblock it and start waiting for it. Atomicity is the key; required invariant cannot be achieved with this operation performed as two independent steps, as the signal could be delivered inbetween.