title: Fork() Parent and Child
body: <p>what I need is to have 3 children and 1 parent (the main program) because the children and parent are going to have diferent functionality.
  I don't know why I can't do it properly, as far I have read, when you do a fork(), if it returns 0, you execute the child's code and the parent's code otherwise.
  So, this is my program:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int
  main(int argc, char ** argv) {
  
     printf ("Init Parent. PID =%d\n", getpid());
  
     fork();
     fork();
     if (fork() == 0) {
     printf ("Init Child. PID =%d\n", getpid());
      sleep(1);
  
     } else {        
      printf ("I am the parent. PID = %d\n",getpid()); 
      sleep(1);
  
     }
      exit(0);
  }
  </code></pre>
  
  <p>This is the output:</p>
  
  <pre><code>Init Parent. PID =9007
  I am the parent. PID = 9007
  I am the parent. PID = 9009
  Init Child. PID =9010
  Init Child. PID =9011
  I am the parent. PID = 9008
  I am the parent. PID = 9012
  Init Child. PID =9013
  Init Child. PID =9014
  </code></pre>
  
  <p>And this is what I want:</p>
  
  <pre><code>Init Parent. PID =9007
  I am the parent. PID = 9007
  Init Child. PID =9010
  Init Child. PID =9011
  Init Child. PID =9013
  Init Child. PID =9014
  </code></pre>
  
  <p>I don't know why it says I am the parent 3 times and how to do it properly, because 1 </p>
  
  <pre><code>if (fork() == 0) 
  </code></pre>
  
  <p>seems bad.</p>
  
  <p>I would really appreciate any help, thank you in advance.</p>