Interprocess synchronization using signals in c, linux

半腔热情 提交于 2019-12-06 00:28:53

If you use the standard signal() method, the process A can lose some signals if many children send him the same signal in a very short time. If you're handling a signal and receive during this time the same one, you will lose the second (more precisely it will be catched by the default signal handler).

If you decide to use sigaction() you'll be able to catch all the signals sent to your process.

Other answerers have already covered how to avoid losing signals - that said, I would recommend not using signals at all. Since signals can break in at any point (even while locks are held!) it's very hard to write signal-safe code more complex than, say, writing a flag into a fifo or incrementing an eventfd.

As such, why not just use a pipe or eventfd to report completion? This not only guarantees that all messages written will make it to the parent process, but also avoids synchronization issues by allowing your process to receive data from the pipe/eventfd only when it's ready.

Majid Azimi

You must use sigaction(); however again you can lose some of them because:

Signals are not queued. Child processes send SIGUSR2 and if they occur at the same time, only one of them will be delivered.

The best way is to use real time signals using sigqueue().

You will be use signal alrm and pause in parent process which wait till child process generate alarm signal which crack pause statement and sync them. .

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