[英]Signal system call
I have this code snippet and I even reading about the signal system call a few times, I still do not understand why the program stops the fourth time I press CTRL-C, and not the third. 我有这个代码片段,甚至我读过几次信号系统调用,但我仍然不明白为什么为什么我第四次按CTRL-C而不是第三次停止该程序。 Thanks in advance!
提前致谢!
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int i=0;
void handler(int sig)
{
i++;
printf("CTRL-C\n");
if (i==3)
signal(SIGINT, SIG_DFL);
}
int main()
{
signal(SIGINT,handler);
while (1)
{
printf("Hello world!\n");
sleep(1);
}
return 0;
}
I read that the signal system call is not portable, so it may help if I mention I am using the last version of Ubuntu (14.04). 我读到信号系统调用不是可移植的,所以如果我提到我正在使用最新版本的Ubuntu(14.04),这可能会有所帮助。
Your custom handler gets called three times. 您的自定义处理程序被调用了三次。 The third time, it registers a new signal handler (namely the default one), which terminates the program the next time the signal is delivered.
第三次,它注册一个新的信号处理程序(即默认信号处理程序),该处理程序在下一次发送信号时终止程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.