【发布时间】:2014-06-27 07:11:45
【问题描述】:
在处理信号时,我发现处理程序中的语句没有打印出来。这是我使用的代码:
#include"stdio.h"
#include"signal.h"
#include"unistd.h"
void handlerSIGINT(int sig)
{
if(sig == SIGINT)
printf("\nFinally caught SIGINT...");
}
int main()
{
printf("Hello ... soon u'll receive a signal");
if(signal(SIGINT, handlerSIGINT) == SIG_ERR)
{
printf("error in SIGINT handling");
}
while(1)
sleep(1);
/*now press control + C to see the effect */
return 0;
}
我在运行程序时得到以下输出:
[root@node1 mytest]# ./a.out
^CHello ... soon u'll receive a signal
^CFinally caught SIGINT...
^CFinally caught SIGINT...
^CFinally caught SIGINT...
^Z
[1]+ Stopped ./a.out
我的困惑是:当我第一次按下“Ctrl+C”时,它没有在处理程序中打印消息,即“终于捕获到 SIGINT...”。但是从第二次开始它开始打印消息。谁能解释一下为什么会这样……
【问题讨论】: