how to handle multiple signals by order of arrival in C
I want to be able to handle many signals of the same type (SIGCHLD
), but, I want to make sure that if a signal is arriving while I'm still handling the previous one, I will finish handling the first to arrive, and only after I 开发者_JAVA百科finish handling it, I'll handle the next ones.
There may be more than one signals waiting to be handled.
Also, does a process sends SIGCHLD
if it's terminated or killed (using SIGTERM
/SIGKILL
) by the parent process?
As long as you use sigaction
and not the problematic signal
function to setup your signal handler, you can be sure (unless you specify otherwise) that your signal handler will not be interrupted by another occurrence of the signal it's handling. However it's possible if many child processes all die at once that you might not receive a signal for each. On each SIGCHLD
, the normal procedure is to attempt to wait
for children until your wait
-family function says there are no children left to wait for. At this point, you can be sure that any further child termination will give you a new SIGCHLD
.
Also, since you're very restricted as to what functions you can use from a signal handler, you'd probably be better off just setting some sort of flag or otherwise notifying your main program loop that it should check for terminated children via one of the wait
interfaces.
And finally, yes, a SIGCHLD
is delivered regardless of the reason the child terminated - including if it was killed by the parent.
精彩评论