See approach the question like this
main() {
int child = fork();
/* → At this point a new process having exactly the same properties will be forked
In simple words with the exact copy of this code a new process is generated
fork() will return
0 to the child process
the ProcessID of the child to the parent*/
/*One good point to note here is that child will execute code from the next line
It will not execute already executed code*/
int c = 5; /* this variable will have a different copy at parent and the child*/
/*From this part it gets interesting*/
// for the child this if is always false as 0 is never equal to 5
/* for the parent it may or maynot be false if child's pid is 5 then it
evaluates to true, if if child's pid is not 5 then it evaluates to false*/
if (child == 5)
{
C + = 5;
}
else {
Child = fork();
C + = 5;
}
}
Now if both parent(Lets call it as P1) and the child(Lets call it as P2) goes to else code
Both will fork two more processes which will execute C+=5
Final value of C in this case in all 4 processes = 5+5=10
Now if both parent(Lets call it as P1) goes to if and the child(Lets call it as P2) goes to else code
Child will fork one more processes which will execute C+=5
Final value of C in this case in all 3 processes = 5+5=10
so C in any case will have only 1 value 5+5=10