title: C, Piping messages to child processes not working. Why?
body: <p>I'm currently working on an assignment for my Operating Systems class, where we are required to write a C program which will first open four pipes, and then create 3 children.</p>
  
  <p>The parent will then take in messages from the user to send to child processes, and send them off through the pipes to the child. The child processes will then display all the messages they receive.</p>
  
  <p>Pipe 1 will go from Parent to Child 1.
  Pipe 2 will go from Parent to Child 2 or 3.
  Pipes 3 and 4 are pipes going between Children 2 and 3 to make sure the message ends up with the correct child.</p>
  
  <p>Your program should behave as follows:</p>
  
  <pre><code>•   AFTER opening the pipes and creating the children, the parent process should prompt the user for the number of messages to pass. ONLY the parent should have access to this quantity. The children should not be aware of it!
  
  •   Once the parent knows how many messages to expect from the user, it should prompt the user for those messages in the form:
  “&lt;message string&gt; &lt;child to receive message&gt;”
  
  •   You may assume that messages are only one word in length (you do not need to handle spaces in messages).
  
  •   The parent will then use pipes P1 and P2 to send all the messages to the appropriate children.
  
  •   Because C2 and C3 share pipe P2, they may receive each other’s messages. In this situation, they are responsible for using P3 or P4 to forward the messages as appropriate.
  
  •   Each process should ensure that its pipes are unidirectional.
  
  •   Once received, messages MUST be printed out in the form
  “Child &lt;x&gt; read message: &lt;msg&gt;”
  Where &lt;x&gt; is the number of the child (1, 2, 3) that is printing the message and
  &lt;msg&gt; is the message itself.
  
  *Hint: To avoid blocking reads in the children, you should consider what happens when processes close one end of a pipe.
  
  *Hint: When sending messages to C2 and C3, you may want to append a special character to the message so that they will know if it was meant for them or not.
  
  *Hint: It’s probably a good idea to perform all the writes before performing any reads.
  </code></pre>
  
  <p>Currently, my children processes will receive one or two messages, but then will stop. I think the read function is blocking, but I'm not sure what for, or why.</p>
  
  <p>Sample output:</p>
  
  <blockquote>
    <p>[root@cmachine CIS370]# ./parksPipes How many messages? 3</p>
    
    <p>message (1)  : ey 1</p>
    
    <p>message (2)  : ay 1</p>
    
    <p>message (3)  : yo 1</p>
    
    <p>Message : ey written to Child 1.</p>
    
    <p>Message : ay written to Child 1.</p>
    
    <p>Message : yo written to Child 1.</p>
    
    <p>Parent exiting... </p>
    
    <p>C1 receive: ey</p>
    
    <p>(program hangs at this point.)</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;fcntl.h&gt;
  #define MSGSIZE 256
  
  main()
  {
      int ppid = getpid(); // Get parent id
      int nmessages = 0;
      int p1[2]; int p2[2]; int p3[2]; int p4[2]; // Arrays used for the pipes
      if(pipe(p1)==-1){printf("p1 err, errno: %d", errno); exit(-1);} if(pipe(p2)==-1){printf("p2 err, errno: %d", errno); exit(-1);}  // Open pipes
      if(pipe(p3)==-1){printf("p3 err, errno: %d", errno); exit(-1);} if(pipe(p4)==-1){printf("p4 err, errno: %d", errno); exit(-1);}
  
      int kidpid1 = fork();
      if(kidpid1 == 0) // child 1
      {
          int nread;
          char mesg[MSGSIZE];
          close(p1[1]);
          close(p2[0]); close(p2[1]); close(p3[0]); close(p3[1]); close(p4[0]); close(p4[1]);
          while( (nread = read(p1[0], mesg, MSGSIZE))&gt;0) // read will be blocked until write end of pipe closed
          {
                printf("C1 receive: %s\n", mesg);
          }
          printf("Child 1 exiting...\n");
          exit(0);
      }
      else if(kidpid1 &gt; 0) // parent
      {
          int kidpid2 = fork();
          if(kidpid2 == 0) // child 2
          {
              int nread;
              char mesg[MSGSIZE];
              while( (nread = read(p2[0], mesg, MSGSIZE))&gt;0)
              {
                  printf("C2 receive: %s\n", mesg);
              }
              printf("Child 2 exiting...\n");
              exit(0);
          }
          else if(kidpid2&gt;0) // parent
          {
              int kidpid3 = fork();
              if(kidpid3 == 0) // child 3
              {
                      close(p2[1]);
                      int nread;
                      char mesg[MSGSIZE];
                      while( (nread = read(p2[0], mesg, MSGSIZE))&gt;0)
                      {
                          printf("C3 receive: %s\n", mesg);
                      }
                      printf("Child 3 exiting...\n");
                      exit(0);
              }
              else if(kidpid3 &gt; 0) // parent
              {
                  close(p1[0]); close(p2[0]); close(p3[0]); close(p4[0]); // Close all the read ends of the pipes (parent will not be reading)
                  close(p3[1]); close(p4[1]); // Close the write ends of pipes 3 and 4 (parent will not be writing down these pipes)
                  printf("How many messages? ");
                  scanf("%d", &amp;nmessages);
                  char *msg[nmessages];
                   int child[nmessages];
                   int i=0;
                  for(i=0; i&lt;nmessages; i++)
                  {
                      printf("Enter message (%d) &lt;message string&gt; &lt;child to receive message&gt;: ", i+1);
                      scanf("%s %d", &amp;msg[i], &amp;child[i]);
                      //printf("%s %d", &amp;msg[i], child[i]); // Parrot the message back for debugging purposes
                  }
                  for(i=0; i&lt;nmessages; i++)
                  {
                      switch(child[i])
                      {
                          case 1: // send msg[i] to child 1 (p1)
                          if(write(p1[1],&amp;msg[i],sizeof(msg[i]))== -1)
                          {
                              perror("Pipe 1 write error. errno: %d\n");
                              exit(-1);
                          }
                          else
                            printf("Message : %s written to Child %d.\n", &amp;msg[i], child[i]);
                          break;
                          case 2: // send msg[i] to child 2 (p2) (p2 goes to C2 and C3, p3 and p4 is between the two children)
                          if(write(p2[1],&amp;msg[i],sizeof(msg[i])) &lt; 0)
                          {
                              printf("Pipe 2 write error. errno: %d\n", errno);
                              exit(-1);
                          }
                          break;
                          case 3: // send msg[i] to child 3 (p2)
                          if(write(p2[1],&amp;msg[i],sizeof(msg[i])) &lt; 0)
                          {
                              printf("Pipe 1 write error. errno: %d\n", errno);
                              exit(-1);
                          }
                          break;
                          default:
                          printf("Incorrect child ID entered: %d", child[i]);
                          exit(-1);
                          break;
                      }
                  }
                  close(p1[1]); // Close write end of Pipe 1
                  close(p2[1]); // Close write end of Pipe 2
                  printf("Parent exiting...\n");
              }
              else
              {printf("Err ln35, errno: %d", errno); exit(-1);}
          }
          else // error
          {printf("Err ln18, errno: %d", errno); exit(-1);}
      }
      else // error
      {printf("Fork err, ln 75. errno: %d", errno); exit(-1);}
  }
  </code></pre>