[Linux]关于sigprocmask函数的讨论
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <unistd.h> 5 #include <signal.h> 6 #include <assert.h> 7 8 void printSignal() 9 { 10 sigset_t set; 11 int ret = 0; 12 sigemptyset(&set); 13 ret = sigpending(&set); 14 int i = 1; 15 for (i = 1; i < 65; i++) 16 { 17 if (0 == (ret = sigismember(&set, i))) 18 { 19 printf("0"); 20 } 21 else if (1 == ret) 22 { 23 printf("1"); 24 } 25 } 26 puts(""); 27 } 28 29 void my_sigaction (int num, siginfo_t *st, void *dat) 30 { 31 int ret = 0; 32 if (SIGINT == num) 33 { 34 sigset_t set; 35 sigemptyset(&set); 36 ret = sigaddset(&set, SIGQUIT); 37 assert(-1 != ret); 38 ret = sigprocmask(SIG_UNBLOCK, &set, NULL); 39 assert(-1 != ret); 40 printf("my_sigaction\n"); 41 printSignal(); 42 printf("func myhandler num = %d\n", num); 43 } 44 else 45 { 46 printf("signal is no SIGQUIT\n"); 47 } 48 } 49 50 51 52 int main(void) 53 { 54 int ret = 0; 55 56 /* 阻塞信号 */ 57 sigset_t set; 58 sigemptyset(&set); 59 ret = sigaddset(&set, SIGQUIT); 60 assert(-1 != ret); 61 ret = sigprocmask(SIG_BLOCK, &set, NULL); 62 assert(-1 != ret); 63 printf("阻塞信号\n"); 64 //printSignal(); 65 sigemptyset(&set); 66 /* 67 sigpending 是从未决状态字中读取的, 但是未决信号并不能立即得到block的同步 68 也就是说在block置1时pending并不能够立即的更新为1 69 */ 70 //sigpending(&set); 71 72 /* 注册信号 */ 73 struct sigaction act; 74 act.sa_sigaction = my_sigaction; 75 ret = sigemptyset(&(act.sa_mask)); 76 assert(-1 != ret); 77 ret = sigaction(SIGINT, &act, NULL); 78 assert(-1 != ret); 79 printf("注册信号\n"); 80 printSignal(); 81 82 while (1) 83 { 84 printSignal(); 85 sleep(1); 86 } 87 return 0; 88 }