Chapter 9: Virtual Memory

Chapter 9: Virtual Memory
 Background  Demand Paging  Copy-on-Write  Page Replacement  Allocation of Frames  Thrashing

Operating System Concepts – 7th Edition, Feb 22, 2005

9.2

Silberschatz, Galvin and Gagne ©2005

1

    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 Operating System Concepts – 7th Edition.3 Silberschatz. Galvin and Gagne ©2005 2 . 2005 9. Feb 22. and allocation of page frames  To discuss the principle of the working-set model Operating System Concepts – 7th Edition. 2005 9.4 Silberschatz. Feb 22.Objectives  To describe the benefits of a virtual memory system  To explain the concepts of demand paging. Galvin and Gagne ©2005 Background  Virtual memory – separation of user logical memory from physical memory. page-replacement algorithms.

Galvin and Gagne ©2005 Virtual-address Space Operating System Concepts – 7th Edition.6 Silberschatz. Feb 22.Virtual Memory That is Larger Than Physical Memory  Operating System Concepts – 7th Edition.5 Silberschatz. Feb 22. Galvin and Gagne ©2005 3 . 2005 9. 2005 9.

7 Silberschatz. Galvin and Gagne ©2005 4 . Galvin and Gagne ©2005 Demand Paging  Bring a page into memory only when it is needed     Less I/O needed Less memory needed Faster response More users  Page is needed  reference to it   invalid reference  abort not-in-memory  bring to memory  Lazy swapper – never swaps a page into memory unless page will be needed  Swapper that deals with pages is a pager Operating System Concepts – 7th Edition. 2005 9.8 Silberschatz.Shared Library Using Virtual Memory Operating System Concepts – 7th Edition. 2005 9. Feb 22. Feb 22.

2005 9. Feb 22. Galvin and Gagne ©2005 Operating System Concepts – 7th Edition.Transfer of a Paged Memory to Contiguous Disk Space Operating System Concepts – 7th Edition. if valid–invalid bit in page table entry is I  page fault 9. 2005 5 . 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 ….10 Silberschatz.9 Silberschatz. i i page table  During address translation. Feb 22.

Feb 22.12 Silberschatz. Set validation bit = v 6.11 Silberschatz. Operating system looks at another table to decide:   Invalid reference  abort Just not in memory 2. Galvin and Gagne ©2005 6 . Reset tables 5. first reference to that page will trap to operating system: page fault 1. 2005 9. Restart the instruction that caused the page fault Operating System Concepts – 7th Edition.Page Table When Some Pages Are Not in Main Memory Operating System Concepts – 7th Edition. 2005 9. Swap page into frame 4. Feb 22. Galvin and Gagne ©2005 Page Fault  If there is a reference to a page. Get empty frame 3.

)  Restart instruction  block move  auto increment/decrement location Operating System Concepts – 7th Edition. 2005 9. 2005 9. Galvin and Gagne ©2005 7 . Feb 22.Page Fault (Cont. Feb 22.14 Silberschatz.13 Silberschatz. Galvin and Gagne ©2005 Steps in Handling a Page Fault Operating System Concepts – 7th Edition.

This is a slowdown by a factor of 40!! Operating System Concepts – 7th Edition.0   if p = 0 no page faults if p = 1. every reference is a fault  Effective Access Time (EAT) EAT = (1 – p) x memory access + p (page fault overhead + swap page out + swap page in + restart overhead ) Operating System Concepts – 7th Edition. 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.999.800  If one access out of 1.000 causes a page fault.Performance of Demand Paging  Page Fault Rate 0  p  1. Feb 22. 2005 9. Feb 22. 2005 9.000. then EAT = 8. Galvin and Gagne ©2005 8 .000 = 200 + p x 7.16 Silberschatz.2 microseconds.15 Silberschatz.

2005 9.Process Creation  Virtual memory allows other benefits during process creation: . Feb 22. Galvin and Gagne ©2005 9 . 2005 9.Memory-Mapped Files (later) Operating System Concepts – 7th Edition. Galvin and Gagne ©2005 Copy-on-Write  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. Feb 22.17 Silberschatz.Copy-on-Write .18 Silberschatz. 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.

2005 9. Galvin and Gagne ©2005 After Process 2 Modifies Page C page C’ Operating System Concepts – 7th Edition.Before Process 2 Modifies Page C Operating System Concepts – 7th Edition. Feb 22.19 Silberschatz. Galvin and Gagne ©2005 10 . 2005 9. Feb 22.20 Silberschatz.

2005 9.22 Silberschatz. but not really in use.What happens if there is no free frame?  Page replacement – find some page in memory. 2005 9. Galvin and Gagne ©2005 11 . Feb 22. Galvin and Gagne ©2005 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 – 7th Edition. Feb 22. 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 – 7th Edition.21 Silberschatz.

2005 9. Find the location of the desired page on disk Find a free frame: . Galvin and Gagne ©2005 Basic Page Replacement 1.Need For Page Replacement Operating System Concepts – 7th Edition. update the page and frame tables Restart the process 3.If there is no free frame.23 Silberschatz. Feb 22. Feb 22. 4. 2005 9. use it . Galvin and Gagne ©2005 12 . 2.If there is a free frame.24 Silberschatz. use a page replacement algorithm to select a victim frame Bring the desired page into the (newly) free frame. Operating System Concepts – 7th Edition.

2. Galvin and Gagne ©2005 13 . 5. 5 Operating System Concepts – 7th Edition. 3. 2. 1. Feb 22.26 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) and computing the number of page faults on that string  In all our examples. 1. 3.Page Replacement Operating System Concepts – 7th Edition. 2005 9. 2005 9. the reference string is 1. 2. 4. Feb 22.25 Silberschatz. 4.

3. 2005 9. Feb 22. 2. 2005 9. 4.Graph of Page Faults Versus The Number of Frames Operating System Concepts – 7th Edition. Galvin and Gagne ©2005 Operating System Concepts – 7th Edition.28 14 . 5 3 frames (3 pages can be in memory at a time per process) 1 2 3  1 2 3 4 1 2 5 3 4 9 page faults 4 frames 1 2 3 4  1 2 5 1 2 3 4 5 10 page faults 3 4 Belady’s Anomaly: more frames  more page faults Silberschatz. 2. 1. 5. Galvin and Gagne ©2005 First-In-First-Out (FIFO) Algorithm   Reference string: 1. Feb 22. 3.27 Silberschatz. 2. 1. 4.

30 Silberschatz.FIFO Page Replacement Operating System Concepts – 7th Edition. 2005 9. Feb 22.29 Silberschatz. Galvin and Gagne ©2005 FIFO Illustrating Belady’s Anomaly Operating System Concepts – 7th Edition. Feb 22. 2005 9. Galvin and Gagne ©2005 15 .

4. 2005 9. 3. 2.32 Silberschatz. 2. 3. Feb 22. Feb 22.Optimal Algorithm  Replace page that will not be used for longest period of time  4 frames example 1. 4. Galvin and Gagne ©2005 Optimal Page Replacement Operating System Concepts – 7th Edition. 1. 5. Galvin and Gagne ©2005 16 . 2005 9.31 Silberschatz. 2. 5 1 2 4 6 page faults 3 4  How do you know this?  Used for measuring how well your algorithm performs 5 Operating System Concepts – 7th Edition. 1.

Feb 22. 3. 2. Galvin and Gagne ©2005 LRU Page Replacement Operating System Concepts – 7th Edition. 1. 5 1 2 3 4 1 2 5 4 1 2 5 3 1 2 4 3 5 2 4 3  Counter implementation   Every page entry has a counter.Least Recently Used (LRU) Algorithm  Reference string: 1.33 Silberschatz. 4. look at the counters to determine which are to change Operating System Concepts – 7th Edition. Feb 22. 1. every time page is referenced through this entry. Galvin and Gagne ©2005 17 . 2. 4.34 Silberschatz. 2005 9. 5. 3. 2. copy the clock into the counter When a page needs to be changed. 2005 9.

Feb 22. Galvin and Gagne ©2005 18 . 2005 9.36 Silberschatz.LRU Algorithm (Cont. Feb 22. Galvin and Gagne ©2005 Use Of A Stack to Record The Most Recent Page References Operating System Concepts – 7th Edition.)  Stack implementation – keep a stack of page numbers in a double link form:  Page referenced:   move it to the top requires 6 pointers to be changed  No search for replacement Operating System Concepts – 7th Edition. 2005 9.35 Silberschatz.

Feb 22. subject to same rules Operating System Concepts – 7th Edition.LRU Approximation Algorithms  Reference bit  With each page associate a bit.37 Silberschatz. however  Second chance  Need reference bit  Clock replacement  If page to be replaced (in clock order) has reference bit = 1 then:  set reference bit 0  leave page in memory  replace next page (in clock order). 2005 9. initially = 0  When page is referenced bit set to 1  Replace the one which is 0 (if one exists)  We do not know the order. Galvin and Gagne ©2005 19 .

Sign up to vote on this title
UsefulNot useful