Why does fork () use the same variable but have a different value?

Here is the code:

#include <stdio.h> #include <unistd.h> void f(int&); void g(int&); int main(int argc, char **argv) { printf("--beginning of program\n"); int counter = 0; pid_t pid = fork(); if (pid == 0) { f(counter); printf("child process: %d, %p", counter, &counter); } else if (pid>0) { g(counter); for (int i=0; i<5; ++i) { sleep(1); printf("parent process: %d, %p\n", counter, &counter); } } printf("--end of program--\n"); return 0; } void f(int& counter) { counter = 1; printf("in f: %d, %p-\n", counter, &counter); } void g(int& counter){ } 

and here is the result:

 --beginning of program in f: 1, 0x7ffc9b01c6a4- child process: 1, 0x7ffc9b01c6a4--end of program-- parent process: 0, 0x7ffc9b01c6a4 parent process: 0, 0x7ffc9b01c6a4 parent process: 0, 0x7ffc9b01c6a4 parent process: 0, 0x7ffc9b01c6a4 parent process: 0, 0x7ffc9b01c6a4 --end of program-- 

Clearly, in the child process, this is the same parameter with the same address, but the value is different.

Why is this happening?

+5
source share
2 answers

Each process has its own virtual memory space.

This means that 0x7ffc9b01c6a4 in one process is not completely associated with 0x7ffc9b01c6a4 in another.

This is not the same object; this is an object in a new process. Since the second process was bifurcated from the first, essentially cloning the process, it is not surprising that objects should exist in the same place of virtual memory in the second, since they were in the first.

+10
source

This is the core of fork() , and this is exactly what allows it to work. To understand this, you need to remember that the entire address space of the process in modern operating systems is virtual - this means that it has nothing to do with the actual address memory of the physical memory. Gone are the days when access to memory at 0x8000 (if I remember the address correctly) went directly to video memory. I used to program this way, and instead of screen manipulation routines, the values ​​in the video memory were simply written, which was much faster. It was fun:)

But this is no longer the case. Now in user programs, the address has nothing to do with physical memory, and whenever you access memory in locaiton '0x1234567', the translation is done. The CPU knows how to map this virtual address to the address of the physical memory, but no one does.

So, when you fork your process, an exact copy of the memory is created. It has the same virtual addresses (since the memory replica is accurate!). But since this is now a process of difference, the processor will translate these virtual addresses to another addressing of the physical memory. At least that's semantics. In real modern systems, exact copying of memory does not actually occur. or fork() will take too long. Instead, the memory is marked as "copy-on-write". This means that until the data is changed, two processes will access the same physical memory. But as soon as any process changes memory, it will be actually copied, and now everyone will have their own copy.

+3
source

Source: https://habr.com/ru/post/1232187/


All Articles