10_15.c先阻塞SIGQUIT信號,再釋放。

當在阻塞階段產生多個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:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章