陷入 sigwait 困境
我在代码中做错了,其他进程向它发送了 SIGUSR2 信号:
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGILL);
sigaddset(&sigset, SIGUSR2);
sigwait(&sigset, &received);
XCode 注意到收到了 SIGUSER2(31) 信号,但收到的 = SIGILL(4) (或集合中的最小信号)。
为什么会这样呢?我哪里错了?
现在,它看起来像这样:
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGILL);
sigaddset(&sigset, SIGUSR2);
sigprocmask(SIG_BLOCK, &sigset, 0);
sigwait(&sigset, &received);
if(received == SIGUSR2) {
//...
} else if(received == SIGILL) {
//...
}
仍然不起作用。
I did something wrong in my code, where an other process send a SIGUSR2 signal to it:
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGILL);
sigaddset(&sigset, SIGUSR2);
sigwait(&sigset, &received);
XCode notices SIGUSER2(31) signal received, but received = SIGILL(4) (or the minimal signal in the set).
Why is that so? Where I am wrong?
Now, it looks like this:
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGILL);
sigaddset(&sigset, SIGUSR2);
sigprocmask(SIG_BLOCK, &sigset, 0);
sigwait(&sigset, &received);
if(received == SIGUSR2) {
//...
} else if(received == SIGILL) {
//...
}
Still not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有时调试器可能会妨碍。我以前见过调试器干扰信号处理。尝试在不涉及调试器的情况下运行代码。
以下代码在 OS X 上完美运行:
Sometimes the debugger can get in the way. I have seen debuggers interfere with signal handling before. Try running the code without the debugger involved.
The following code works perfectly on OS X:
如相关问题sigwait in Linux (Fedora 13) vs OS X中所述,您需要使用
sigprocmask()
(对于单线程应用程序)或pthread_sigmask()
(对于多线程应用程序)来阻止信号。检查 sigwait 的返回值是否有错误也不错。
As stated in the related question sigwait in Linux (Fedora 13) vs OS X, you need to block the signal using
sigprocmask()
(for single-threaded applications) orpthread_sigmask()
(for multi-threaded applications).Checking
sigwait
's return value for errors would not be bad either.