edition. And I came across this practice exercise (3.1). I understand why the value is 5, but when I ran the code on my machine with some modifications, I printed the address of the "value" variable, and it was the same. Why do they point to the same address if each process should have a separate copy of its own?
Each process has its own address space. The fork() syscall exactly duplicates file descriptors, credentials, namespace, cgroup settings, MM (this includes the pointer you showed, so yes, the pointer is duplicated too), and many more. What you see here is a virtual address. The same virtual address in a different process occupies a different physical address, (assuming we ignore the COW optimization on fork). But the point is the address space is private to each process. A process can't clobber other processes' addresses unless explicitly shared (e.g., using mmap + MAP_ANONYMOUS | MAP_SHARED before fork()).
Yeah, I also thought it had something to do with virtual memory. Thanks!
Обсуждают сегодня