title: How to fork() n child processes correctly in C?
body: <p>That is my code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main ( int argc, char *argv[] )
  {
      int i, pid;
  
  for(i = 0; i &lt; atoi(argv[1]); i++) {
      pid = fork();
      if(pid &lt; 0) {
          printf("Error");
          exit(1);
      } else if (pid == 0) {
          printf("Child (%d): %d\n", i + 1, getpid());
          exit(0); 
      } else  {
          wait(NULL);
      }
  }
  
  }
  </code></pre>
  
  <p>The output is like that.</p>
  
  <pre><code>Child (1): 5676
  Child (2): 4624
  Child (3): 4800
  Child (4): 5596
  Child (5): 5580
  </code></pre>
  
  <p>However that is not the expect output in the my homework.
  It should be like that. What's wrong with code? Can someone help me?</p>
  
  <pre><code>Child (2): 4625
  Child (1): 4624
  Child (3): 4626
  Child (4): 4627
  Child (5): 4628
  </code></pre>
  
  <p>Thank You for your help. Now I will try it out.</p>
  
  <p>P.S. Sorry my English is bad. I hope you can understand what I said.</p>