Question: For input of `n = 10`, i am getting a weird output : 

```
pid = 31456
Sum of Odd series : 25
pid = 31456
Sum of Even series : 30
pid = 31456

```

Code

```
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

#include <stdlib.h>

int main()
{
    pid_t child_pid;
    int i, j, k, n;
    int sum;

    //printf("Enter the last number of series : ");
    scanf("%d", &n);
    printf("pid = %d\n", getpid());

    for(i = 0; i < 2; i++)
    {
        child_pid = fork();
        if (child_pid < 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 <= 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 <= n; j += 2)
                    sum += j;

                printf("Sum of Even series : %d\n", sum);
                exit(0);
            }

        }
        else
        {
            wait(&child_pid);
        }
    }
    return 0;
}

```

Ideone link : [ideone.com/TyIkJa](http://ideone.com/TyIkJa)

Answer: 
You will have to change your code as below.

```
if (childs[i] == 0)
    {
        if (i == 0) //find the sum of odd series
        {
            for(j = 1; j <= 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
        {
            for(k = 2; k <= n; k += 2)
                sum += k;
            printf("Sum of even series : %d\n", sum);  
            exit(0);
        }

else
    {
        wait(&childs[i]);
    }

```

The reason is both children and parent have their own copies of sum. So when you are ending the parent still has sum=0 ,but these values are updated for "sum" of child1 and "sum" of child2.
When you fork a process, then parent and child are initially 
more or less exact copies, including the values of variables, 
 except for the return code of fork(). 

But the key word there is copies. 
Those two processes are now independent and variables, 
"global" or not, \**are not connecte*\*d in any way.