title: How to run child processes simultaneously? in c
body: <p>So here is my code that will take an int as an command line argument then fork N child processes (That run simultaneously). And then when each child ends, the parent will echo the child that child exit status.</p>
  
  <p>But right now I can only do child by child but not simultaneously. How can I do it?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;time.h&gt;
  
  int main ( int argc, char *argv[] )
  {
      int i, pid, ran;
  
      for(i = 0; i &lt; atoi(argv[1]); i++) {
      pid = fork();
      srand(time(NULL));
      ran = (rand() % 10) + 1 ;
  
       if (pid &lt; 0) {
          printf("Error");
          exit(1);
       } else if (pid == 0) {
          printf("Child (%d): %d\n", i + 1, getpid());
          printf("Sleep for = %d\n", ran);
          sleep(ran);
          exit(ran); 
       } else  {
          int status = 0;
          pid_t childpid = wait(&amp;status);
          printf("Parent knows child %d is finished. \n", (int)childpid);
       }
    }
  </code></pre>
  
  <p>}</p>