不可靠信号是不支持排队的,一直以为信号处理函数没有结束之前,就收不到这个信号.
现在
在执行信号处理函数中,操作系统会把当前信号加入信号屏蔽字里面.等信号处理函数之后,然后重新设置信号屏蔽字.所以说,信号还是可以接受到的.
#include <signal.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> void showMask(char*); void onSigProcess(int signo) { printf("receive signo:%d/n",signo); showMask("onSigProcess"); sleep(10); printf("receive signo:%d/n",signo); } void showMask(char *str) { sigset_t sigset; sigprocmask(SIG_BLOCK, NULL, &sigset); printf("%s, SIGINT IN MASK,RESULT:%d/n",str,sigismember(&sigset,SIGINT)); printf("%s, SIGUSR1 IN MASK,RESULT:%d/n",str,sigismember(&sigset,SIGUSR1)); } int main() { showMask("begin"); printf("begin,pid:%d/n",getpid()); signal(SIGINT,onSigProcess); while(1){ sleep(10); showMask("sleep over"); } }
运行结果如下:
begin, SIGINT IN MASK,RESULT:0 begin, SIGUSR1 IN MASK,RESULT:0 begin,pid:13529 ^Creceive signo:2 onSigProcess, SIGINT IN MASK,RESULT:1 onSigProcess, SIGUSR1 IN MASK,RESULT:0 ^C^C^C^Creceive signo:2 receive signo:2 onSigProcess, SIGINT IN MASK,RESULT:1 onSigProcess, SIGUSR1 IN MASK,RESULT:0 receive signo:2 sleep over, SIGINT IN MASK,RESULT:0 sleep over, SIGUSR1 IN MASK,RESULT:0