【发布时间】:2014-06-09 23:54:19
【问题描述】:
我有一个奇怪的行为,联机帮助页和谷歌没有提供帮助。
在我的代码中,我想在发送 SIGUSR2 时阻止/取消阻止 SIGINT。为此,我安装了信号处理程序并在函数中准备了掩码集:
void installSignallHandler(){
sBlock.sa_handler = handleBlock;
sigemptyset(&sBlock.sa_mask);
sigaddset(&sBlock.sa_mask, SIGUSR2);
sBlock.sa_flags = 0;
sigaction(SIGUSR2, &sBlock, NULL);
sigemptyset(&signals_protected);
sigaddset(&signals_protected, SIGINT);
// For some reason sigprocmask only works if it is called here the first time
// sigprocmask(SIG_BLOCK, &signals_protected, NULL);
// sigintBlocked = true;
}
如果发送了 SIGUSR2,则调用此函数:
void handleBlock(int signal){
if(sigintBlocked){
printf("Unblocking SIGINT ...\n");
sigprocmask(SIG_UNBLOCK, &signals_protected, NULL);
sigintBlocked = false;
}
else{
printf("Blocking SIGINT ...\n");
sigprocmask(SIG_BLOCK, &signals_protected, NULL);
sigintBlocked = true;
}
}
为了测试,我这样称呼它:
int main(int argc, char **argv) {
installSignallHandler();
while(1){
printf("processing...\n");
sleep(1);
}
return EXIT_SUCCESS;
}
现在的问题:我发布代码的方式,sigprocmask 无效。但是,如果我取消注释上面的两行,它就可以工作。所以我的两个问题:
- 您能解释一下这种行为吗?
- 我可以做些什么来解决它? - 我不想从信号屏蔽开始。
【问题讨论】:
标签: linux linux-kernel signals sigprocmask