How does processes synchronization using signals work?

前端 未结 1 467
一个人的身影
一个人的身影 2021-01-06 12:28

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条回答
  • 2021-01-06 13:07

    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:

    • fork
    • parent gets the time slice, sends signal to the child with kill
    • child gets the time slice, and waits for the signal

    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.

    0 讨论(0)
提交回复
热议问题