title: execution of if/else if/else with a fork()
body: <p>I have tried implementing an os program. Here is the code: </p>
  
  <pre><code>#include&lt;sys/types.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;unistd.h&gt;
  
  int main()
  {
      pid_t pid, pid1;
  
      pid = fork();
  
      if(pid&lt;0)
      {
              fprintf(stderr,"Fork Failed");
              return 1;
      }
  
      else if(pid == 0) /* child process */
      {
              pid1 = getpid();
              printf("child: pid = %d\n",pid);
              printf("child: pid1 = %d\n",pid1);
  
      }
  
      else   /* parent process */
      {
              pid1 = getpid();
              printf("parent: pid = %d\n",pid);
              printf("parent: pid1 = %d\n",pid1);
  
      }
  
      return 0;
  }
  </code></pre>
  
  <p>and its o/p:</p>
  
  <pre><code>parent: pid = 1836
  parent: pid1 = 1835
  child: pid = 0
  child: pid1 = 1836
  </code></pre>
  
  <p>can somebody explain me how is it working , i.e. the sequence of the execution for the <code>if</code>/<code>else-if</code>/<code>else</code> statements written in the code. I would think once the <code>else if</code> condition becomes true then <code>else</code> part is not executed, however here it has executed the parent process part i.e. <code>else</code> part and then the child part ..... how come?</p>