
操作系统 (1)ch09-virtual memory.pdf
78页LI Wensheng, SCST, BUPT Chapter 9 Virtual Memory Teaching hours: 4h Strong point: Demand Paging Page Replacement Thrashing Wensheng Li BUPT 2 contents 9.1 Background 9.2 Demand Paging 9.3 Copy-on-write 9.4 Page Replacement 9.5 Allocation of Frames 9.6 Thrashing 9.7 Memory-mapped file* 9.8 Allocating Kernel Memory* 9.9 Other considerations* 9.10 Operating-System Examples* Wensheng Li BUPT 3 Objectives To describe the benefits of a virtual memory system To explain the concepts of demand paging, page- replacement algorithms, and allocation of page frames To discuss the principle of the working-set model Wensheng Li BUPT 4 9.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. Virtual memory can be implemented via: – Demand paging – Demand segmentation Wensheng Li BUPT 5 Virtual Memory is Larger Than Physical Memory Wensheng Li BUPT 6 Virtual-address Space Physical memory may be organized in page frames, and the physical page frames assigned to a process may not be contiguous. It is up to the MMU to map logical pages to physical page frames in memory. The large blank space between the heap and the stack is part of the virtual address space, but will require actual physical pages only if the heap or stack grows. Wensheng Li BUPT 7 Shared Library Using Virtual Memory Wensheng Li BUPT 8 9.2 Demand Paging Bring a page into memory only when it is needed. Benefits of demand paging – Less I/O needed – Less memory needed – Faster response – More users Page is needed reference to it – invalid reference abort – not-in-memory bring it into memory Lazy swapper – never swaps a page into memory unless page will be needed – Swapper that deals with pages is a pager Wensheng Li BUPT 9 Swapper: Transfer of a Paged Memory to Contiguous Disk Space Wensheng Li BUPT 10 Valid-Invalid Bit With each page table entry a valid–invalid bit is associated (1 in-memory, 0 not-in-memory) Initially valid–invalid bit is set to 0 on all entries. Example of a page table snapshot. During address translation, if valid–invalid bit in page table entry is 0 page fault(缺页)(缺页) Frame# valid-invalid bit Page table 1 1 0 1 0 1 0 0 0 1 2 3 4 5 6 7 Wensheng Li BUPT 11 Pager: lazy swapper Page Table When Some Pages Are Not in Main Memory A B C D E F G H logical memory 0 1 2 3 4 5 6 7 4 1 0 6 1 0 0 9 1 0 0 Frame# valid-invalid bit 0 1 2 3 4 5 6 7 page table A C F 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 physical memory C F A D B E Wensheng Li BUPT 12 Page Fault If there is ever a reference to a page, first reference will trap to OS page fault OS looks at another internal table to decide: – Invalid reference abort. – Just not in memory. Get empty frame. Swap page into frame. Reset tables, validation bit = 1. Restart instruction Wensheng Li BUPT 13 Steps in Handling a Page Fault load M page table 0 OS Free frame physical memory reference 1 trap 2 3 page is on backing store 4 bring in missing page 5 reset page table 6 restart instruction 1 Pure demand paging e.g. A+B C Wensheng Li BUPT 14 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 pages may be brought into memory several times. Wensheng Li BUPT 15 Performance of Demand Paging Page Fault Rate 0 p 1.0 – if p = 0, no page faults – if p = 1, every reference is a fault Effective Access Time (EAT) EAT = (1– p) memory access time + p page fault service time page fault service time = page fault overhead + [ swap page out ] + swap page in + restart overhead Wensheng Li BUPT 16 Demand Paging Example Memory access time = 200 nanoseconds average page-fault service time 8 milliseconds – servicing the page-fault interrupt and restarting the process may be reduced to several hundred instructions, and each may take from 1 to 100 microseconds. – The page-switch time will probably be close to 8 milliseconds • Hard disk has an average latency of 3 milliseconds; • hard disk has a seek of 5 milliseconds; • transfer time of 0.05 milliseconds. Effective Access Time EAT = (1 – p) 200 + p (8 000 000) = 200 + 7 999 800 p Wensheng Li BUPT 17 9.3 Copy-on-Write—process creation Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory If either process modifies a shared page, only then is the page copied. COW allows more efficient process creation as only modified pages are copied. (see P.326 Figure9.7-9.8) Free pages are allocated from a pool of zeroed-out pages. Wensheng Li BUPT 18 9.4 Page Repl。
