Signaling and carrying data between processes

Source: Internet
Author: User

A process signals the specified process and passes the data

The send-side code is as follows:

Send_data_signo.c

#include <signal.h>#include<stdio.h>#include<stdlib.h>intMainintargcChar**argv)    {Union Sigval value; intSignum = SIGTERM;//Default Send Sigtermpid_t pid; inti; /*Initialize*/Value.sival_int=0; /*Check the number of parameters*/    if(ARGC! =3&& ARGC! =5&& ARGC! =7) {printf ("./send_data_signo <-d data> <-s signum> [-p][data]\n"); Exit (1); }    /*parse the signal number, PID, and data to be transmitted from the command line arguments*/     for(i=1; i<argc; i++) {        if(!STRCMP (Argv[i),"- D") ) {Value.sival_int= Atoi (argv[i+1]); Continue; }        if(!STRCMP (Argv[i),"- S") ) {Signum= Atoi (argv[i+1]); Continue; }        if(!STRCMP (Argv[i),"- P") ) {PID= Atoi (argv[i+1]); Continue; }    }    /*use Sigqueue to send a signal to the PID Signum, and carry the data value*/    if(Sigqueue (PID, Signum, value) <0) {perror ("Sigqueue"); Exit (1); }    return 0;}

The receive-side code is as follows:

#include <signal.h>#include<stdio.h>/*three-parameter signal processing program*/voidHandler_sigint (intSigno, siginfo_t *siginfo,void*pvoid) {printf ("recv SIGINT, the data value is:%d\n", siginfo->si_int);}intMain () {structSigaction Act; /*Assignment Act Structure*/act.sa_sigaction=Handler_sigint; Act.sa_flags= Sa_siginfo;//Specify a signal processing function that uses three parameters    /*installing signal processing functions*/sigaction (SIGINT,&Act, NULL);  while(1)        ; return 0;}

After compiling/linking, it is executed at two terminals, and the output is as follows:

The receiving end prints the signal, and carries the data

Send Side

The sending Process signals 2 (SIGINT) to process number 18617 and carries data, and then 9 (SIGKILL) carries the data.

Signaling and carrying data between processes

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.