title: Why the output is printing twice?
body: <p>May be it look childish for most of you but I am unable to understand this small piece of code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  int main(int argc, char** argv) {
      int i, pid;
      pid = fork();
      printf("Forking the pid: %d\n",pid);
      for(i =0; i&lt;5; i++)
          printf("%d  %d\n", i,getpid());
      if(pid)
          wait(NULL);
      return (0);
  }
  </code></pre>
  
  <p>Out put of this program is </p>
  
  <pre><code>Forking the pid: 2223
  0  2221
  1  2221
  2  2221
  3  2221
  4  2221
  Forking the pid: 0
  0  2223
  1  2223
  2  2223
  3  2223
  4  2223
  Press [Enter] to close the terminal ...
  </code></pre>
  
  <p>In the for loop the printf command is used once. Why "Forking the pid" and after that the pid's are printed twice. How this is working? Can anybody explain me this? Thanks in advance. 
  Can anybody explain me why we have to use wait here? What I understood from the man pages is wait retuns the control to parent process? Is what I understood is correct?Is it necessary to use wait after forking a process?
  Operating system : ubuntu, compiler : gcc, IDE : netbeans </p>