나는 "fork()"에 익숙하다. fork()가 현재 (호출 중) 프로세스의 정확한 복사본을 호출 할 때 어디서나 읽을 수있다. 다음 코드를 실행하면, 두 개의 다른 프로세스가 있어야한다. 두 개의 서로 다른 메모리 위치가 해당 변수 및 함수에 할당됩니다.fork가 사용될 때 메모리가 어떻게 매핑됩니까?
#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);
}
Why The output looks like:: child address::804a01c parent address::804a01c
왜 모두 주소는 부모뿐만 아니라 자녀에 대해 동일합니다?
그들은 * 더 좋았습니다 * 같았습니다. 포인터가 두 프로세스 모두에서 동일한 방식으로 메모리를 참조하도록합니다 (두 메모리 덩어리가 구분되는 경우에도 마찬가지입니다). * 가상 메모리 *에 대한 내용을 읽으면 질문에 대한 대답을 얻을 수 있습니다. –
두 복사본이 같은 가상 주소에 저장되어 있더라도 'i'의 * 값 *은 부모와 자식 사이에 다릅니다. – markgz