C:尝试使用 sigaction 恢复信号处理程序但未成功。

标签 c linux ubuntu operating-system

我正在用 C 编写一个小程序(在 ubuntu 上运行)。程序第一次收到 Ctrl+C,我希望信号被忽略,但第二次(等待 10 秒后),我希望恢复原来的 Action (并且信号不被忽略)。所以我有这样的东西:

void (*sold)(int);
struct sigaction s;
sold = s.sa_handler;
s.sa_handler = SIG_IGN;
sigaction(SIGINT,&s,NULL);
sleep(10)
s.sa_handler = sold; //(this could be replaced by s.sa_handler = *sold and it doenst make a difference)

该程序似乎很好地忽略了 SIGINT,但它并没有恢复......,因为它没有恢复旧的处理程序。我做错了什么?

最佳答案

如果你想恢复旧的信号处理程序,你需要实际保存并恢复旧的处理程序:

struct sigaction newHandler;
struct sigaction oldHandler;
memset(&newHandler, 0, sizeof(newHandler));
sigemptyset( &newHandler.sa_mask );
newHandler.sa_handler = SIG_IGN;
sigaction(SIGINT, &newHandler, &oldHandler );
sleep( 10 );
sigaction(SIGINT, &oldHandler, NULL );

关于C:尝试使用 sigaction 恢复信号处理程序但未成功。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30178017/

相关文章:

c - va_arg( , double) 每次返回一个无效值

linux - Git - 致命的 : Could not get current working directory?

c - Linux, C : Accumulate data from multiple threads

git - 如何让 GIT/SSH 在特定位置查找 "known_hosts"?

python - 在 Ubuntu 中找不到(但已安装)模块

c++ - 项目组织的最佳方式是什么?

python - 搜索 python 脚本比 C 等效脚本更快

linux - 使用bash脚本进行git克隆,无法读取存储库的密码

python - Ubuntu 11.10 上的 VPython 错误

c - Win32 滚动条 : When drag horizontal scroll bar to right most position, nPos 的值没有达到 nMaxPos