看不懂 fork() 系统调用?

Not able to understand fork() system call clearly?

SO 上有很多关于 fork() 的问题,我已经阅读了很长时间。我正在尝试解决这样的技巧问题:

Consider the code fragment:
if(fork==0)
{a = a+5; printf("%d, %d \n", a, &a);}
else{a = a-5; printf("%d, %d \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 of the following is true:

a) u = x+10 and v = y
b) u = x+10 and v != y
c) u+10 = x and v = y
d) u+10 = x and v != y

显然,父进程和子进程的地址不同,所以答案必须是b或d。我什至无法解释 u 可以是 x+10 或者 u+10 可以是 x。这究竟是如何工作的? fork() 创建进程的两个完全相同的副本。如果 fork() returns 为零,则表示父级正在执行 if 块。否则,子执行else块中的语句,然后父执行if块中的语句?我的思路正确吗?

Is my line of thought correct?

差不多。

fork() creates two exact copies of the processes.

Obviously, the addresses of parent and child process are different

这两种说法自相矛盾。 fork后没有"reloading"进程,所以所有地址必须相同。这是可能的,因为 CPU 的 MMU 为每个进程创建了一个不同的虚拟地址 space,这也是必须大量修改内核以 运行 的主要原因之一CPU 没有。

If the fork() returns zero, it means the parent is executing in the if block.

fork() returns parent 中的新 child 的 PID,以及 child.

中的 0