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