1 / 100

Chapter 10: Virtual Memory 虚拟 存储器

Chapter 10: Virtual Memory 虚拟 存储器. Background Demand Paging ( 请求页式) Process Creation ( 进程的创建 ) Page Replacement ( 页置换) Allocation of Frames ( 页框的分配) Thrashing ( 抖动) Operating System Examples. 局部性原理.

valiant
Télécharger la présentation

Chapter 10: Virtual Memory 虚拟 存储器

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10: Virtual Memory虚拟存储器 • Background • Demand Paging (请求页式) • Process Creation (进程的创建) • Page Replacement (页置换) • Allocation of Frames (页框的分配) • Thrashing (抖动) • Operating System Examples Operating System Concepts

  2. 局部性原理 • 局部性原理(principle of locality):指程序在执行过程中的一个较短时期,所执行的指令地址和指令的操作数地址,分别局限于一定区域。具体表现在两个方面: • 时间局部性:一条指令的一次执行和下次执行,一个数据的一次访问和下次访问都集中在一个较短时期内; • 空间局部性:当前指令和邻近的几条指令,当前访问的数据和邻近的数据都集中在一个较小区域内。 Operating System Concepts

  3. 局部性原理的具体体现 • 程序在执行时,大部分是顺序执行的指令,少部分是转移和过程调用指令。 • 过程调用的嵌套深度一般不超过5,因此执行的范围不超过这组嵌套的过程。 • 程序中存在相当多的循环结构,它们由少量指令组成,而被多次执行。 • 程序中存在相当多的数据结构的操作,如数组操作,往往局限在较小范围内。 Operating System Concepts

  4. 虚拟存储器的原理 • 在程序装入时,不必将其全部读入到内存,而只需将当前需要执行的部分页或段读入到内存,就可让程序开始执行。 • 在程序执行过程中,如果需执行的指令或访问的数据尚未在内存(称为缺页或缺段),则由处理器通知操作系统将相应的页或段调入到内存,然后继续执行程序。 • 另一方面,操作系统将内存中暂时不使用的页或段调出保存在外存上,从而腾出空间存放将要装入的程序以及将要调入的页或段。这样,只需程序的一部分在内存就可执行。 1. 虚拟存储的基本原理 Operating System Concepts

  5. 2. 引入虚拟存储技术的好处 • 大程序:可在较小的可用内存中执行较大的用户程序; • 大的用户空间:提供给用户可用的虚拟内存空间通常大于物理内存(real memory) • 并发:可在内存中容纳更多程序并发执行; • 易于开发:与覆盖技术比较,不必影响编程时的程序结构 Operating System Concepts

  6. 3. 虚拟存储技术的特征 • 不连续性:物理内存分配的不连续,虚拟地址空间使用的不连续(数据段和栈段之间的空闲空间,共享段和动态链接库占用的空间) • 部分交换:与交换技术相比较,虚拟存储的调入和调出是对部分虚拟地址空间进行的; • 大空间:通过物理内存和快速外存相结合,提供大范围的虚拟地址空间 • 总容量不超过物理内存和外存交换区容量之和 Operating System Concepts

  7. 10.1 Background • Virtual memory – separation of user logical memory from physical memory. (虚拟内存—物理内存和用户逻辑内存的区分) • Only part of the program needs to be in memory for execution. (只有部分运行的程序需要在内存中) • Logical address space can therefore be much larger than physical address space. (因此,逻辑地址空间能够比物理地址空间大) • Allows address spaces to be shared by several processes.(允许若干个进程共享地址空间) • Allows for more efficient process creation. (允许更多有效进程创建) Operating System Concepts

  8. Background Cont. • Virtual memory can be implemented via: (虚拟内存能够通过以下手段来执行) • Demand paging (请求页式) • Demand segmentation (请求段式) Operating System Concepts

  9. Virtual Memory That is Larger Than Physical Memory Operating System Concepts

  10. 10.2 Demand Paging • Bring a page into memory only when it is needed (只有在一个页需要的时候才把它换入内存). • Less I/O needed(需要很少的I/O) • Less memory needed (需要很少的内存) • Faster response(快速响应) • More users(多用户) • Page is needed  reference to it (需要页) (查阅此页) • invalid reference abort (无效的访问) (中止) • not-in-memory bring to memory (不在内存) (换入内存) Operating System Concepts

  11. Transfer of a Paged Memory to Contiguous Disk Space(a paging system with swapping) Operating System Concepts

  12. Valid-Invalid Bit Frame # valid-invalid bit 1 1 1 1 0  0 0 page table • With each page table entry a valid–invalid bit is associated(1  in-memory, 0 not-in-memory) 在每一个页表的表项有一个有效- 无效位相关联,1表示在内存,0表示不内存 • Initially valid–invalid but is set to 0 on all entries. 在所有的表项,这个位被初始化为0 Operating System Concepts

  13. Valid-Invalid Bit Cont. • Example of a page table snapshot.一个页表映象的例子 • During address translation, if valid–invalid bit in page table entry is 0  page fault. 在地址转换中,如果页表表项位的值是0  (缺页) Operating System Concepts

  14. Page Table When Some Pages Are Not in Main Memory Operating System Concepts

  15. Page Fault(缺页) Steps in handling: • If there is ever a reference to a page, first reference will trap to OS  page fault trap 如果有对一个页的访问,第一个访问要陷入OS 缺页 • OS looks at another table to decide: OS查看另一个表来决定 • Invalid reference  abort. 无效引用  终止 • Just not in memory. 仅仅不在内存 Operating System Concepts

  16. Page Fault Cont. • Get empty frame(得到空的页框). • Swap page into frame(把页调入页框). • Reset tables, validation bit = 1(重新设置页表,把位设为1). • Restart instruction(重启指令): Least Recently Used (最近未使用) Operating System Concepts

  17. Steps in Handling a Page Fault Operating System Concepts

  18. Steps in Handling a Page Fault • Check an internal table for this process, to determine whether the reference was a valid or invalid memory access • If the reference was invalid, we terminate the process. If it was valid, but we have not yet brought in that page, we now page it in • We find a free frame • We schedule a disk operation to read the desired page into the newly allocated frame • When the disk read is complete, we modify the internal table kept with the process and the page table to indicate that the page is now in memory • We restart the instruction that was interrupted by the illegal address trap. The process can now access the page as though it had always been in memory Operating System Concepts

  19. Hardware to support demanding paging • Page table • Secondary memory Operating System Concepts

  20. Discuss about page fault interrupt(缺页中断) • 由处理器的地址变换机构产生缺页中断,然后调用操作系统提供的中断处理例程。 • 缺页中断的特殊性: • 缺页中断在指令执行期间产生和进行处理,而不是在一条指令执行完毕之后。所缺的页面调入之后,重新执行被中断的指令。 • 一条指令的执行可能产生多次缺页中断,如:swap A, B而指令本身和两个操作数A, B都跨越相邻外存页的分界处,则产生6次缺页中断。 Operating System Concepts

  21. Some Example(1) • Example 1. ADD A, B => C • Fetch and place the instruction (ADD) • Fetch A • Fetch B • Add A and B • Store the sum in C • 在取指令、取操作数 A和B、保存结果到C的任何一个步骤中都有可能发生缺页中断 • 此外,指令、操作数等可能跨在相邻的两个页之间 Operating System Concepts

  22. Some Example(2) • Example 2.块操作指令(如IBM 360/370 MVC 指令) • 可移动256个字节 • 可能源或目的地数据块分跨在不同页上 • 可能源和目的地之间有重叠 • Example 3. 自动增、自动减间接指令 • MOV (R2)+, -(R3) Operating System Concepts

  23. What happens if there is no free frame? • Page replacement – find some page in memory, but not really in use, swap it out 页置换—找到内存中并没有使用的一些页,换出. 需要考虑: • Algorithm(算法) • Performance(性能) – want an algorithm which will result in minimum number of page faults(找出一个导致最小缺页数的算法). • Same page may be brought into memory several times 同一个页可能会被装入内存多次 Operating System Concepts

  24. 10.2.2 Performance of Demand Paging • Page Fault Rate(缺页率) 0  p  1.0 • if p = 0 no page faults (如果p = 0 ,没有缺页) • if p = 1, every reference is a fault(每次访问都缺页) • Effective Access Time (EAT) EAT = (1 – p) x ma + p x page fault time = (1 – p) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead) Operating System Concepts

  25. Demand Paging Example • Swap page time (交换页的时间) = 25 msec = 25,000,000 nsec • a memory access time is 100nsec • EAT = (1 – p) x 100 + p (25,000,000) = 100 + 24,999,900 x p (in nsec) • 可见 EAT 与缺页率(page-fault rate)成正比 • If p=1/1000, EAT = 25us • If p=1/10000, EAT = 2.5us • If p=1/100000, EAT = 0.25us • 假设memory access time is 100nsec,为确保因缺页引起的性能下降低于10%,即:EAT<110ns, 则: • 100 + 24,999,900 x p <110  p<0.0000004 • 也即必须确保 缺页率<1/2500000 Operating System Concepts

  26. 10.3 Process Creation • Virtual memory allows other benefits during process creation:在进程创建过程中虚存存在其它的好处: - Copy-on-Write 写拷贝 - Memory-Mapped Files 内存映射文件 Operating System Concepts

  27. 10.3.1 Copy-on-Write • Fork()—creates a child process as a duplicate of its parent. • Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory. COW允许父进程和子进程在内存中初始共享相同页 • If either process modifies a shared page, only then is the page copied. 如果任一进程(父或子)需要修改一个共享页,则仅仅是该页被拷贝,随后再修改该页。 • For COW technique, • Only the pages that are modified by either process are copied • All non-modified pages be shared by the parent and child process • COW allows more efficient process creation as only modified pages are copied. COW允许进程的创建更加有效,只有在需要修改页时才会拷贝页面 • Windows 2000, Linux and Solaris 2 都采用这种技术 Operating System Concepts

  28. Copy-on-Write • Free pages are allocated from a poolof free pages,called zeroed-out pages. The technique is known as zero-filled-on-demand. (预先清零) 空闲页从一个zeroed-out页池中被分配。 • System call vfork() in Unix (for virtual memory fork()). With vfork() ,the parent process is suspended and the child process uses the address space of parent Operating System Concepts

  29. 10.3.2 Memory-Mapped Files • Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory. 通过映射磁盘块到内存中一页的方式,内存映射文件 I/O 允许文件的 I/O操作被处理成常规的存储访问。 • A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses. 一个文件开始被读使用请求页。文件的一页大小从文件系统被读入一物理的页。随后文件读 / 写处理被当作普通存储器访问。 • Simplifies file access by treating file I/O through memory rather than read()/write() system calls. 通过内存处理文件I/O可简化文件访问。这种处理方式要优于通常的read()/write()系统调用。 • Also allows several processes to map the same file allowing the pages in memory to be shared. 允许若干个进程映射到相同的文件,以此达到内存中页面被共享的目的 Operating System Concepts

  30. Memory Mapped Files Operating System Concepts

  31. 10.4 Page Replacement • Prevent over-allocation of memory by modifying page-fault service routine to include page replacement(通过修改缺页服务例程,来包含页置换,防止分配过多的内存). • Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk(通过设置修改位以减少页面传送的次数—只有被修改的页面才写入磁盘). • Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory(页置换完善了逻辑内存和物理内存的划分—在一个较小的物理内存基础之上可以提供一个大的虚拟内存 Operating System Concepts

  32. Need For Page Replacement Operating System Concepts

  33. Need For Page Replacement • 在前面的例子中, User1的第3页和User2的第1页尚未分配相应的物理内存。此时物理内存已经全部被占用。 • 此时,User1中的load M指令需要使用第3页上的数据,但是,该页并未在内存中,该如何处理? • OS 有三种处理方法: • 中止一个用户进程 • 交换出(Swap out)一个进程 • 页面置换 Operating System Concepts

  34. 10.4.1 Basic Page Replacement Steps: • Find the location of the desired page on disk.在磁盘上找到需要页的位置 • Find a free frame找到空闲的页框: - If there is a free frame, use it.(如果有一个空闲页框,使用它) - If there is no free frame, use a page replacement algorithm to select a victim frame.如果没有空闲页框,使用页代替算法选择一个牺牲(淘汰)页框。 • Read the desired page into the (newly) free frame. Update the page and frame tables.读入该页到 ( 最新 )空闲页框。更新页和页框表 • Restart the process. 重启进程 Operating System Concepts

  35. Page Replacement Operating System Concepts

  36. Page Replacement • Use modifybit (or dirty bit) to reduce overhead of page transfers – only modified pages are written to disk(通过设置修改位以减少页面传送的次数—只有被修改的页面才写入磁盘). • The bit is indicating that the page has been modified • If the bit is set, the page must be written to disk • If the bit is not set, the page has not been modified sine it was read into memory, so we can avoid writing the page to disk. • In this way ,we can reduce the time required to service a page fault Operating System Concepts

  37. Page Replacement Algorithms • Two major problems: • Frame-allocation algorithm • How many frames to allocate to each process? • Page-replacement algorithm • Which frames are to be replaced? • There are many algorithms • Goal: • Lowest page-fault rate Operating System Concepts

  38. Page Replacement Algorithms • Want lowest page-fault rate. (追求最小的缺页率) • Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string. (通过运行一个内存访问的特殊序列(访问序列),计算这个序列的缺页次数) • We only consider page number, rather than the entire address • If we have a reference to a page p, then any immediately following references to page p will never cause a page fault • In all our examples, the reference string is (在所有的例子中,访问序列是): (1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5) and ( 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 ) Operating System Concepts

  39. Graph of Page Faults Versus The Number of Frames Operating System Concepts

  40. 10.4.2 FIFO Page Replacement 缺页次数:15次 Operating System Concepts

  41. 10.4.2 FIFO Algorithm-- Belady’s Anomaly 1 1 4 5 2 2 9 page faults 1 3 3 3 2 4 1 1 5 4 2 2 10 page faults 1 5 3 3 2 4 4 3 • 在FIFO算法中,有时侯页架数的增加反而会使缺页次数增加,如下例:number of frames is 3 or 4 • Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 • 3 frames (3 pages can be in memory at a time per process) • 4 frames • FIFO Replacement – Belady’s Anomaly • more frames  less page faults Operating System Concepts

  42. FIFO Illustrating Belady’s Anamoly Operating System Concepts

  43. 10.4.3 Optimal Replacement • Replace page that will not be used for longest period of time. (被置换的页将是今后最长时间不被使用的页) 缺页次数:9次 Operating System Concepts

  44. Optimal Algorithm 1 4 2 6 page faults 3 4 5 • 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 • How do you know this? • 情况与SJF CPU-scheduling算法相似 • Used for measuring how well your algorithm performs. (用来衡量你的算法的效率) Operating System Concepts

  45. 10.4.4 LRU Page Replacement Least Recently Used (LRU) 缺页次数:12次 Operating System Concepts

  46. Least Recently Used (LRU) Algorithm 1 5 2 3 5 4 4 3 • Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 • 实现方式: (1)Counter implementation (计数器的实现) • Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. (每一个页表项 有一个计数器,每次页通过这个表项被访问,把时间拷贝到计数器中) • When a page needs to be changed, look at the counters to determine which are to change. (当一个页需要替换时,查看计数器来决定改变哪一个页。) Operating System Concepts

  47. LRU Algorithm (Cont.) (2)Stack implementation – keep a stack of page numbers in a double link form : 栈实现—在一个双链表中保留一个记录页数目的栈 • Page referenced (被访问的页) : • move it to the top when a page is referenced(移到栈顶) • requires 6 pointers to be changed 需要改变6个指针 • No search for replacement 没有为置换进行查找 • The top of the stack is always the most recently used page • The bottom is the LRU page Operating System Concepts

  48. Use Of A Stack to Record The Most Recent Page References Operating System Concepts

  49. 置换算法举例(1) 某程序在内存中分配三个页面,初始为空,页面走 向为4,3,2,1,4,3,5,4,3,2,1,5。 Operating System Concepts

  50. 置换算法举例(2) Operating System Concepts

More Related