title: How to use fork() to run 3 commands in C?
body: <p>I have to implement code that will run a certain number of executables (ex. grep, echo, etc.) at the same time (concurrently). The executables will be given as a string, which I have parsed. The requirement of the code is that the output of one executable is the input of another.</p>
  
  <p>I am trying to understand how to implement this when there is an odd number of executables. To my knowledge the fork function will replicate the code at hand. If I put my code as such:</p>
  
  <pre><code>for(int I=0; I&lt;3; I++){
       pid_t pid = fork();
  }
  </code></pre>
  
  <p>then when <code>I==0</code>, 1 child will be produced. When <code>I==1</code>, 2 children will be produce, and similarly, when <code>I==2</code>, 4 children will be produced. If I was to only allow execution to happen in the child (when <code>pid==0</code>), I would still have a total of 7 executions, which is 4 more than I need.</p>