zoukankan      html  css  js  c++  java
  • 实时信号和非实时信号

    #include <assert.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>
    
    void myHandler(int num)
    {
        int ret = 0;
        
        if (SIGUSR1 == num)
        {
            sigset_t set;
            ret = sigemptyset(&set);
            assert(!(-1 == ret));
            ret = sigaddset(&set, SIGINT);
            assert(!(-1 == ret));
            ret = sigaddset(&set, SIGRTMIN);
            assert(!(-1 == ret));
            ret = sigprocmask(SIG_UNBLOCK, &set, NULL);
            assert(!(-1 == ret));
            printf("解除阻塞 recv sig num: %d
    ", num);
        }
        else if (num == SIGINT || num == SIGRTMIN)
        {
            printf("recv sig num: %d
    ", num);
        }
        else
        {
            printf(" 其他信号recv sig num: %d
    ", num);
        }
    }
    
    int main(void)
    {
        pid_t pid;
        int ret = 0;
        // 设置回调函数
        struct sigaction act;
        act.sa_handler = myHandler;
        act.sa_flags = SA_SIGINFO;
        // 注册非实时信号的处理函数
        ret = sigaction(SIGINT, &act, NULL);
        assert(!(-1 == ret));
        // 注册实时信号的处理函数
        ret = sigaction(SIGRTMIN, &act, NULL);
        assert(!(-1 == ret));
        // 注册用户自定义信号
        ret = sigaction(SIGUSR1, &act, NULL);
        assert(!(-1 == ret));
        
        // 把 SIGINT  SIGRTMIN 军添加到阻塞状态字中
        sigset_t set;
        ret = sigemptyset(&set);
        assert(!(-1 == ret));
        ret = sigaddset(&set, SIGINT);
        assert(!(-1 == ret));
        ret = sigaddset(&set, SIGRTMIN);
        assert(!(-1 == ret));
        ret = sigprocmask(SIG_BLOCK, &set, NULL);
        assert(!(-1 == ret));
        
        pid = fork();
        assert(!(-1 == ret));
        if (0 == pid)
        {
            union sigval value;
            value.sival_int = 10;
            int i = 0;
            // 发三次不稳定信号
            for (i = 0; i < 3; i++)
            {
                ret = sigqueue(getppid(), SIGINT, value);
                assert(!(-1 == ret));
                printf("发送不可靠信号 ok
    ");
            }
            
            // 发三次稳定信号
            value.sival_int = 20;
            for (i = 0; i < 3; i++)
            {
                ret = sigqueue(getppid(), SIGRTMIN, value);
                assert(!(-1 == ret));
                printf("发送可靠信号ok
    ");
            }
            // 向父进程发送 SIGUSR1 解除阻塞
            ret = kill(getppid(), SIGUSR1);
            assert(!(-1 == ret));
        }
        while (1)
        {
            sleep(1);
        }
        return 0;
    }
  • 相关阅读:
    杭电2054
    M1/M2总结
    软件开发相关的读书笔记 问题与方法
    代码复审1234跟我一起来
    电梯调度算法简述
    Pair_Work Project
    读书笔记摘录:
    Individual Project
    个人阅读作业——软件工程M1/M2的总结
    个人阅读作业2
  • 原文地址:https://www.cnblogs.com/bangiao/p/6498715.html
Copyright © 2011-2022 走看看