##### Let u,v be the values printed by the parent process and x,y be the values printed by the child process. Which one of the following is TRUE?
Consider the following code fragment:
if (fork() == 0)
{
a = a + 5;
printf("%d, %p n", a, &a);
}
else
{
a = a - 5;
printf ("%d, %p n", a,& a);
}


Let u,v be the values printed by the parent process and x,y be the values printed by the child process. Which one of the following is TRUE?

1. u = x + 10 and v = y
2. u = x + 10 and v != y
3. u + 10 = x and v = y
4. u + 10 = x and v != y
##### 1Comment
Sumit Verma
15 Jun 2017 12:49 am

Fine then, let's go line by line:

1.if (fork() == 0)

2. { a = a + 5; printf("%d,%d\n", a, &a); }

3. else { a = a –5; printf("%d, %d\n", a, &a); } 

Process P will execute line1. fork() will cause creation of a new child process say C1
Now Both P and C1 will start execution from the next instruction in line 1 which is the comparision of the returned value of fork() with zero.
For parent process P, this comparision is false as value returned by fork will be greater than 0(Process id of child process). So P will execute the else block.
For child process C1, Condition fork()==0 is true. So Cwill execute the if block.
Now u, v be the values printed by the parent process, and x, y be the values printed by the child process.
u= a-5, v=address of a (in parent process)
x= a+5, y=address of a (in child process)
Note one thing here: In virtual memory environment, all the processes start execution from the virtual address zero and hence both the process will have same logical address of the variable 'a' (physical address will be different).
Hence u + 10 = x and v = y is the correct option.