標籤: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
Linux 處理序間通訊之使用訊號