Use signals for inter-process communication between Linux

Source: Internet
Author: User

Linux processes can send signals to each other to send some notifications, the signal can carry data (4 bytes), specifically see the Sigqueue function.

If you want to use a custom signal to send data, the normal signal only reserved two signals USER1 USER2, if two is not enough, Linux also provides real-time signal this kind of thing.

The user can define his own signal and send it, but the number is not infinite at present about 32 can be used.

Test code:

#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&NBSP;SIG,&NBSP;SIGINFO_T&NBSP;*SIGINFO,&NBSP;VOID&NBSP;*PTR)  {    if  ( SIG&NBSP;==&NBSP;SIG_TEST1)  {        printf ("I get sig  test1 %d \n ",  siginfo->si_value);     }    if   (SIG&NBSP;==&NBSP;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;}

Output:

[Root@centos ~]#./a.out

I get SIG Test1 1

I get SIG Test2 2

Use signals for inter-process communication between Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.