title: Concurrent Processes using fork()
body: <p>I am given the following code:</p>
  
  <pre><code>main()
  {
  int i, rc;
   for (i = 0; i&lt;=1; i++)
   {
     if( (rc=fork()) == 0)
     {
      printf("Child %d executing\n",i);
     }  /*end if*/
   } /*end for*/ 
  }
  printf("All children created\n");
  </code></pre>
  
  <p>I am also given the solution of the possible permutations of which outputs may occur. </p>
  
  <blockquote>
    <p>Child 0 executing |</p>
    
    <p>Child 1 executing | Child 1
     All children created |</p>
    
    <p>Child 1 executing | Child 2
     All children created |</p>
    
    <p>Child 1 executing | Grand child
     All children created |</p>
    
    <p>All children created | Parent</p>
  </blockquote>
  
  <p>I know that these outputs are created by each process but I am just having trouble tracing them to understand <strong>HOW</strong> these outputs occur. I know <code>fork()</code> creates a process and <code>if (fork() == 0)</code> means it is a child process, but if anyone can help me understand where the answers beyond <em>Child 0 executing |</em> come about thanks. I believe the | is just a description of which process is currently being ran. How come <code>child 1</code> can create a "grand child" but child 0 can not?</p>