Linux 處理序間通訊之使用訊號

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.