如有转载,请注明出处:Windeal专栏
首先简述下几个概念的关系:
#include <signal.h> int sigemptyset(sigset_t *set); //清空信号集 int sigfillset(sigset_t *set); //填满信号集 int sigaddset(sigset_t *set,int signo); //添加一个信号 int sigdelset(sigset_t *set,int signo); //删除信号集中的一个信号 All four return: 0 if OK,−1 on error int sigismember(const sigset_t *set,int signo); Returns: 1 if true, 0 if false,−1 on error
#define sigemptyset(ptr) (*(ptr) = 0) #define sigfillset(ptr) (*(ptr) = ˜(sigset_t)0, 0)
#include <signal.h> #include <errno.h> /* *<signal.h> usually defines NSIG to include signal number 0. */ #define SIGBAD(signo) ((signo) <= 0 || (signo) >= NSIG) int sigaddset(sigset_t *set, int signo) { if (SIGBAD(signo)) { errno = EINVAL; return(-1); } *set |= 1 << (signo - 1); /* turn bit on */ return(0); } int sigdelset(sigset_t *set, int signo) { if (SIGBAD(signo)) { errno = EINVAL; return(-1); } *set &= ˜(1 << (signo - 1)); /* turn bit off */ return(0); } int sigismember(const sigset_t *set, int signo) { if (SIGBAD(signo)) { errno = EINVAL; return(-1); } return((*set & (1 << (signo - 1))) != 0); }
#include <signal.h> int sigprocmask(int how,const sigset_t *restrict set,sigset_t *restrict oset); Returns: 0 if OK,−1 on error
#include <signal.h> int sigpending(sigset_t *set); Returns: 0 if OK,−1 on error
$./a.out ˆ\ generate signal once (before5seconds areup) SIGQUIT pending after return fromsleep caught SIGQUIT in signal handler SIGQUIT unblocked after return fromsigprocmask ˆ\Quit(coredump) generate signal again $./a.out ˆ\ˆ\ˆ\ˆ\ˆ\ˆ\ˆ\ˆ\ˆ\ˆ\ generate signal 10 times (before5seconds areup) SIGQUIT pending caught SIGQUIT signal is generated only once SIGQUIT unblocked ˆ\Quit(coredump) generate signal again
#include <signal.h> int sigsuspend(const sigset_t *sigmask); Returns:−1witherrnoset toEINTR
#include <signal.h>
int sigsuspend(const sigset_t *sigmask);
Returns:−1witherrnoset toEINTR