信号集 信号屏蔽字/pending的处理

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位图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值