打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux c 怎么屏蔽信号 使用sigprocmask命令
userphoto

2018.04.27

关注

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

NAME
       sigprocmask - examine and change blocked signals
功能  sigprocmask - 测试和改变被屏蔽的信:号
​SYNOPSIS
头文件    #include <signal.h>


​函数      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 <stdio.h>
  3 #include <signal.h>
  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)处理完毕后,继续返回内核,再次判断是否有信号到达。

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


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux进程间通信(二):信号集函数 sigemptyset()sigprocmask()
C语言中查询进程信号是否被遮罩或搁置的简单方法
Introduction To Unix Signals Programming
signal函数
Linux信号与信号处理
从kernel原始码的角度分析signal的错误用法和注意事项(zt)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服