title: Executing child before parent in C
body: <pre><code>#include&lt;stdio.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;stdlib.h&gt;
  
  int main(int argc, char ** argv)
  {
  
      int pid;
      int status;
      pid = fork();
  
      if (pid &lt; 0)
      {
          printf("Cannot create a child process");
          exit(1);
      }
      else if (pid == 0)
      {
  
          printf("I am the child process. %d \n", getpid());
          printf("The child process is done. \n");
          fflush(stdout); // it does not write immediately to a disk.
          exit(0);
      }
      else
      {
  
          printf("I am the parent process.%d \n", getpid());
          sleep(5); // sleep does not works
          pid = wait(&amp;status); // want to wait until the child is complited
          printf("The parent process is done. \n");
          fflush(stdout); // it does not write immediately to a disk.
          exit(1);
      }
  }
  </code></pre>
  
  <p>Hi guys, I am currently trying to create a child process. So far so good, however, what I am trying now is to execute the child first and print out and always print out the message "The parent process is done" last.</p>
  
  <pre><code>I am the parent process.28847  
  I am the child process. 28848 
  The child process is done.
  The parent process is done.
  </code></pre>
  
  <p>Currently is printing this:</p>
  
  <pre><code>I am the parent process.28847 
  The parent process is done.
  I am the child process. 28848 
  The child process is done.
  </code></pre>
  
  <p>It is my first time using forking so I am not really confident in what I am doing, I've tried sleep and wait(&amp;status) to try wait until the child process is finished and then execute the parent but something is not working.</p>
  
  <p>P.S. sorry for the bad layout, fist time using stackoverflow.</p>