简体   繁体   English

我可以检查待处理的信号并立即执行它们

[英]Can I check for pending signals and execute them immediately

What I want to do is to only handle signals at some known points in my code. 我想要做的是只处理代码中某些已知点的信号。 So at those points I will check for a pending signal and execute its handler immediately. 因此,在这些点上,我将检查待处理的信号并立即执行其处理程序。

I know I can use sigprocmask to block a signal and later unblock it, but sill in that way, the signal will be delivered at any point in time after unblocking, which I don't want. 我知道我可以使用sigprocmask阻塞信号然后解锁它,但是这样,信号将在解锁后的任何时间点传递,这是我不想要的。 I want to execute the signal handler precisely at a specific location in my code. 我想在我的代码中的特定位置精确地执行信号处理程序。 In other words, I want to make signal handling synchronous. 换句话说,我想让信号处理同步。

Is there a possible way to achieve what I'm trying to do? 有没有办法实现我想做的事情?

You can fetch any pending blocked signal using the sigwait family of syscalls. 您可以使用sigwait系列的系统调用来获取任何挂起的阻塞信号。 It's not possible to wait for both signals and other types of events concurrently though, if you do not want to block in sigwait you can first check if there's pending signals using sigpending . 不可能同时等待信号和其他类型的事件,如果你不想在sigwait中阻塞,你可以先使用sigpending检查是否有待处理的信号。

If you're using an event driven design for your application you'll typically have several fds in a run loop. 如果您正在为应用程序使用事件驱动设计,那么在运行循环中通常会有几个fds。 For that situation you should instead use a signal handler which writes to a pipe . 对于这种情况,您应该使用写入pipe的信号处理程序。 The signal will be delivered asynchronously but will then appear in your run loop as just another fd that needs processing. 信号将以异步方式传送,但随后会出现在您的运行循环中,只是需要处理的另一个fd。

If I understand you correctly you can simply use sigwait to clear each of the pending signals and then call the your signal handler function directly. 如果我理解正确,您只需使用sigwait清除每个待处理信号,然后直接调用您的信号处理函数。

At the points in your code where you want to handle and clear pending signals you can use sigpending to check if there are signals waiting and then repeatedly call sigwait to clear the pending signals, manually calling a signal handling function to perform the required action. 在代码中要处理和清除待处理信号的位置,您可以使用sigpending检查是否有信号等待,然后重复调用sigwait以清除待处理信号,手动调用信号处理函数以执行所需操作。

Any pending signals will be delivered immediately when you unmask them. 当您取消屏蔽它们时,将立即传递任何待处理信号。 Just unmask/remask at the point where you want to let them be delivered. 只需在要传递它们的位置取消屏蔽/重新屏蔽即可。 If you are using threads be aware that asynchronous signals (like those you get from kill ) can go to any thread that has them unblocked. 如果您正在使用线程,请注意异步信号(如您从kill获得的信号)可以转到任何已解除阻塞的线程。

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

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