简体   繁体   English

为什么虚拟内存地址在不同的进程中是相同的?

[英]Why Virtual Memory Address is the same in different process?

http://i.stack.imgur.com/RGMsz.png

I know the virtual memory separates 2G(for kernel)/2G(for user) in Windows. 我知道虚拟内存在Windows中分离2G(用于内核)/ 2G(用户)。 But why the address of variable isn't stack continually? 但为什么变量的地址不是连续堆栈?

Likes 0x22ff74,0x22ff78,0x22ff82,0x22ff86 ? 喜欢0x22ff74,0x22ff78,0x22ff82,0x22ff86? Does it mean that Windows use sandbox mechanism in user process? 这是否意味着Windows在用户进程中使用沙盒机制?

That's exactly what virtual memory is. 这正是虚拟内存的本质。 The operating system provides each program with its own private address space. 操作系统为每个程序提供自己的专用地址空间。 In reality the operating system is in charge of mapping those virtual addresses back to the physical address space without the application being aware. 实际上,操作系统负责将这些虚拟地址映射回物理地址空间而不需要应用程序知晓。

As you noticed this means that two applications can have different data residing at the same virtual address in the program. 正如您所注意到的,这意味着两个应用程序可以在程序中的同一虚拟地址中拥有不同的数据。

Read more about virtual memory here . 了解更多关于虚拟内存的位置

Are you confusing physical addresses and virtual addresses? 您是否混淆了物理地址和虚拟地址? It's ok for two processes to access the same virtual address, because each process see its own virtual memory space. 两个进程可以访问同一个虚拟地址,因为每个进程都可以看到自己的虚拟内存空间。 On the other hand, all processes share the same physical memory space in the machine, so each process will have that same virtual address mapped to a different physical address (assuming there is no shared memory). 另一方面,所有进程在机器中共享相同的物理内存空间,因此每个进程将具有映射到不同物理地址的相同虚拟地址(假设没有共享内存)。

Let me explicate it another way. 让我以另一种方式阐述它。 Process X is running on machine A, and the same program is running as process Y on machine B. Does it matter if some global variable of your program takes same memory address on both machines? 进程X在机器A上运行,同一程序在机器B上作为进程Y运行。如果程序的某个全局变量在两台机器上占用相同的内存地址,这是否重要? They are different! 他们是不同的! The same way, if that global variable is stored at XYZ location for one instance of process, another instance of process may have the same virtual address (XYZ) for that global variable. 同样,如果该全局变量存储在一个进程实例的XYZ位置,则另一个进程实例可能具有该全局变量的相同虚拟地址 (XYZ)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM