Good day.
I am trying to understand the structure of the mechanisms of processes, in particular with variables. And the question arose.
There is the following code:

pid_t pid1; int i = 4; int *pointer = &i; void main () { pid1 = fork(); if(pid1 == 0) { i=i+2; pointer = &i; printf("child : pointer adress %p : i adress %p : pointer_value %d\n", &pointer, &i, *pointer); exit(0); } if(pid1 > 0) { wait(0); printf("parent: pointer adress %p : i adress %p : pointer_value %d\n", &pointer, &i, *pointer); } return; } 

The output gives:

 child : pointer adress 0x601060 : i adress 0x601058 : pointer_value 6 parent: pointer adress 0x601060 : i adress 0x601058 : pointer_value 4 

Then why are pointer_values ​​different if the memory areas they point to are the same?

1 answer 1

Each process has its own virtual address space.

Two identical addresses in different processes are mapped to different physical RAM addresses, therefore their contents are different.


A more in-depth introduction to this topic can be started with an article.
virtual memory in vetch.