一 点睛
sigprocmask:查询或设置信号屏蔽字
表头文件
#include<signal.h>
定义函数
int sigprocmask(int how,const sigset_t *set,sigset_t *oldset);
函数说明
sigprocmask()可以用来改变目前的信号屏蔽字,其操作依参数how来决定
SIG_BLOCK:表示加入信号到进程屏蔽
SIG_UNBLOCK:表示从进程屏蔽里将信号删除。
SIG_SETMASK:表示将set的值设置为新的进程屏蔽。
如果参数oldset不是NULL指针,那么目前的信号屏蔽会由此指针返回。
返回值
执行成功则返回0,如果有错误则返回-1。
错误代码
EFAULT:参数set,oldset指针地址无法存取。
EINTR: 此调用被中断
二 实战
1 代码
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void handler(int sig)
{
printf("Deal SIGINT"); //SIGINT信号处理函数
}
int main()
{
sigset_t newmask;
sigset_t oldmask;
sigset_t pendmask;
struct sigaction act;
act.sa_handler = handler; //handler为信号处理函数首地址
sigemptyset(&act.sa_mask); //用来将参数set信号集初始化并清空
act.sa_flags = 0;
sigaction(SIGINT, &act, 0); //信号捕捉函数,捕捉CTRL+C
sigemptyset(&newmask); //初始化信号量集
sigaddset(&newmask, SIGINT); //将SIGINT添加到信号量集中
sigprocmask(SIG_BLOCK, &newmask, &oldmask); //将newmask中的SIGINT阻塞掉,并保存当前信号屏蔽字到oldmask
//休眠5秒钟,说明:在5秒休眠期间,任何SIGINT信号都会被阻塞,如果在5秒内收到任何键盘的CTRL+C信号,
//则此时会把这些信息存在内核队列中,等待5秒结束后,可能要处理此信号
sleep(5);
sigpending(&pendmask); //检查信号是悬而未决
// 判断信号SIGINT是否悬而未决。所谓悬而未决,是指SIGINT被阻塞还没被处理
if (sigismember(&pendmask, SIGINT))
printf(" SIGINT pending\n");
sigprocmask(SIG_SETMASK, &oldmask, NULL);//恢复被屏蔽的信号SIGINT
// 此处开始可以处理信号
printf("SIGINT unblocked\n");
sleep(5); // 这个时间段内,如果按下CTRL+C,则会调用函数handler
return (0);
}
2 运行
[root@localhost test]# g++ -o test test.cpp
[root@localhost test]# ./test
# 在开始5秒内,按下一次CTRL+C,则不会有反应,因为这个信号被我们屏蔽了,
# 5秒后,悬而未决的SIGINT信号从队列取出进行处理
[root@localhost test]# ./test
^C SIGINT pending
Deal SIGINTSIGINT unblocked
[root@localhost test]# ./test
# 5秒后按CTRL+C的结果
SIGINT unblocked
^CDeal SIGINT
[root@localhost test]# ./test
# 前5秒和后5秒都按CTRL+C的结果
^C^C^C^C^C SIGINT pending
Deal SIGINTSIGINT unblocked
^CDeal SIGINT