简体   繁体   English

虚拟内存地址转换

[英]Virtual memory address translation

I was reading about how Virtual Memory and Memory management works in an Operating System. 我正在阅读有关虚拟内存内存管理如何在操作系统中工作的信息。 I understood how each process has its own contiguous logical address space. 我了解每个进程如何具有自己的连续逻辑地址空间。 This address space need not be contiguous in physical memory. 该地址空间不需要在物理存储器中连续。 For this purpose, paging is used. 为此,使用分页。 The page tables are used to do the mapping from logical address to a physical address. 页表用于执行从逻辑地址到物理地址的映射。 The logical address is divided in two parts, first part gives the logical page number, which using the page table is translated to the physical page number, and the second part is offset into that page. 逻辑地址分为两部分,第一部分给出逻辑页码,使用页表转换为物理页码,第二部分偏移到该页。 Thus the contents from memory are retrieved. 因此,检索来自存储器的内容。
Virtual memory is an extension of this, where all the pages need not be in main memory, and can be brought there through page faults. 虚拟内存是其中的扩展,其中所有页面不需要在主内存中,并且可以通过页面错误带到那里。

Using my understanding I solved the 4th question on this page: www.ics.uci.edu/~bic/courses/JaverOS/ch8-ex.pdf 根据我的理解,我在本页解决了第四个问题: www.ics.uci.edu/~bic/courses/JaverOS/ch8-ex.pdf

I got my answers wrong, and I have no clue what the right answers are. 我的答案是错误的,我不知道正确的答案是什么。 This is the way I did it: 这就是我这样做的方式:

From the diagram in the question, Page table for Process P1 will look like this according to me:
0-4
1-5
2-6
3-7

So when process P1 makes a reference to 0 i.e is 0000 in 4 bit binary, we divide 
it as 00|00. 
Thus logical page no = 00 and offset = 00.
From the page table, we can see 0 is mapped to 4th physical frame. 
Offset is also 00 here. So I get the 0th entry(offset) in the 4th frame. 
The content at this memory location(i.e Frame No. 4, offset 0) is 0.

Why is this wrong? 为什么这是错的? Can anyone help? 有人可以帮忙吗?

I'll start with virtual address 8, it should illustrate things better. 我将从虚拟地址8开始,它应该更好地说明事情。

8 = 2×4 + 0. The physical address is stored at offset 0 of page 2 of the process. 8 = 2×4 + 0.物理地址存储在过程第2页的偏移0处。 We look up the second page. 我们查找第二页。 For P1, page 2's address is stored at PA 4 + 2, and it is 28. We look up the contents of PA 28, and get value 0. For P2, page 2's address is stored at PA 12 + 2, it is 24, the contents of PA 24 are 5. 对于P1,第2页的地址存储在PA 4 + 2,它是28.我们查找PA 28的内容,得到值0.对于P2,第2页的地址存储在PA 12 + 2,它是24 ,PA 24的内容为5。

Now with virtual address 15, which illustrates the exceptional cases. 现在使用虚拟地址15,它说明了特殊情况。

15 = 3×4 + 3. The physical address is stored at offset 3 of the process's page 3. For P1, page 3's address is stored at PA 4 + 3, which contains -2. 15 = 3×4 + 3.物理地址存储在过程页面3的偏移3处。对于P1,第3页的地址存储在PA 4 + 3,其中包含-2。 The sign bit indicates the page is outside physical memory, which means there will be a hard page fault (a situation the OS handles exceptionally, but not an error). 符号位表示页面位于物理内存之外,这意味着将出现硬页面错误(操作系统处理异常情况,但不是错误)。 For P2, page 3's address is stored at PA 12 + 3, which some special mechanism (a poison value, an external frame table?) tells us is invalid. 对于P2,第3页的地址存储在PA 12 + 3,其中一些特殊机制(毒性值,外部帧表?)告诉我们无效。 This is an error, and gets reported as a segmentation fault. 这是一个错误,并报告为分段错误。

One last example with VA 7 and P1 to explain offsets: VA 7和P1的最后一个例子来解释偏移量:

7 = 1×4 + 3. The physical address is stored at offset 3 of P1's page 1. Page 1 is at PA 8, offset 3 of page 1 is at PA 8+3. 7 = 1×4 + 3.物理地址存储在P1第1页的偏移3处。页面1位于PA 8,第1页的偏移3位于PA 8 + 3。

I'm assuming the diagram shows the physical memory and physical addresses on the left. 我假设该图在左侧显示了物理内存和物理地址。 It wouldn't make sense to let p1 and p2 directly access their own and each other's page tables. 让p1和p2直接访问他们自己和彼此的页表是没有意义的。

Given that, for virtual address 0 you fetch -45 from page frame 1 for p1 and 0 from page frame 3 for p2. 鉴于此,对于虚拟地址0,您从p1的页面帧1获取-45,对于p2从页面帧3获取0。

For VA = 1 you fetch 8 and -12 respectively. 对于VA = 1,您分别获取8和-12。

For VA = 4 you fetch nothing because that's outside of the defined page tables for p1 and p2 (that is how I interpret "The size of each page and page table is 4" and "The page table of p1 begins at address 4; the page table of p2 begins at address 12."; if my interpretation is wrong, then the problem statement is incorrect as well). 对于VA = 4,你什么都不拿,因为它在p1和p2的定义页表之外(这就是我解释“每页和页表的大小是4”和“p1的页表从地址4开始;” p2的页表从地址12开始。“;如果我的解释错误,那么问题陈述也是错误的)。 Ditto for the rest of the VAs because they're greater than or equal to 4 and fall outside the defined page tables too. 其余的VAs同样如此,因为它们大于或等于4并且也超出了定义的页表。

You can then work out the final answers from here. 然后,您可以从这里找出最终答案。

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

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