Problems with a process communication

Source: Internet
Author: User

The following is a description of the problem:

The parent process creates child process 1 and then creates child process 2, and child process 1 multiplies its own PID by 2, sends a reliable signal to child process 2, and the child process 2 is received and sent to the parent process.

Second, the problem analysis:

First, the process of sending data between processes can be done with sigaction/sigqueue, and the reliable signal is 32-64 of the kill-l. The question is, how does child process 1 know the PID of child process 2? If you know, then this problem will be solved.

Third, problem solving.

After the parent process creates child process 1, waits, after the child process 2 is created, the parent process sends the PID of process 2 to process 1 so that the data can be sent continuously.

The code is as follows: (The child process inherits the signal registered by the parent process, so the parent process--child process 1, child process 1-child process 2, child process 2-the processing function of the three send signal of the parent process is registered at the beginning.) )

1#include <stdio.h>2#include <stdlib.h>3#include <errno.h>4#include <fcntl.h>5#include <unistd.h>6#include <sys/stat.h>7#include <sys/types.h>8#include <sys/types.h>9#include <signal.h>Ten#include <string.h> One#include <sys/wait.h> A //The requirement is that child process 1 is sent to 2, but how does 1 get 2 to PID?  - voidHandleintNum, siginfo_t * sigdata,void*p) - { the     if(num = = sigrtmin+1){ -printf"recv data:%d from pid2\n",sigdata->si_value.sival_int);  -     } -     if(num = = sigrtmin+2){ +         //received the parent process sent to the signal, parse out the PID2, and then send Pid*2 sent to Pid2 -printf"get Pid2 ' from Father%d\n",sigdata->si_value.sival_int); + Union Sigval SendData; ASenddata.sival_int = Getpid () *2;  at         if(Sigqueue (sigdata->si_value.sival_int,sigrtmin+3, senddata) = =-1){ -Perror ("Error"); -         }                     -     } -     if(num = = sigrtmin+3){ -pid_t PID =getppid (); in Union Sigval SendData; -Senddata.sival_int = sigdata->Si_value.sival_int; toprintf"get data%d from pid1\n", senddata.sival_int); +         if(Sigqueue (pid,sigrtmin+1, senddata) = =-1){ -Perror ("Error"); the         } *     } $ }Panax Notoginseng intMainvoid) - { thepid_t pidarray[Ten] = {0}; + pid_t pid; A     inti; the     structsigaction Sigaf; +Sigaf.sa_sigaction =handle;  -Sigaf.sa_flags =Sa_siginfo; $Sigaction (sigrtmin+1,&sigaf,null); $      -Sigaction (sigrtmin+2,&sigaf,null); -  theSigaction (sigrtmin+3,&sigaf,null); -      for(i =0; I <2; ++i) {WuyiPidarray[i] = PID =fork (); the         if(PID = =0){ -              Break;//child processes do not give birth to child processes Wu         } -         Else if(PID >0){ About         } $     } -     if(PID >0){//Parent Process -         //Send Pid2 to Pid1 -          while(pidarray[2] ==0){ A              +         } the Union Sigval Sendtoone; -Sendtoone.sival_int = pidarray[1]; $         if(Sigqueue (pidarray[0],sigrtmin+2, sendtoone) = =-1){ thePerror ("Error");  the         } theSleepTen); the     } -  in     if(PID = =0&& i = =0){//Sub-Process 1 theSleep4); theExit0); About     } the     if(PID = =0&& i = =1){//Sub-process 2 theSleep4); theExit0); +     } -     intMypid =0; the      while(Mypid = Waitpid (-1, Null,wnohang)) >0){Bayiprintf"%d end \ n", mypid); the     } the     return 0; -}

Problems with a process communication

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.