linux下如何屏蔽代码,linux c 怎么屏蔽信号 使用sigprocmask命令

SIGPROCMASK(2)             Linux Programmer's Manual            SIGPROCMASK(2)

NAME

sigprocmask - examine and change blocked signals

功能  sigprocmask - 测试和改变被屏蔽的信:号

​SYNOPSIS

头文件    #include

​函数      int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

参数:

​how:

​       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 argument.

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.

​*set:

​       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).

​*oldset:

If  oldset is non-NULL, the previous value of the signal mask is stored

in oldset.

​返回值:成功返回0,失败返回-1

​  1 //屏蔽2号信号

2 #include

3 #include

4

5 int main(void){

6     sigset_t set,oset;

7     //将集合清空

8     sigemptyset(&set);

9     //将2信号添加到集合set里

10     sigaddset(&set,SIGINT);

11     sigprocmask(SIG_SETMASK,&set,&oset);

12 //    sigprocmask(SIG_BLOCK,&set,&oset);

13     while(1);

14     return 0;

15 }

​前台与后台之间转换过程

1)在shell下,启动一个前台进程。

2)用户按下crtl+c,这个键盘输入产生一个硬件中断

3)如果此时cpu正在运行进程的用户空间代码,

需要切换到内核空间,处理硬件中断。

4)中断驱动程序将crtl+c 解释成一个sigint信号,记

在该进程的PCB中。

5)从内核返回到用户空间的时候,先检查

进程的PCB中是否有信号到达。如果有信号到达,

调用信号处理函数。

6)处理完毕后,继续返回内核,再次判断是否有信号到达。

如有切换到第五步,没有,返回进程的用户态,继续执行。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值