title: How do I make the parents processes wait for the children to finish using waitpid?
body: <p>I want to make the parent processes (P1 and P2) wait for their respective child processes(P3 and P4) to terminate using waitpid (<strong>basically, parent processes can only terminate after the children terminates</strong>). Here's the code below. I'm portuguese, sorry for having non-english words on the code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  
  int main()
  {
    int estado1, estado2;
  
    pid_t p = fork();
    pid_t k = fork();
  
    if(p&gt;0) //Execução do pai
    {
      waitpid(p, &amp;estado1, 0); //Don't know how waitpid works that well either
      waitpid(k, &amp;estado2, 0);
  
      printf("p=%d: PID = %d;\t", p, getpid());
      // \t é para dar um tab (o que se usa nas indentações)
  
      sleep(45);
  
      printf("O processo pai terminou.\n");
  
      exit(0);
    }
    else if(p==0) //Execução do filho
    {
      printf("p=%d: PID = %d;\t", p, getpid());
      // \t é para dar um tab (o que se usa nas indentações)
  
      printf("O processo filho terminou.\n");
  
      exit(0);
    }
    else if(p&lt;0)
    {
      printf("ERRO! p=%d\n", p);
  
      exit(p);
    }
  }
  </code></pre>
  
  <p>This is what I have but I think it isn't right.
  Thanks in advance.
  Edit: Thanks to zeroSpook for fixing my text :)</p>