#include <errno.h> 2 #include <signal.h> 3 #include "../../ourhdr.h" 4 5 void pr_mask (const char *str) 6 { 7 sigset_t sigset; 8 int errno_save; 9 10 errno_save = errno; 11 /*we can be called by signal handlers*/ 12 13 if (sigprocmask (0, NULL, &sigset) < 0) 14 err_sys ("sigprocmask error"); 15 16 printf ("%s/n",str); 17 if (sigismember (&sigset, SIGINT)) 18 printf ("SIGINT/n"); 19 if (sigismember (&sigset, SIGQUIT)) 20 printf ("SIGQUIT/n"); 21 if (sigismember (&sigset, SIGUSR1)) 22 printf ("SIGUSR1/n"); 23 if (sigismember (&sigset, SIGALRM)) 24 printf ("SIGALRM/n"); 25 26 /*remianing signals can go here*/ 27 28 errno = errno_save;
29 }
1 #include <signal.h> 2 #include "../../ourhdr.h" 3 4 static void sig_int (int); 5 6 int main() 7 { 8 sigset_t newmask, oldmask, zeromask; 9 10 if (signal(SIGINT, sig_int) == SIG_ERR) 11 err_sys("signal (SIGINT) error"); 12 sigemptyset(&zeromask); 13 14 sigemptyset(&newmask); 15 16 sigaddset (&newmask, SIGINT); 17 /*block SIGINT and save current signal mask*/ 18 19 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) 20 err_sys("SIG_BLOCK error"); 21 22 /*critocal region of code */ 23 pr_mask("in critical region :"); 24 sleep(4); 25 26 27 if (sigsuspend(&oldmask) != -1) 28 err_sys("sigsuspend error"); 29 pr_mask("after return from sigsuspend"); 30 31 32 if (sigprocmask (SIG_SETMASK, &oldmask, NULL) < 0) 33 err_sys("SIG_SETMASK error"); 34 pr_mask("oldmask in proccess"); 35 36 exit(0); 37 } 38 39 40 static void sig_int(int signo) 41 { 42 pr_mask("/nin sig_int:"); 43 return; 44 }
sigsuspend 基本功能:
进程的信号屏蔽子设置为参数指向的值,在捕捉到一个信号或发生一个会终止该进程的信号之前,该进程被挂起 ,如果捕捉到一个信号而且从该信号处理程序返回,则SIGSUSPEND返回,并且该进程的信号屏蔽子设置为调用SIGSUSPEND之前的值,
注意,此函数没有成功返回值,如果它返回到调用者,则总是返回-1 ,
这个程序这行结果
in critical region : SIGINT ^C in sig_int: SIGINT after return from sigsuspend SIGINT oldmask in proccess