title: Use Fork() to create three child processes with only 2 grandchild processes
body: <p>Using fork() to create child process, I am trying to make a tree that looks like this:</p>
  
  <pre><code>    p
    / | \
   p  p  p
     / \
    p   p
  </code></pre>
  
  <p>I have the parent create the three children processes, but I can't get the two outside children to stop forking and the second child to only fork twice.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  
  int main ()
   {
  
  pid_t lchildpid, rchildpid,mchildpid, parentpid;
  int n = 0;
  lchildpid = 0;
  rchildpid = 0;
  mchildpid = 0;
  
  
   printf("\nLvl\tProc\tParent\tChild 1\tChild 2\tChild 3\n");
  printf("No.\tID\tID\tID\tID\tID\n");
  
   while(n &lt; 3){
  
  
  if((lchildpid = fork()) == 0 || (mchildpid = fork()) == 0 || (rchildpid = fork()) == 0) {
  
          parentpid = getppid();
          n++;
          continue;
  
      }
  
      //check for forking errors
      if(lchildpid == -1 || rchildpid == -1 || mchildpid == -1) 
      {
          perror("\n The fork failed\n");
          exit(1);
      }
  
      //If current fork has two children, print complete generation to console and exit while loop
      if(lchildpid &amp;&amp; rchildpid &amp;&amp; mchildpid ){
          printf("%d\t%ld\t%ld\t%ld\t%ld\t%ld\n",n, (long)getpid(), (long)getppid(), (long)lchildpid, (long)mchildpid,(long)rchildpid);
          break;
  
  
        }
  
   }
         exit(0);
   }      
  </code></pre>
  
  <p>Here is my output:</p>
  
  <pre class="lang-none prettyprint-override"><code>   Lvl     Proc    Parent  Child 1 Child 2 Child 3                                                                                                                                   
     No.     ID      ID      ID      ID      ID                                                                                                                                        
      0       22      7       23      24      25                                                                                                                                        
      1       23      1       26      28      32                                                                                                                                        
      1       25      1       29      31      33                                                                                                                                        
      1       24      1       27      30      34                                                                                                                                
  </code></pre>
  
  <p>I want this output:</p>
  
  <pre class="lang-none prettyprint-override"><code>    Lvl     Proc    Parent  Child 1 Child 2 Child 3                                                                                                                                   
      No.     ID      ID      ID      ID      ID                                                                                                                                        
       0       40      7       41      42      43                                                                                                                                        
       1       41      40       0       0      0                                                                                                                                
       1       42      40      44      45      0                                                                                                                                        
       1       43      40       0       0      0        
  </code></pre>