title: For loop is executing in wrong order
body: <p>I am doing a for loop to create children with fork and print their pids, but instead of printing them in order it mixes them up.!  what it prints :</p>
  <p>[PARENT/PID=953] Created child 0 [PID=954] and initial state f</p>
  <p>[PARENT/PID=953] Created child 3 [PID=957] and initial state f</p>
  <p>[PARENT/PID=953] Created child 2 [PID=956] and initial state t</p>
  <p>[PARENT/PID=953] Created child 1 [PID=955] and initial state f</p>
  <p>also prints in other orders</p>
  <p>tried to put wait(&amp;status) in different places but it didnt help.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/stat.h&gt;
  #define PERMS 0644
  int main(int argc, char **argv) {
  int n = strlen (argv[1]);
  int status;
      for(int i=0;i&lt;n;i++) // loop will run n times
      {
          if(fork() == 0)
          {
              char s[5];
              sprintf(s, &quot;%c&quot;, argv[1][i]);
              char *const argv2[] = {&quot;./child.c&quot;,s,NULL};
              wait(&amp;status);
              status = execv(&quot;./child&quot;, argv2);    
              printf(&quot;[PARENT/PID=%d] Created child %d [PID=%d] and initial state %s \n&quot;,getppid(),i,getpid(),s);
              exit(0);
          }
      }
      for(int i=0;i&lt;n;i++) // loop will run n times
      wait(NULL);
  }
  </code></pre>