标签:style http io ar color os 使用 sp on
Linux 进程之间可以相互发送信号,来发送一些通知,信号可以携带数据(4个字节) ,具体看 sigqueue 函数。
如果要使用自定义的信号来发送数据的话,普通信号只预留了两个信号 USER1 USER2 ,如果两个不够用的话,Linux还提供了实时信号这种东西。
用户可以定义自己的信号 并发送它,但是数量也不是无限的 目前大概有 32 个可以使用。
测试代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <cygwin/signal.h>
using namespace std;
int SIG_TEST1 = SIGRTMIN + 1;
int SIG_TEST2 = SIGRTMIN + 2;
static void sig_hdl(int sig, siginfo_t *siginfo, void *ptr) {
if (sig == SIG_TEST1) {
printf("i get sig test1 %d \n", siginfo->si_value);
}
if (sig == SIG_TEST2) {
printf("i get sig test2 %d \n", siginfo->si_value);
}
}
int main() {
struct sigaction st;
memset(&st, 0, sizeof(st));
st.sa_flags = SA_SIGINFO;
st.sa_sigaction = sig_hdl;
sigaction(SIG_TEST1, &st, NULL);
st.sa_sigaction = sig_hdl;
sigaction(SIG_TEST2, &st, NULL);
sigval t;
t.sival_int = 1;
sigqueue(getpid(), SIG_TEST1, t);
t.sival_int = 2;
sigqueue(getpid(), SIG_TEST2, t);
return 0;
}
输出:
[root@centos ~]# ./a.out
i get sig test1 1
i get sig test2 2
标签:style http io ar color os 使用 sp on
原文地址:http://my.oschina.net/sincoder/blog/351473