且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么子进程和父进程的变量的地址相同

更新时间:2022-06-22 04:23:46

由于这是一个的虚拟的地址,而不是物理上的。

Because it's a virtual address, not a physical one.

每个过程获得它自己的地址空间(例如,一个32位的系统可以允许每个进程具有与全4G范围它自己的地址空间)。

Each process gets its own address space (for example, a 32-bit system may allow each process to have its own address space with the full 4G range).

这是内存管理单元(如果换出的页面需要在从辅助存储器买回处理事情就像缺页),将虚拟地址映射到物理的。

It's the memory management unit that will map virtual addresses to physical ones (and handle things like page faults if swapped out pages need to be bought back in from secondary storage).

下面的图表可以帮助,每节重presenting内存4K块:

The following diagram may help, each section representing a 4K block of memory:

   Process A           Physical Memory      Process B
   +-------+           +-------------+      +-------+
0K |       |---->   0K |  (shared)   | <----|       | 0K
   +-------+           +-------------+      +-------+
4K |       |--+     4K |             | <----|       | 4K
   +-------+  |        +-------------+      +-------+
8K |       |  +->   8K |             |      |       | 8K
   +-------+           +-------------+      +-------+
       |                : : : : : : :           |
       |               +-------------+          |
       |          128K |             | <--------+
       |               +-------------+
       +--------> 132K |             |
                       +-------------+

您可以看到,图中,虚拟内存地址和物理内存地址之间的脱节(流程和可能性共享内存块以及)。走在左,右两侧的地址是该过程看到虚拟地址。

You can see, in that diagram, the disconnect between virtual memory addresses and physical memory addresses (and the possibility for processes to share memory blocks as well). The addresses down the left and right sides are virtual addresses which the processes see.

在中间块的地址是在数据真的是实际的物理地址,以及MMU处理的映射。

The addresses in the central block are actual physical addresses where the data "really" is, and the MMU handles the mapping.

有关叉有了更深的解释(和 EXEC ),您可能也想看看的这个答案

For a deeper explanation of fork (and exec), you may also want to look at this answer.