#include<stdio.h>---->(1)
int main()
{
int i;
int j,K;
j = 200;
K = 300;
printf("Before forking: j = %d, K = %d\n ", j, K);---->(2)
i = fork();---->(3) /* Calling fork() */
if (i > 0) { /* Parent Process */ ----->(4)
sleep(10);/* Delay the parent */
printf("After forking, parent: j = %d, K = %d\n", j, K);
} ----->(5)
else {----->(6) /* Child process */
j++;
K++;
printf("After forking, child: j = %d, K = %d\n", j, K);
}---->(7)
o/p of the above pgm is
Before forking: j = 200, K = 300
After forking, child: j = 201, K = 301
After forking, parent: j = 200, K = 300.
由于我是IPC的初学者,所以在回答问题之前,我想确保我的理解是正确的。行#(1)行(2)&行(4)行5构成父行过程&行(6)到行(7)构成了子进程,任何高于fork()的行,即从行(1)到行(2)都被复制到子地址空间。我正确地对上述点?那么为什么第(2)行不打印两次,一个在子进程中,另一个在父进程中打印。为什么这行不会被复制到子进程的地址空间中?
>然后,说孩子得到父母的地址空间的副本是什么意思.line no 2在父母的地址空间是正确的? – 2013-03-27 11:55:53
@SubiPuthalath:是的,但该行已被执行。孩子在* fork()后继续*。 – RichieHindle 2013-03-27 12:01:36
>好吧,我得到了。所以你的意思是说只有变量的值(可以在堆中或栈中)才会被复制到子地址空间。 – 2013-03-27 12:11:13