Fork - same memory addresses?

前端 未结 5 795
青春惊慌失措
青春惊慌失措 2020-12-01 11:18

This is about C in Linux.

I have fork() in main() where I create 2 child processes. Then, in both child process a run the function ab

5条回答
  •  鱼传尺愫
    2020-12-01 11:34

    You need to understand that there is a disconnect between physical memory and the virtual address space of a process.

    Every single process gets its own 4G virtual address space and it's the job of the operating systems and hardware memory managers to map your virtual addresses to physical ones.

    So, while it may seem that two processes have the same address for a variable, that's only the virtual address.

    The memory manager will map that to a totally different physical addressa.

    This mapping is also what allows you to run ten processes, each taking up 1G, even though your machine only has 4G of physical memory. The OS can swap bits of your memory out to disk and bring them back in when you try to use them.


    a: Mostly, this is true. It may map to the same physical address if you're sharing stuff between processes. For example, shared memory, kernel code and data, dynamic libraries and so forth.

提交回复
热议问题