title: Create two child process to finish summation of odd and even series
body: <p>For input of <code>n = 10</code>, i am getting a weird output : </p>
  
  <pre><code>pid = 31456
  Sum of Odd series : 25
  pid = 31456
  Sum of Even series : 30
  pid = 31456
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;stdio.h&gt;
  
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      pid_t child_pid;
      int i, j, k, n;
      int sum;
  
      //printf("Enter the last number of series : ");
      scanf("%d", &amp;n);
      printf("pid = %d\n", getpid());
  
      for(i = 0; i &lt; 2; i++)
      {
          child_pid = fork();
          if (child_pid &lt; 0)
          {
              printf("Failed to create child process . . .");
              return 1;
          }
          else if (child_pid == 0)
          {
              if (i == 0) //find the sum of odd series
              {
                  sum = 0;
                  for(j = 1; j &lt;= n; j += 2)
                      sum += j;
  
                  printf("Sum of Odd series : %d\n", sum);
                  exit(0);
              }
              else if (i == 1) //find the sum of even series
              {
                  sum = 0;
                  for(j = 2; j &lt;= n; j += 2)
                      sum += j;
  
                  printf("Sum of Even series : %d\n", sum);
                  exit(0);
              }
  
          }
          else
          {
              wait(&amp;child_pid);
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>Ideone link : <a href="http://ideone.com/TyIkJa" rel="nofollow">ideone.com/TyIkJa</a></p>