title: Why do we use wait when we create child processes?
body: <p>I am a new C++ programmer and I am trying to figure out how parent and processes works. To do this, I tried to create three processes based on the codes here. However, I understand that we should exit after create our child processs each time to allow other child processes but I do not understand that in the second for loop, we we wait(NULL) for three times. Why are we doing this? It is my first time trying to understand parent and child process. I'd glad if you give me a source to study as well. Thanks!</p>
  
  <pre><code>int main(){
  
  cout&lt;&lt;" Dad process is "&lt;&lt;getpid()&lt;&lt;endl;
  
  for(int i=0;i&lt;3;i++){
      if(fork()==0)
      {
          cout&lt;&lt;"Son pid "&lt;&lt;getpid()&lt;&lt;" from pid "&lt;&lt;getppid()&lt;&lt;endl;
          exit(0);
      }
  }
  for(int i=0;i&lt;3;i++){
      wait(NULL);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>`</p>