#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