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

  • 相关阅读:
    CSS(八) 常见的居中定位
    VUE常用的标签属性和指令
    ES6面向对象
    ES6解构赋值
    jvarScript的多个参数
    ES6的箭头函数
    Light OJ 1125 Divisible Group Sums
    Light OJ 1168 Wishing Snake
    Light OJ 1044 Palindrome Partitioning
    hdu 3967 Zero's Number
  • 原文地址:https://www.cnblogs.com/zhangxuan/p/6693994.html
Copyright © 2011-2022 走看看