私は "fork()" を初めて使用します。fork() が呼び出されると、現在の (呼び出し元の) プロセスの正確なコピーが開始されることをどこでも読みました。次のコードを実行すると、2 つの異なるプロセスが存在するはずです。変数と関数に割り当てられたメモリ位置。
#include<stdio.h>
int i=10;
int pid;
int main(){
if((pid=fork())==0){
i++;//somewhere I read that separate memory space for child is created when write is needed
printf("parent address= %p\n",&i);// this should return the address from parent's memory space
}else{
i++;
i++;
printf("child address= %p\n",&i);// this should return the address of child's memory space
}
wait(0);
return(0);
}
出力が次のようになる理由:: 子アドレス::804a01c 親アドレス::804a01c
親と子の両方のアドレスが同じなのはなぜですか?