Click here to Skip to main content
15,665,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to use fork and pipe to enable two communication between a parent process and 2 child processes.This code works perfectly when I use one child process, but if I use two, I am not getting sent between the parent and child process #2. It simply prints the first two messages again.This is the code that I have been using:

using namespace std;

char String1[] = "Hello, this is the message from parent to child1";
char String2[] = "Hello, this is the message from child1 to parent";
char String3[] ="Hello this is a msg from parent to child2";
char String4[]="Hello this is a msg from child2 to parent";

void catchInt(int signum)
  cout<<"Child1's PID is "<<(int)getpid()<<"\n";
  cout<<"Child1 process terminated"<<"\n";
  cout<<"Waiting for child2 to complete"<<"\n";;

void catchInt1(int signum)
  cout<<"Child2's PID is "<<(int)getpid()<<"\n";
  cout<<"Child2 process terminated\n";

int main ()
  char buf[1024];
  int fds1[2], fds2[2],fds3[2],fds4[2];
  int child1r, child1w, parentr1, parentw1,child2r,child2w,parentr2,parentw2;

  pipe (fds1);
  pipe (fds2);
  pipe (fds3);
  pipe (fds4);
  child1r = fds1[0];
  child1w = fds2[1];
  parentr1 = fds2[0];
  parentw1 = fds1[1];
  pid_t  pid = fork();
  pid_t pid1=fork();
  if (pid>0)
  { /* the parent process */
      close (child1r); close (child1w);close (parentr2);close (parentw2);close(child2r);close(child2w);
      write (parentw1, String1,sizeof (String1));
      read (parentr1, buf, sizeof (String2));
      cout<<" Parent:"<<buf<<"\n";
      kill(pid, SIGINT);
 else if(pid==0)
 { /* the child process */
      close (parentr1); close (parentw1);close (parentr2);close(parentw2);close(child2r);close(child2w);
      write (child1w, String2, sizeof (String2));
      read (child1r, buf, sizeof (String1));
      cout<<" Child1:"<<buf<<"\n";
      signal(SIGINT, catchInt);

  if (pid1>0)
      close (child1r); close (child1w);close (parentr1);close (parentw1);close(child2r);close(child2w);
      write (parentw2, String1, sizeof (String3));
      read (parentr2, buf, sizeof (String4));
      cout<<" Parent:"<<buf<<"\n";
      kill(pid1, SIGINT);
      cout<<"Parent PID :"<<(int)getpid()<<"\n";
      cout<<"Parent process terminated"<<"\n";
    else if (pid1==0)
      close (parentr1); close (parentw1);close (parentr2);close(parentw2);close(child1r);close(child1w);
      write (child2w, String2, sizeof (String4));
      read (child2r,buf,sizeof (String3));
      cout<<" Child2:"<<buf<<"\n";;
      signal(SIGINT, catchInt1);

  return 0;
Sergey Alexandrovich Kryukov 31-Jan-14 19:00pm    
Why would you use multi-process approach at all? Processes are well isolated, identification of them from the outer processes is complicated and unreliable; and you have to use IPC. If you can, it's always better to use in-proc modularization, at the level of PE files (DLLs) and, on top of that, you can use different threads of the same process. I would first rething you whole approach.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900