加载中…
博文

    看到网上大部分解释都是抄来抄去的,结果说不清楚,最后还是不理解,于是花了几个小时研究了下,感觉大体上明白了。希望本文能对你有用处。

1)头文件:#include <signal.h>

2)一个保护临界区代码的错误实例:(sigprocmask()和pause()实现)

#include <unistd.h>

#include <signal.h>

#include <stdio.h>

 

void handler(int sig)    //信号处理函数的实现

{

   printf('SIGINT sig');

}

int main()

{

    sigset_t new,old;

    struct sigaction act;

    act.sa_handler = handler;  //信号处理函数handler

    sigemptyset(&act.sa_mask);

    act.sa_flags = 0;

    sigaction(SIGINT, &act, 0);  //准备捕捉SIGINT信号

    sigemptyset(&new);

    sigaddset(&new, SIGINT);

 

功能描述:设定对信号屏蔽集内的信号的处理方式(阻塞或不阻塞)。
用法:
#include <signal.h>
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
参数:
how:用于指定信号修改的方式,可能选择有三种:
SIG_BLOCK //加入信号到进程屏蔽。
SIG_UNBLOCK //从进程屏蔽里将信号删除。
SIG_SETMASK //将set的值设定为新的进程屏蔽。

set:为指向信号集的指针,在此专指新设的信号集,如果仅想读取现在的屏蔽值,可将其置为NULL。
oldset:也是指向信号集的指针,在此存放原来的信号集。
返回说明:
成功执行时,返回0。失败返回-1,errno被设为EINVAL。

#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.s

个人资料
西风_Wind
西风_Wind
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,138
  • 关注人气:34
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有