//当调用一个信号处理程序时,被捕捉的信号自动地加到进程的当前信号屏蔽字中,阻止后来产生的这种信号中断改信号处理程序。
//当从信号处理程序返回时,自动恢复原来的屏蔽字
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
sigset_t set;
void print(int signo)
{
printf("handle SIGINT in print()!!!\n");
}
void catch(int signo)
{
printf("\nin catch()...\n");
signal(SIGINT, print);
sleep(5);
sigpending(&set);
if(sigismember(&set, SIGINT))
printf("block SIGINT!!!\n");
printf("out catch()...\n");
}
int main()
{
signal(SIGINT, catch);
while(1)
{
printf("in main()...\n");
sleep(1);
}
}
/*
coolulu@ubuntu:~/code/syscall$ ./signal1
in main()...
in main()...
^C
in catch()...
^C^C^Cblock SIGINT!!!
out catch()...
handle SIGINT in print()!!! <-如apue所说,解除了该信号的阻塞后,只会向进程传送一次该信号
in main()...
in main()...
in main()...
^Chandle SIGINT in print()!!!
in main()...
^Chandle SIGINT in print()!!!
in main()...
^Chandle SIGINT in print()!!!
in main()...
^\Quit
coolulu@ubuntu:~/code/syscall$
*/
//这样说明了在 信号处理函数catch中^C被block,
//在信号处理函数catch中的 signal(SIGINT, print)永远收不到^C
转载于:https://www.cnblogs.com/coolulu/archive/2012/09/05/4084910.html