How the memory is mapped when fork is used?

本秂侑毒 提交于 2019-12-01 18:14:17

having two different memory locations assigned to their vars and functions.

Nope; Linux implements virtual memory, meaning that each process has its own complete address space. As a result, after a fork, both processes see the same addresses for their copies of in-memory objects.

(As an aside: VM also causes code to be shared between the process in physical memory, and all data will only be copied-on-write.)

Addresses are process-local. 804a01c in one process is not the same as 804a01c in another process.

Because of virtual memory: both address spaces look identical to the respective processes. The physical memory those are stored in is different. However that is, in practice, complicated by a memory optimization (implemented by most kernels) which maps the corresponding different virtual pages to the same physical pages until one of those processes writes to that page of memory, at which time the page is duplicated physically to another physical address (and the virtual page is remapped for the process).

There are many other complications as well: The most recognized is that the return value of fork() differs between processes, though that is usually a difference in a register value, not memory. However, open files, and some other resources can be marked un-inheritable, so there could be other differences—minor but sometimes useful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!