当在阻塞阶段产生多个SIGQUIT信号时,当释放阻塞后也只catch到一个SIGQUIT信号。
还原信号阻塞时最好用SIG_SETMASK。
一、源代码:
vim 10_15.c
1 #include "apue.h"
2
3 static void sig_quit(int);
4
5 int main()
6 {
7 sigset_t newset,oldset,maskset;
8
9 if (signal(SIGQUIT,sig_quit) == SIG_ERR)
10 err_sys("Establish SIGQUIT handler failed");
11 sigemptyset(&newset);
12 sigaddset(&newset,SIGQUIT);
13 if (sigprocmask(SIG_BLOCK,&newset,&oldset) < 0)
14 err_sys("SIGQUIT block error");
15
16 sleep(5);
17
18
19 if (sigpending(&maskset) < 0)
20 err_sys("get pending signal error");
21
22 if (sigismember(&maskset,SIGQUIT))
23 printf("\nSIGQUIT pending\n");
24
25 if (sigprocmask(SIG_SETMASK,&oldset,NULL) < 0)
26 err_sys("SIG_SETMASK error");
27
28 printf("SIGQUIT unblocked");
29
30 sleep(5);
31 exit(0);
32 }
33
34
35
36
37 static void sig_quit(int signo)
38 {
39 printf("caught SIGQUIT\n");
40 if (signal(SIGQUIT,SIG_DFL) == SIG_ERR)
41 err_sys("can't reset SIGQUIT");
42 }
~
"10_15.c" 42L, 755C written
二、编译及运行结果:
gcc -Wall -ggdb3 10_15.c -o block_quit
In file included from apue.h:132,
from 10_15.c:1:
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'
./block_quit
^\
SIGQUIT pending
caught SIGQUIT
SIGQUIT unblocked
./block_quit
^\^\^\^\^\^\
SIGQUIT pending
caught SIGQUIT
SIGQUIT unblocked
./block_quit
^\
SIGQUIT pending
caught SIGQUIT
^\Quit(coredump)
<bldc:/home/tingbinz/apue.3e/SBSCODE/10>R*_*G: