Chapter 9: Virtual Memory Chapter 9: Virtual Memory Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel Memory Other Considerations Operating-System Examples Operating System Concepts – 7th Edition, Feb 22, 2005 9.2 Silberschatz, Galvin and Gagne ©2005 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 Operating System Concepts – 7th Edition, Feb 22, 2005 9.3 Silberschatz, Galvin and Gagne ©2005 Background Virtual memory – separation of user logical memory from physical memory z Only part of the program needs to be in memory for execution z Logical address space can therefore be much larger than physical address space z Allows address spaces to be shared by several processes z Allows for more efficient process creation Virtual memory can be implemented via: z Demand paging z Demand segmentation Operating System Concepts – 7th Edition, Feb 22, 2005 9.4 Silberschatz, Galvin and Gagne ©2005 Virtual Memory That is Larger Than Physical Memory ⇒ Operating System Concepts – 7th Edition, Feb 22, 2005 9.5 Silberschatz, Galvin and Gagne ©2005 Virtual-address Space Operating System Concepts – 7th Edition, Feb 22, 2005 9.6 Silberschatz, Galvin and Gagne ©2005 Shared Library Using Virtual Memory Operating System Concepts – 7th Edition, Feb 22, 2005 9.7 Silberschatz, Galvin and Gagne ©2005 Demand Paging Bring a page into memory only when it is needed z Less I/O needed z Less memory needed z Faster response z More users Page is needed ⇒ reference to it z invalid reference ⇒ abort z not-in-memory ⇒ bring to memory Lazy swapper – never swaps a page into memory unless page will be needed z Swapper that deals with pages is a pager Operating System Concepts – 7th Edition, Feb 22, 2005 9.8 Silberschatz, Galvin and Gagne ©2005 Transfer of a Paged Memory to Contiguous Disk Space Operating System Concepts – 7th Edition, Feb 22, 2005 9.9 Silberschatz, Galvin and Gagne ©2005 Valid-Invalid Bit With each page table entry a valid–invalid bit is associated (v ⇒ in-memory, i ⇒ not-in-memory) Initially valid–invalid bit is set to i on all entries Example of a page table snapshot: Frame # valid-invalid bit v v v v i … i i page table During address translation, if valid–invalid bit in page table entry is I ⇒ page fault Operating System Concepts – 7th Edition, Feb 22, 2005 9.10 Silberschatz, Galvin and Gagne ©2005 Buddy System Allocates memory from fixed-size segment consisting of physically- contiguous pages Memory allocated using power-of-2 allocator z Satisfies requests in units sized as power of z Request rounded up to next highest power of z When smaller allocation needed than is available, current chunk split into two buddies of next-lower power of Continue until appropriate sized chunk available Operating System Concepts – 7th Edition, Feb 22, 2005 9.56 Silberschatz, Galvin and Gagne ©2005 Buddy System Allocator Operating System Concepts – 7th Edition, Feb 22, 2005 9.57 Silberschatz, Galvin and Gagne ©2005 Slab Allocator Alternate strategy Slab is one or more physically contiguous pages Cache consists of one or more slabs Single cache for each unique kernel data structure z Each cache filled with objects – instantiations of the data structure When cache created, filled with objects marked as free When structures stored, objects marked as used If slab is full of used objects, next object allocated from empty slab z If no empty slabs, new slab allocated Benefits include no fragmentation, fast memory request satisfaction Operating System Concepts – 7th Edition, Feb 22, 2005 9.58 Silberschatz, Galvin and Gagne ©2005 Slab Allocation Operating System Concepts – 7th Edition, Feb 22, 2005 9.59 Silberschatz, Galvin and Gagne ©2005 Other Issues Prepaging Prepaging z To reduce the large number of page faults that occurs at process startup z Prepage all or some of the pages a process will need, before they are referenced z But if prepaged pages are unused, I/O and memory was wasted z Assume s pages are prepaged and α of the pages is used cost of s * α save pages faults > or < than the cost of prepaging s * (1- α) unnecessary pages? Is α near zero ⇒ prepaging loses Operating System Concepts – 7th Edition, Feb 22, 2005 9.60 Silberschatz, Galvin and Gagne ©2005 Other Issues – Page Size Page size selection must take into consideration: z fragmentation z table size z I/O overhead z locality Operating System Concepts – 7th Edition, Feb 22, 2005 9.61 Silberschatz, Galvin and Gagne ©2005 Other Issues – TLB Reach TLB Reach - The amount of memory accessible from the TLB TLB Reach = (TLB Size) X (Page Size) Ideally, the working set of each process is stored in the TLB z Otherwise there is a high degree of page faults Increase the Page Size z This may lead to an increase in fragmentation as not all applications require a large page size Provide Multiple Page Sizes z This allows applications that require larger page sizes the opportunity to use them without an increase in fragmentation Operating System Concepts – 7th Edition, Feb 22, 2005 9.62 Silberschatz, Galvin and Gagne ©2005 Other Issues – Program Structure Program structure z Int[128,128] data; z Each row is stored in one page z Program for (j = 0; j [...]... 4 4 3 9 page faults 4 frames 10 page faults Belady’s Anomaly: more frames ⇒ more page faults Operating System Concepts – 7th Edition, Feb 22, 2005 9. 28 Silberschatz, Galvin and Gagne ©2005 FIFO Page Replacement Operating System Concepts – 7th Edition, Feb 22, 2005 9. 29 Silberschatz, Galvin and Gagne ©2005 FIFO Illustrating Belady’s Anomaly Operating System Concepts – 7th Edition, Feb 22, 2005 9. 30 Silberschatz,... Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory Operating System Concepts – 7th Edition, Feb 22, 2005 9. 22 Silberschatz, Galvin and Gagne ©2005 Need For Page Replacement Operating System Concepts – 7th Edition, Feb 22, 2005 9. 23 Silberschatz, Galvin and Gagne ©2005 Basic Page Replacement 1 Find the... caused the page fault Operating System Concepts – 7th Edition, Feb 22, 2005 9. 12 Silberschatz, Galvin and Gagne ©2005 Page Fault (Cont.) Restart instruction z block move z auto increment/decrement location Operating System Concepts – 7th Edition, Feb 22, 2005 9. 13 Silberschatz, Galvin and Gagne ©2005 Steps in Handling a Page Fault Operating System Concepts – 7th Edition, Feb 22, 2005 9. 14 Silberschatz,... EAT = 8.2 microseconds This is a slowdown by a factor of 40!! Operating System Concepts – 7th Edition, Feb 22, 2005 9. 16 Silberschatz, Galvin and Gagne ©2005 Process Creation Virtual memory allows other benefits during process creation: - Copy-on-Write - Memory- Mapped Files (later) Operating System Concepts – 7th Edition, Feb 22, 2005 9. 17 Silberschatz, Galvin and Gagne ©2005 Copy-on-Write Copy-on-Write...Page Table When Some Pages Are Not in Main Memory Operating System Concepts – 7th Edition, Feb 22, 2005 9. 11 Silberschatz, Galvin and Gagne ©2005 Page Fault If there is a reference to a page, first reference to that page will trap to operating system: page fault 1 Operating system looks at another table to decide: z z Invalid reference ⇒ abort Just not in memory 2 Get empty frame 3 Swap page into... – p) x memory access + p (page fault overhead + swap page out + swap page in + restart overhead ) Operating System Concepts – 7th Edition, Feb 22, 2005 9. 15 Silberschatz, Galvin and Gagne ©2005 Demand Paging Example Memory access time = 200 nanoseconds Average page-fault service time = 8 milliseconds EAT = (1 – p) x 200 + p (8 milliseconds) = (1 – p x 200 + p x 8,000,000 = 200 + p x 7 ,99 9,800... and frame tables 4 Restart the process Operating System Concepts – 7th Edition, Feb 22, 2005 9. 24 Silberschatz, Galvin and Gagne ©2005 Page Replacement Operating System Concepts – 7th Edition, Feb 22, 2005 9. 25 Silberschatz, Galvin and Gagne ©2005 Page Replacement Algorithms Want lowest page-fault rate Evaluate algorithm by running it on a particular string of memory references (reference string)... 2, 5, 1, 2, 3, 4, 5 Operating System Concepts – 7th Edition, Feb 22, 2005 9. 26 Silberschatz, Galvin and Gagne ©2005 Graph of Page Faults Versus The Number of Frames Operating System Concepts – 7th Edition, Feb 22, 2005 9. 27 Silberschatz, Galvin and Gagne ©2005 First-In-First-Out (FIFO) Algorithm 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... 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 Free pages are allocated from a pool of zeroed-out pages Operating System Concepts – 7th Edition, Feb 22, 2005 9. 18 Silberschatz, Galvin and Gagne ©2005 Before Process 1 Modifies Page C Operating System Concepts – 7th Edition, Feb 22, 2005 9. 19. .. 2, 3, 4, 5 1 4 2 6 page faults 3 4 5 How do you know this? Used for measuring how well your algorithm performs Operating System Concepts – 7th Edition, Feb 22, 2005 9. 31 Silberschatz, Galvin and Gagne ©2005 Optimal Page Replacement Operating System Concepts – 7th Edition, Feb 22, 2005 9. 32 Silberschatz, Galvin and Gagne ©2005 Least Recently Used (LRU) Algorithm Reference string: 1, 2, 3, 4, 1, ... Physical Memory ⇒ Operating System Concepts – 7th Edition, Feb 22, 2005 9. 5 Silberschatz, Galvin and Gagne ©2005 Virtual- address Space Operating System Concepts – 7th Edition, Feb 22, 2005 9. 6 Silberschatz,... Considerations Operating- System Examples Operating System Concepts – 7th Edition, Feb 22, 2005 9. 2 Silberschatz, Galvin and Gagne ©2005 Objectives To describe the benefits of a virtual memory system. .. pages Operating System Concepts – 7th Edition, Feb 22, 2005 9. 18 Silberschatz, Galvin and Gagne ©2005 Before Process Modifies Page C Operating System Concepts – 7th Edition, Feb 22, 2005 9. 19 Silberschatz,