使用函数sigprocmask()可以设置,和显示当前进程的signal mask。
int sigprocmask(int how , const sigset_t *restrict set , sigset_t *restrict oset );
其中的how可以有三中选择。
SIG_BLOCK | The new signal mask for the process is the union of its current signal mask and the signal set pointed to by set . That is, set contains the additional signals that we want to block. |
SIG_UNBLOCK | The new signal mask for the process is the intersection of its current signal mask and the complement of the signal set pointed to by set . That is, set contains the signals that we want to unblock. |
SIG_SETMASK | The new signal mask for the process is replaced by the value of the signal set pointed to by set . |
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#define BUFFSIZE 4096
void
pr_mask(const char *str)
{
sigset_t sigset;
int errno_save;
errno_save = errno; /* we can be called by signal handlers */
if (sigprocmask(0, NULL, &sigset) < 0)
{
perror("sigprocmask error");
return;
}
printf("%s", str);
if (sigismember(&sigset, SIGINT)) printf("SIGINT ");
if (sigismember(&sigset, SIGQUIT)) printf("SIGQUIT ");
if (sigismember(&sigset, SIGUSR1)) printf("SIGUSR1 ");
if (sigismember(&sigset, SIGALRM)) printf("SIGALRM ");
/* remaining signals can go here */
printf("/n");
errno = errno_save;
}
int
main(void)
{
int n;
char buf[BUFFSIZE];
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGQUIT);
sigaddset(&sigset, SIGUSR1);
sigprocmask(SIG_BLOCK, &sigset, NULL) ;
pr_mask("main: ");
exit(0);
}