1.相关函数
NAME
sigemptyset, sigfillset, sigaddset, sigdelset, sigismember -
POSIX signal set opera‐tions.
SYNOPSIS
#include <signal.h>
int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set, int signum);
int sigdelset(sigset_t *set, int signum);
int sigismember(const sigset_t *set, int signum);
DESCRIPTION
These functions allow the manipulation of POSIX signal sets.
sigemptyset() initializes the signal set given by set to
empty, with all signals excluded from the set.
sigfillset() initializes set to full, including all signals.
sigaddset() and sigdelset() add and delete respectively
signal signum from set.
sigismember() tests whether signum is a member of set.
①所有的应用程序在使用信号集之前,都必须先调用sigemptyset/sigfillset一次
②一旦已经初始化了一个信号集,那么就可以对该信号集进行添加和删除信号的操作
2.sigprocmask
NAME
sigprocmask - examine and change blocked signals
SYNOPSIS
#include <signal.h>
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
The behavior of the call is dependent on the value of how, as follows.
SIG_BLOCK
The set of blocked signals is the union of the current set and
the set argu‐ment.
SIG_UNBLOCK
The signals in set are removed from the current set of blocked
signals. It is permissible to attempt to unblock a signal
which is not blocked.
SIG_SETMASK
The set of blocked signals is set to the argument set.
If oldset is non-NULL, the previous value of the signal mask is
stored in oldset.
If set is NULL, then the signal mask is unchanged
(i.e., how is ignored), but the current value of the signal mask
is nevertheless returned in oldset (if it is not NULL).
3.一个demo
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void Handler(int n)
{
write(1, "!", 1);
}
int main()
{
int i;
sigset_t set;//信号集
//都必须先进行置空操作
sigemptyset(&set);
//加入INT这个信号
sigaddset(&set, SIGINT);
//设置该信号的行为
signal(SIGINT, Handler);
while(1)
{
//将该信号集中的信号进行阻塞
sigprocmask(SIG_BLOCK, &set, NULL);
for(i = 0; i < 5; i++)
{
write(1, "*", 1);
sleep(1);
}
write(1, "\n", 1);
//不阻塞信号集中的信号
sigprocmask(SIG_UNBLOCK, &set, NULL);
}
return 0;
}
多个信号为什么会丢失,前面我已经解释过了,简单来说他就表现在位图上
4.sigsetjmp sigsetlongjmp
int sigsetjmp(sigjmp_buf env, int savesigs);
sigsetjmp() is similar to setjmp(). If, and only if,
savesigs is nonzero, the process's current signal mask is saved
in env and will be restored if a siglongjmp(3) is later performed
with this env.
它可以保存你的mask位图
void siglongjmp(sigjmp_buf env, int val);
POSIX does not specify whether longjmp() will restore the
signal context (see setjmp(3) for some more details).
If you want to portably save and restore signal masks,
use sigsetjmp(3) and siglongjmp().
跳转同时恢复你得mask位图