黑马《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!

猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/85346518