title: Waiting for all child processes before parent resumes execution UNIX
body: <p>In my program I am forking (in parallel) child processes in a finite while loop and doing exec on each of them. I want the parent process to resume execution (the point after this while loop ) only after all children have terminated. How should I do that?</p>
  
  <p>i have tried several approaches. In one approach, I made parent pause after while loop and sent some condition from SIGCHLD handler only when waitpid returned error ECHILD(no child remaining) but the problem I am facing in this approach is even before parent has finished forking all processes, retStat becomes -1 </p>
  
  <pre><code>    void sigchld_handler(int signo) {
          pid_t pid;
          while((pid= waitpid(-1,NULL,WNOHANG)) &gt; 0);
          if(errno == ECHILD) {
              retStat = -1;
          }
      }
  
      **//parent process code**
      retStat = 1;
      while(some condition) {
         do fork(and exec);
      }
  
      while(retStat &gt; 0)
          pause();
  //This is the point where I want execution to resumed only when all children have finished
  </code></pre>