简体   繁体   English

几乎连续的与物理上连续的记忆

[英]Virtually contiguous vs. physically contiguous memory

Is virtually contiguous memory also always physically contiguous? 几乎连续的记忆也总是在物理上连续吗? If not, how is virtually continuous memory allocated and memory-mapped over physically non-contiguous RAM blocks? 如果没有,如何在物理上非连续的RAM块上分配几乎连续的内存并进行内存映射? A detailed answer is appreciated. 详细的答案表示赞赏。

Short answer: You need not care (unless you're a kernel/driver developer). 简短回答:您无需关心(除非您是内核/驱动程序开发人员)。 It is all the same to you. 对你来说都是一样的。

Longer answer: On the contrary, virtually contiguous memory is usually not physically contiguous (only in very small amounts). 更长的答案:相反,几乎连续的记忆通常不是物理上连续的(只是非常少量)。 Except by coincidence, or shortly after the machine has just booted. 除了巧合,或机器刚刚启动后不久。 That isn't necessary, however. 但是,这不是必要的。

The only way of allocating larger amounts of physically contiguous RAM is by using large pages (since the memory within one page needs to be contiguous). 分配大量物理连续RAM的唯一方法是使用大页面(因为一页内的内存需要是连续的)。 It is however a useless endeavor, since there is no observable difference for your process whether memory of which you think that it is contiguous is actually contiguous, but there are disadvantages to using large pages. 然而,这是一个无用的尝试,因为您的过程没有可观察到的差异,您认为它是连续的内存是否实际上是连续的,但使用大页面存在缺点。

Memory mapping over phyically non-contiuous RAM works in no particularly "special" way. 在物理上非连续的RAM上的存储器映射不是特别“特殊”的方式。 It follows the same method which all memory management follows. 它遵循所有内存管理遵循的相同方法。

The OS divides virtual memory in "pages" and creates page table entries for your process. 操作系统在“页面”中划分虚拟内存,并为您的进程创建页表条目。 When you access a memory in some location, either the corresponding page does not exist at all, or it exists and corresponds to a real page in RAM, or it exists but doesn't correspond to a real page in RAM. 当您访问某个位置的内存时,相应的页面根本不存在,或者它存在并对应于RAM中的实际页面,或者它存在但与RAM中的实际页面对应。

If the page exists in RAM, nothing happens at all 1 . 如果页面存在于RAM中,则根本不会发生任何事情1 Otherwise a fault is generated and some operating system code is run. 否则会生成故障并运行某些操作系统代码。 If it turns out the page doesn't exist at all (or does not have the correct access rights), your process is killed with a segmentation fault. 如果事实证明页面根本不存在(或者没有正确的访问权限),则您的进程会因分段错误而终止。

Otherwise, the OS chooses an arbitrary page that isn't used (or it swaps out the one it thinks is the least important one), and loads the data from disk into that page. 否则,操作系统会选择一个未使用的任意页面(或者交换掉它认为最不重要的页面),然后将数据从磁盘加载到该页面中。 In the case of a memory mapping, the data comes from the mapped file, otherwise it comes from swap (and for completely new allocated memory, the zero page is copied). 在内存映射的情况下,数据来自映射文件,否则它来自交换(对于全新分配的内存,复制零页)。 The OS then returns control back to your process. 然后操作系统将控制权返回给您的流程。 You never know this happened. 你永远不会知道这件事。

If you access another location in a "contiguous" (or so you think!) memory area which lies in a different page, the exact same procedure runs. 如果您访问位于不同页面的“连续”(或者您认为!)内存区域中的另一个位置,则会运行完全相同的过程。


1 In reality, it is a little more complicated, since a page may exist in RAM but not exist "officially", being part of a list of pages that are to be recycled or such. 1实际上,它有点复杂,因为页面可能存在于RAM中但不是“正式”存在,是要回收的页面列表的一部分。 But this gets too complicated. 但这太复杂了。

No, it doesn't have to. 不,它不必。 Any page of the virtual memory can be mapped to an arbitrary physical page. 虚拟内存的任何页面都可以映射到任意物理页面。 Therefore you can have adjacent pages of your virtual memory pointing to non-adjacent physical pages. 因此,您可以让虚拟内存的相邻页面指向不相邻的物理页面。 This mapping is maintained by the OS and is used by the MMU unit of CPU. 该映射由OS维护,并由CPU的MMU单元使用。

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

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