引用:#include
void (*signal(int signo, void (*func)(int)) ) (int);
在FreeBSD下,signal安装指定信号的handler函数后,这个handler函数会一直存在,除非你再指定一个handler函数或者是SIG_DFL,SIG_IGN。而在Linux中,当接受到相应的信号调用handler函数处理完毕后,这个handler函数也就撤消了,下次再次接受该信号时会执行SIG_DFL动作(一般也就是结束程序)。
Linux的man page中的解释:
引用:The original Unix signal() would reset the handler to SIG_DFL, and
System V (and the Linux kernel 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 behaviour.
FreeBSD的man page中的解释:
引用:The handled signal is unblocked when the function returns and the process
continues from where it left off when the signal occurred. Unlike previ-
ous signal facilities, the handler func() remains installed after a sig-
nal has been delivered.
真是想不到BSD系统和Linux之间区别这么大,如果在这方面做移植的话需要多加小心。测试环境为FreeBSD 6.2 Release和Red Hat AS4,测试signal的代码:
引用:#include
#include
#include
void sig_int(int signo);
int main()
{
signal(SIGINT,sig_int);
for(int i=0; i<10; i++)
{
sleep(2);
printf("No: %d \n",i);
}
}
void sig_int(int signo)
{
printf("Caught SIGINT\n");
}