How the memory is mapped when fork is used?

前端 未结 3 404
眼角桃花
眼角桃花 2021-01-18 20:03

i am new to \"fork()\",I read everywhere that when a fork() is called an exact copy of current (calling) process is started.Now when I run following code ,there should be tw

3条回答
  •  一生所求
    2021-01-18 20:08

    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.

提交回复
热议问题