简体   繁体   English

如何按C到达顺序处理多个信号

[英]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 finish handling it, I'll handle the next ones. 我希望能够处理许多相同类型的信号( SIGCHLD ),但是,我想确保如果信号在我仍在处理前一个信号时到达,我将完成处理第一个信号到达,并且只有在我完成处理之后,我才会处理下一个。

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? 此外,如果父进程终止或终止(使用SIGTERM / SIGKILL )进程,进程是否会发送SIGCHLD

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. 只要您使用sigaction而不是有问题的signal函数来设置信号处理程序,您可以确定(除非您另行指定)您的信号处理程序不会被其处理的另一个信号中断。 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. 在每个SIGCHLD ,正常的过程是尝试wait孩子,直到你的wait -family函数说没有孩子等待。 At this point, you can be sure that any further child termination will give you a new SIGCHLD . 此时,您可以确定任何进一步的子终止将为您提供新的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. 此外,由于您对信号处理程序可以使用的功能非常有限,您可能最好只设置某种标志或以其他方式通知主程序循环它应该检查已终止的子项wait接口。

And finally, yes, a SIGCHLD is delivered regardless of the reason the child terminated - including if it was killed by the parent. 最后,是的,无论孩子终止的原因是什么,都会提供SIGCHLD - 包括它是否被父母杀死。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM