title: Print exit order of child processes
body: <p>I have this code. My aim is to create 'n' child processes from parent process and print the pid of each child processes once it is created.</p>
  
  <p>Then I'll ask each child process to sleep for random duration using <code>rand_r</code> function and <code>exit</code>. I want to print the exit order of child processes <em>i.e</em> I want to print their pids in their exit order.</p>
  
  <p>This is my C code. It is not giving required output:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/wait.h&gt;
  
  int func(int n) 
  {
      if(n==0){return 0;}
      int pid=fork();
  
      unsigned int k=n;
      int x=rand_r(&amp;k) % 3;
      //printf("here: %d\n", x);
  
      if(pid==0){
          printf("Child %d is created\n",getpid());
          func(n-1);
          sleep(x);
          printf("Child %d exited\n",getpid());
          exit(0);
      }
      return 1;
  }
  
  
  int main(int argc, char *argv[]){
      char *inp = argv[1];
      int n = atoi(inp);
      func(n);
  
      return 0;
  }
  </code></pre>
  
  <p>I want output like this:</p>
  
  <pre><code>Child 7432 is created 
  Child 7433 is created 
  Child 7434 is created 
  Child 7435 is created   
  Child 7433 exited 
  Child 7432 exited 
  Child 7435 exited 
  Child 7434 exited
  </code></pre>