当前位置: 技术问答>linux和unix
关于不可靠信号的问题,照抄书上的程序,结果却和它说得不一样
来源: 互联网 发布时间:2016-04-18
本文导语: 抄书上的代码 #include void sigact(int sig) { printf("kb interrupt signum is %dn",sig); sleep(1); printf("quitn"); } int main() { signal(SIGINT,sigact); while(getchar()!='q') return 0; } 这是不可靠信号方式,signal不是永久安装的,应该第一...
抄书上的代码
这是不可靠信号方式,signal不是永久安装的,应该第一次ctrl+c的时候,执行sigact函数的内容,第二次就会退出程序了。
可是在我这里特别顽强,按了六七次ctrl+c一直在执行sigact函数的内容。很奇怪,为什么不像不可靠信号的表现呢?应该捕获一次就按照默认动作执行了啊
请大虾解惑,谢谢
#include
void sigact(int sig)
{
printf("kb interrupt signum is %dn",sig);
sleep(1);
printf("quitn");
}
int main()
{
signal(SIGINT,sigact);
while(getchar()!='q')
return 0;
}
这是不可靠信号方式,signal不是永久安装的,应该第一次ctrl+c的时候,执行sigact函数的内容,第二次就会退出程序了。
可是在我这里特别顽强,按了六七次ctrl+c一直在执行sigact函数的内容。很奇怪,为什么不像不可靠信号的表现呢?应该捕获一次就按照默认动作执行了啊
请大虾解惑,谢谢
|
咋一个问题发了三次啊?
|
glibc2 对singal()采用的是BSD 的处理方式:不会调用一次就reset到默认行为;
具体你可以: man signal,然后主要看Portability一节:
Portability
The original Unix signal() would reset the handler to SIG_DFL, and System V (and the Linux ker-
nel and libc4,5) does the same. On the other hand, BSD does not reset the handler, but blocks
new instances of this signal from occurring during a call of the handler. The glibc2 library
follows the BSD behavior.
If one on a libc5 system includes instead of then signal() is rede-
fined as __bsd_signal() and signal() has the BSD semantics. This is not recommended.
If one on a glibc2 system defines a feature test macro such as _XOPEN_SOURCE or uses a separate
sysv_signal(3) function, one obtains classical behavior. This is not recommended.
good luck!
具体你可以: man signal,然后主要看Portability一节:
Portability
The original Unix signal() would reset the handler to SIG_DFL, and System V (and the Linux ker-
nel and libc4,5) does the same. On the other hand, BSD does not reset the handler, but blocks
new instances of this signal from occurring during a call of the handler. The glibc2 library
follows the BSD behavior.
If one on a libc5 system includes instead of then signal() is rede-
fined as __bsd_signal() and signal() has the BSD semantics. This is not recommended.
If one on a glibc2 system defines a feature test macro such as _XOPEN_SOURCE or uses a separate
sysv_signal(3) function, one obtains classical behavior. This is not recommended.
good luck!