title: Check for exited child processes in POSIX?
body: <p>I'm trying to write a program that checks for exited child processes and restarts them if they have exited. It needs to restart the processes as they exit, without waiting for any other processes to have exited. I'm a little lost. Here's the code that I've done so far. It's not finished or correct, really. But perhaps someone can point me in the right direction? </p>
  
  <pre><code>for(int ifile = 1; ifile &lt; 4; ifile++){
  
      child_pid[ifile - 1] = vfork();
  
      if(child_pid[ifile - 1] == -1){
          cerr &lt;&lt; "fork error on " &lt;&lt; argv[ifile] &lt;&lt; endl;
      }
      else if(child_pid[ifile - 1] == 0){
          execl(argv[ifile], argv[ifile], NULL);
      }
  }
  
  for(int index = 0; index &lt; 3; index++){
      do{
          wait_pid = waitpid(child_pid[index], &amp;status, WUNTRACED);
          if(WIFEXITED(status)){
              count++;
              child_pid[index] = vfork();
              if(child_pid[index] == -1){
                  cerr &lt;&lt; "rescheduled process error" &lt;&lt; endl;
                  return -1;
              }
              else if(child_pid[index] == 0){
                  cout &lt;&lt; "Rescheduling " &lt;&lt; argv[index + 1] &lt;&lt; endl;
                  execl(argv[index + 1], argv[index + 1], NULL);
              }
          }
      }while(count != 4);
  }
  </code></pre>