title: Creating multiple child processes with a single pipe
body: <p>I need to create three child processes, each of which reads a string from the command line arguments and writes the string to a single pipe. The parent would then read the strings from the pipe and display all three of them on the screen. I tried doing it for two processes to test and it is printing one of the strings twice as opposed to both of them. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int main (int argc, char *argv[]) {
  
      char *character1 = argv[1];
      char *character2 = argv[2]; 
  
      char inbuf[100]; //creating an array with a max size of 100
  
      int p[2]; // Pipe descriptor array
      pid_t pid1; // defining pid1 of type pid_t
      pid_t pid2; // defining pid2 of type pid_t
  
      if (pipe(p) == -1) {
          fprintf(stderr, "Pipe Failed"); // pipe fail
      }
  
      pid1 = fork(); // fork
  
      if (pid1 &lt; 0) {
          fprintf(stderr, "Fork Failed"); // fork fail
      }
  
      else if (pid1 == 0){ // if child process 1
          close(p[0]); // close the read end
          write(p[1], character1, sizeof(&amp;inbuf[0])); // write character 1 to the pipe
      }
  
      else { // if parent, create a second child process, child process 2
          pid2 = fork();  
  
          if (pid2 &lt; 0) {
          fprintf(stderr, "Fork Failed"); // fork fail
          }
  
          if (pid2 = 0) { // if child process 2
              close(p[0]); // close the read end
              write(p[1], character2, sizeof(&amp;inbuf[0])); // write character 2 to the pipe
          }
  
          else { // if parent process
              close(p[1]); // close the write end
  
              read(p[0], inbuf, sizeof(&amp;inbuf[0])); // Read the pipe that both children write to 
              printf("%s\n", inbuf); // print 
  
              read(p[0], inbuf, sizeof(&amp;inbuf[0])); // Read the pipe that both children write to 
              printf("%s\n", inbuf); // print 
          }
      } 
  }
  </code></pre>