title: How to make parent process wait for child processes to finish?
body: <p>I have an assignment which gives me this code to transform into a code that makes the parent process wait for all children processes to finish.
  PS: the first code has 4 processes and needs to use waitpid to solve this.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  int main(){
    pid_t p = fork();
    pid_t k = fork();
  
    if(p&gt;0){
       printf("p=%d: PID = %d\n", p, getpid());
       sleep(45);
       exit(0);
    }
    else if(p==0){
       printf("p=%d: PID = %d\n", p, getpid());
       exit(0);
    }
    else if(p&lt;0){
       printf("ERRO! p=%d\n", p);
       exit(p);
    }
  }
  </code></pre>
  
  <p>I've tried this, but I think that this only works for only 1 child process and not for a lot of them.</p>
  
  <pre><code>int main(){
  
      pid_t p = fork(); 
      pid_t k = fork(); 
  
      if(p&lt;0){
          printf("fodeu");
          exit(p);
      }
      else if(p==0){
          printf("");
          exit(0);
      }
      else{
          for(i=0;i&lt;4;i++){
              int returnstatus; 
              waitpid(p,&amp;returnstatus,0);
  
              if(returnstatus == 0){
                  printf("o processo filho correu normalmente");
              }
              else if(returnstatus == 1){
                  printf("o processo filho ardeu");
              }
          }
      }
  }
  </code></pre>