zoukankan      html  css  js  c++  java
  • 信号的发送与处理

    信号不仅可以用来处理异步事件,也可以用来传递数据。

    利用函数sigaction跟sigqueue来实现进程间的数据传递。

    程序1:发送数据

    #include <stdio.h>
    #include <sys/types.h>
    #include <signal.h>
    
    
    
    
    int main(int argc, char* argv[])
    {
        union sigval send_val;
        send_val.sival_int = 1;
        
        //get the pid
        pid_t pid = atoi(argv[1]);
        //利用sigqueue给pid发送信号SIGINT,并携带数据value
        sigqueue(pid, SIGINT, send_val);
    
    
        return 0;
    }

    程序2:接收数据

    #include <stdio.h>
    #include <sys/types.h>
    #include <signal.h>
    
    //3参数的信号处理函数
    void hand_singno(int signo, siginfo_t *siginfo, void *pvoid)
    {
        printf("recv SIGINT,the data is:%d
    ", siginfo->si_int);
    }
    
    
    int main(int argc, char* argv[])
    {
    
        struct sigaction act;
        act.sa_sigaction = hand_singno;
        act.sa_flags = SA_SIGINFO;//指定使用3参数的信号处理函数
        
        //安装信号处理函数
        sigaction(SIGINT, &act, NULL);
    
    
        while(1);
    
        return 0;
    }

    整型数据从进程1传递给了进程2

  • 相关阅读:
    npm 安装Vue环境时报错
    WinSCP与SecureCRT
    LeetCode100---Same Tree
    LeetCode404---Sum of Left Leaves
    LeetCode283---Move Zeroes
    LeetCode344---Reverse String
    Java多线程一
    Java知识点总结
    Java泛型
    深入浅出设计模式学习笔记四:单例模式
  • 原文地址:https://www.cnblogs.com/zhangxuan/p/6693994.html
Copyright © 2011-2022 走看看