title: How parent process wait all child processes terminate
body: <p>I am new to C , So i  only know simple functions.(ex: wait(NULL) ).</p>
  
  <p>Here is my main question :</p>
  
  <blockquote>
    <p>Modify the program so that only the parent process creates 3 child processes, and each
    new created process calls a function CPU(). In addition, make the parent process wait for
    each child’s termination.</p>
  </blockquote>
  
  <p><strong>I know this one is the correct answer</strong></p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  int main(void)
  {
      int i ;
  
      for( i = 0; i &lt; 3; i++)
      {
          int ret = fork();
          if(ret == 0){
              printf("My process ID is %d\n", getpid());
              return 0;
          }
  
      }
  
      for (i = 0; i &lt; 3; i++) //Line B
          wait(NULL);
  }
  </code></pre>
  
  <p><strong>but my problems are</strong></p>
  
  <ol>
  <li><p>why below code is wrong when wait is executed by the parent in the loop</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void)
  {
      int i ;
  
      for( i = 0; i &lt; 3; i++)
      {
          int ret = fork();
  
          if(ret == 0) {
              printf("My process ID is %d\n", getpid());
              return 0;
          }
          else
              wait(NULL);
      }
  }
  </code></pre></li>
  <li><p>in very first code why do we write <code>wait(NULL)</code> inside a for loop?
  can't we write it without a for loop</p></li>
  <li><p><strong>if</strong> there is no "return 0" in child process, should the for loop be changed to</p>
  
  <pre><code>for (i = 0; i &lt;7; i++)
      wait(NULL);
  </code></pre></li>
  <li><p>I don't know how to write the <code>CPU</code> function</p></li>
  </ol>