黑马《linux系统编程》学习笔记(从56到60)

五十六. setitimer定时器函数的使用

五十七. 阻塞信号集和未决信号集的关系

比如说我们要阻塞某些进程,先在自定义信号集中指定0或者1,再把自定义信号集,写进阻塞信号集。

五十八. 读当前进程的未决信号集

这里首先是signal_set.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <signal.h>


int main(int argc, const char* argv[])
{
        //每隔1s赌一次内存的未决信号集
        while(1)
        {
                sigset_t pendset;
                sigpending(&pendset);
                //1~31
                for(int i=1;i<32;i++)
                {
                        //对每一个信号一次判断
                        if(sigismember(&pendset,i))
                        {
                                printf("1");
                        }
                        else
                        {
                                printf("0");
                        }
                }
                printf("\n");
                sleep(1);
        }

    return 0;
}

运行一下这个程序

[root@VM_0_15_centos 7Day]# vim signal_set.c
[root@VM_0_15_centos 7Day]# ls
abort.c  alarm_uncle.c  kill.c   setitimer.c  sigmaks.c  signal_set.c
alarm.c  homework       raise.c  sigaction.c  signal.c   thread_attr.c
[root@VM_0_15_centos 7Day]# gcc signal_set.c -std=gnu99
[root@VM_0_15_centos 7Day]# ./a.out
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000
0000000000000000000000000000000

显然这里由于没有信号阻塞,所以未决信号集里,全部是0

五十九. 设置信号阻塞

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <signal.h>


int main(int argc, const char* argv[])
{
        //手动屏蔽信号
        //自定义信号集集合
        sigset_t myset;
        //清空集合
        sigemptyset(&myset);
        //添加要阻塞的信号
        sigaddset(&myset,SIGINT);
        sigaddset(&myset,SIGQUIT);
        sigaddset(&myset,SIGKILL);

        //自定义集合数据设置给内核的阻塞信号集
        sigprocmask(SIG_BLOCK, &myset, NULL);

        //每隔1s赌一次内存的未决信号集
        while(1)
        {
                sigset_t pendset;
                sigpending(&pendset);
                //1~31
                for(int i=1;i<32;i++)
                {
                        //对每一个信号一次判断
                        if(sigismember(&pendset,i))
                        {
                                printf("1");
                        }
                        else
                        {
                                printf("0");
                        }
                }
                printf("\n");
                sleep(1);
        }

    return 0;
}

 

 

六十. signal信号捕捉函数

 signal.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <signal.h>

void myfunc(int sig)
{
    printf("cathc you signal: %d\n", sig);
}

int main(int argc, const char* argv[])
{
    // 注册信号捕捉函数
    signal(SIGINT, myfunc);

    while(1)
    {
                sleep(3);
                printf("hello!\n");
    }
    return 0;
}

运行结果:

[root@VM_0_15_centos 7Day]# vim signal.c
[root@VM_0_15_centos 7Day]# gcc signal.c
[root@VM_0_15_centos 7Day]# ./a.out
hello!
hello!
^Ccathc you signal: 2
hello!
^Ccathc you signal: 2
hello!
^Ccathc you signal: 2
hello!
hello!
^Ccathc you signal: 2
hello!
^Ccathc you signal: 2
hello!
^Ccathc you signal: 2
hello!
^Ccathc you signal: 2
hello!
hello!
hello!

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值