How does processes synchronization using signals work?

耗尽温柔 提交于 2019-11-30 23:58:43

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!