title: understanding fork(), sleep() and processes flux
body: <p>Been practicing with those system calls, but I stucked into this code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  main()
  {
      pid_t pid;
      switch(pid = fork())
      {
          case -1:
              printf("fork failed");
              break;
          case 0:  //first child
              printf("\ni'm the first child, my pid is %d", getpid());
              fflush(stdout);
              break;
          default:    //parent
              sleep(5); /** sleep is generating problems **/
              printf("\ni'm the parent process, my pid is %d", getpid());
              printf("\ngenerating a new child");
              fflush(stdout);
              switch(pid = fork())
              {
                  case -1:
                      printf("fork failed");
                      break;
                  case 0: //second child
                      printf("\nhere i am, the second child, my pid is %d", getpid());
                      break;
                  default:  //parent
                      wait((int *)0);
                      printf("\nback to parent, my pid is %d", getpid());
              }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The output I'm getting is:</p>
  
  <pre>i'm the first child, my pid is 6203
  i'm the parent process, my pid is 6202
  generating a new child
  back to parent, my pid is 6202
  Process returned 0 (0x0)   execution time: 5.004 s
  Press ENTER to continue
  
  here i am, the second child, my pid is 6204</pre>
  
  <p>What I'm trying it's a simple print of these messages managing the timing via <code>sleep()</code>.
  I can't understand why the program is returning before printing the second child message.
  The default case(the one right after the second fork) got printed before its child(second) acting on the output like he's ignoring its <code>wait()</code>. Its child therefore got printed after the process returns.</p>
  
  <p>I wasn't able to figure out what's the problem. I've marked sleep() function since if I substitute it with <code>wait((int *)0);</code> the processes flux is working how it was designed to (anyhow, without any timing).
  At this point I'm not sure anymore about process flux, or the sleep() usage (man pages wasn't that helpful, way too concise to be honest).</p>