《操作系统精髓与设计原理》网站上的演示——虚拟内存[0]

1.PagingTLB

The TLB contains only a few of the page-table entries.When a logical address is generated by the CPU, its page number is presented to the TLB. If the page number is found, its frame number is immediately available and is used to access memory. If hte page number is immediately available and is used to access memory. If the page number is not in the TLB(known as a TLB miss), a memory reference to the page table must be made. When the frame number is obtained, we can use it to access memory. In addition, we add the page number and frame number to the TLB, so that they will be found quickly on the next reference.

TLB仅包含少数的页表项,当CPU给出一个逻辑地址,这个逻辑地址中的页号就被送去TLB做比较,如果这页在TLB中,与之相对应的页框号可以立即被找到,用这个页框号去访问内存。如果页号在TLB中没找到,就要访问一次内存去寻找。当页框号找到了,就用它来访问内存。并且,将找到的页号和页框号添加到TLB中,这样在下次的寻找中可以快速的找到。

下面是一个演示的例子: http://cs.uttyler.edu/Faculty/Rainwater/COSC3355/Animations/pagingtlb.htm 。

The CPU produces a reference to the letter g, or logical address 0102.

CPU给出了一个字母g的逻辑地址0102.

The TLB is checked, and the page number 01 is found.

检查TLB中是否有页号01,结果发现了01页号。

The frame number from the TLB is then used to construct the physical mumory address for memory access.

来自TLB的页框号现在被用作构造物理内存地址来访问内存。

The CPU produces a reference to the letter p, or logical address 0303.

The TLB is checked, and the page number 03 is not found.

A momory access must now be made to the page table to obtain the frame number.

现在必须访问内存来查找页表找到页框号。

The frame number form the page table is then used to construct the physical memory address for memory access.

In addition, we add the page number and the frame number to the TLB so that they will be found quickly on the next reference.

这个例子完整的演示了CPU给出一个逻辑地址,然后再TLB中寻找,有和无的状况分别的处理。若有,则提取相应的TLB项,得到对应的物理页框号,用这个物理页框号和虚拟地址中的偏移量构成物理地址,由于进程的页和物理页框的大小是相同的,所以如果通过页号找到了页框号,那么只要加上偏移就可以还原出物理地址。这是一个基于分页的虚拟地址方案。

虚拟地址由页号和偏移量构成,物理地址由页框号和偏移量构成,页表项是由从页号到页框号的映射构成,并且页表项应该还有其他控制位,如P表示是否在内存中,M位表示是否被修改过。通过一个页表指针的寄存器(这个寄存器保存了页表的首地址,通过页号*页表项大小可以获得当前页表项的位置,然后得到页框号,页框号也就是整个物理内存的被分成的页框,通过页框号*页框大小应该可以获得这块内存页框首部的具体位置,然后通过CPU给出的逻辑地址中的偏移量完整的获得物理地址。那么TLB的作用显而易见,他是一个对内存的高速缓存,先查找缓存,缓存有则加快访问速度,没有则再去访问内存。

《操作系统精髓与设计原理》书上又讲:在大多数的系统中,每个进程都有一个页表。但是每个进程可以占据大量的虚存空间,如果使用2^9=512B的页,就意味着每个进程需要有2^22个表项。为了克服这个占主存过大的问题,将大多数页表保存在虚拟内存中,这意味着页表页遵循了其他页的分页管理。这样,如果进程的一部分在内存中,而当需要不在内存中的那些页表的时候,先通过这个进程在内存中的指向其他剩余页表的页表项找到装载这些剩余页表项的页框号,将这些页表载入内存,然后访问这些在内存中的页表,找到进程的剩余部分的页号,再找到页框号,来构造物理地址,访问内存,将进程需要的内容调入内存运行。

2.Two Level Paging

In a two level page table, the page table itself is divided into pages.

在一个二级页表中,页表本身就被分成页。

这个例子在  http://cs.uttyler.edu/Faculty/Rainwater/COSC3355/Animations/twolevelpaging.htm 。

A logical address is divided into an outer page number(P1), a page displacement within the page of the outer page(P2), and a page offset(d).

现在将逻辑地址分为目录页(P1)(outer page)、目录页的偏移(P2)和页的偏移(d)。

For example, if the CPU produces logical address 010340, it will reference outer page 01, which points to page 1 in the page table. Then we access offset 03 within the page to obtain frame number 1100. We then use the offset to produce physical memory address 1140.

例如,如果CPU产生一个010340的逻辑地址,首先将会找到页目录01,而页目录01指向了页表中的01页,然后又通过03确定在页表中的偏移量,找到了具体给出页框号的页号的页表项。(注意,这个相当于没有外层目录的页表,多的只是一个外层目录)。找到了页狂号,那么最后根据40的偏移找到了实际的物理地址。

If the CPU produces logical address 000100, it will reference outer page 00, which points to page 0 in the page table. Then we access offset 01 within the page to obtain frame number 200. We then use the offset to produce physical memory address 200.

3.Virtual Memory

Virtual momory is the separation of user logical memory  from physical memory. This separation allows an extremely large virtual memory to be provided for programmers when only a smaller physical memory is available.

虚拟内存是从逻辑内存分离出来的。单一个较小的物理内存可用时,这种分离允许为程序员拓展更大的虚拟内存。

Only a portion of the pages available in virtual memory can be loaded in the physical(main) memory at the same time.

在一时刻虚拟内存中只有一部分页在物理内存中。

Those pages in virtual memory that do not currently reside in physical (main) memory may reside on a local disk or other medium.

这些在虚拟内存中的页目前不再物理内存上可能在磁盘上。

Pages may be swapped from main memory to the disk and viceversa as needed.

页可能从住处交换到磁盘上反之亦然。

4.Demand Paging

When a process is to be swapped in, the pager guess which pages will be used before the process is swapped out agin.Instead of swapping in a whole process, the pager brings only those necessary pages into memory. Thus, it avoids reading into memory pages that will not be used anyway, decreasing(减少) the swap time and the amount(量) of physical memory needed. We begin with process A's pages in memory and process B's pages on disk in locations 17-19.

The pager determines(决定) that Process B's pages on disk locations 17-19 should be brought into main memory.

The pager then determines that Process A's pages in main memory should be put back on the disk.

By swapping pages to and from the disk as needed, a demand paging system can keep in main memory only those pages that are or will soon be needed.

6.Second Chance (clock) Page Replacement Algorithm

5.The Steps In Handling A Page Fault

If the invalid bit in the page table is set due to the page not currently being in physical memory, the procedure for handling the page fault is straightforward.

1) We check an internal table(usually kept with the process control block) for this process, to determine whether the reference(引用) was a valid or invalid memory access.

2)The reference was invalid because the page is currently on the backing store. A trap to the OS (page-fault trap)is generated.

3)The page is on the backing store . We find a free frame(by taking one from the free-frame list, for example).

4)We schedule a disk operation to read the desired page into the newly allocated frame.

5)When the disk read is complete, we modify the internal table kept with the process and the page table to indicate that page is now in memory.

6)We restart the instruction that was interrupted by the trap. The process can now access the page as though it had always been in memory.

6.Page Replacement

Page replacement takes the following approach: Find the location of the desired page on the disk..Find a free frame. If there is a free frame, use it, In our case, there is no free frame. We must use a page-replacement algorithm to select a victim frame.Write the victim page to the disk and change the page and frame tables accordingly.

Read the desired page into the (newly) free frame, and change the page and frame tables..Restart the user process.

原文地址:https://www.cnblogs.com/changme/p/4039857.html