1 / 74

Chapter 12: Virtual Memory

Chapter 12: Virtual Memory. Prof. Steven A. Demurjian, Sr. † Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155. steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818.

tameka
Download Presentation

Chapter 12: 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 12: Virtual Memory Prof. Steven A. Demurjian, Sr. † Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155 steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818 † These slides have been modified from a set of originals by Dr. Gary Nutt.

  2. Purpose of this Chapter • What is Virtual Memory? • Virtual Address Space for Process Execution • Automatic Transfer of “Pages” Back and Forth Between Primary and Secondary Memory • Why was Virtual Memory Developed? • Allow Multiple Processes to Run in Limited Memory Regardless of Process Size • Reduce Need for “Programmer” Intervention • How is Virtual Memory Management Attained? • What is Segmentation vs. Paging? • What are Various Static Paging Algorithms?

  3. Names, Virtual Addresses, and Physical Addresses Distinguish Between Symbolic Names vs. Virtual Addrs. vs. Physical Addrs. Source Program Absolute Module Executable Image Name Space Virtual Address Space Physical Address Space Compile and Link Tools Yt: Virtual Address Space  Physical Address Space  {}

  4. Virtual Memory • Components of Program (Identifiers, Labels, variables, etc.) Define Name Space • Language Translation • Converts Name Space Elements to Virtual Addresses • Binds Symbolic Names to Virtual Addresses • Virtual Memory System • Dynamically Binds Virtual Addresses to Physical Addresses • Defers Binding Decision to During Program Execution • Perform Binding “As Needed”

  5. Virtual Memory • Virtual Memory Utilizes Dynamic Address Relocation/Binding • Generalization of Base-Limit Registers (Chapter 11) • Physical Address Corresponding to a Compile-time Address is Not Known Until Run Time • Idea is that Only Part of the Address Space is Loaded As Process Executes • This Works Because of Program and Data Locality • To Support VM, a Virtual Address Translation Map or Page Map Table Yt Where t Refers to Virtual Time

  6. Virtual Memory • Use a Dynamic Virtual Address Map, Yt • For Element i (page i), Examine Yt(i) • If  Then Generate Interrupt Since Element in Secondary Memory, Load, Set Yt(i) = k • Else, Map to Physical Address Yt(i) = k Virtual Address Space Physical Address Space Yt

  7. Segmentation vs. Paging • Segmentation Extends Relation-Limit Registers (see Section 11.3 and Figures 11.12, 13, 15) • Programmer Defined Variable Length Segments for Various Program Components • Virtual Address of <segmentNumber, offset> • Variable Sized Segments Complicate Placement of Process in Primary Memory • Programmer Control Requires “Expertise” • Paging Utilizes Single Component Addresses • Defines Fixed Size Pages as Unit of Transfer Between Primary and Secondary Memory • Process Works Transparently • Paging Commercially Dominant form VM

  8. Summarizing Address Formation • Translation System Produces an Address Space, but Address Are Virtual Instead of Physical • A Virtual Address, x, is: • Mapped to y = Yt(x) if x is Loaded at Physical Address y • Mapped to  if x is not Loaded • Map Changes As the Program Executes • Yt : Virtual Address  Physical Address  {} • Segmentation and Paging Differ in • Their Realization of Yt • Programmer Intervention Typically Needed for Segmentation Solutions

  9. Paging • Paging Utilizes Allocation Strategy that Reduces External Fragmentation via Fixed Size Pages • A Page (Virtual Memory) is a Fixed Sized Block of Virtual Addresses • A Page Frame (Primary Memory) is a Fixed Size Block of Memory, the Same Size As a Page • When a Virtual Address in Page i is Referenced by the CPU • If Page i is Loaded at Page Frame j, the Virtual Address is Relocated to Page Frame j • If Page i is Not Loaded, the OS Interrupts the Process and Loads the Page Into a Page Frame

  10. Goals and Capabilities of Paging System • Goals • Identify Set of Pages Needed for Process’s Current Locality • Load Only Those Pages into Page Frames • Unload Those Pages with “Old” Locality • Key: What is “Best” Page to Unload? • Capabilities • Translate Virtual Address to Physical Address • Virtual Address in Yt(i) to <pageFrameNumber, Offset> • Dynamically Bind Pages (Virtual) to Page Frames (Real) as Part of Address Translation and Page Loading Process

  11. Addresses • Suppose There are G= 2g+h Virtual Addresses and H= 2j+h Physical Addresses • Each Page/Page Frame has 2h Addresses • There are 2g Pages in the Virtual Address Space for Some g • There are 2j Page Frames Are Allocated to the Process for Some j • Rather Than Map Individual Addresses, Yt Maps the 2g Pages to the 2j Page Frames • What Does it Mean when g < j? g > j?

  12. Address Translation • Let N = {d0, d1, … dn-1} be the Pages • Let M = {b0, b1, …, bm-1} be Page Frames • Virtual Address, i, Satisfies 0  I < G= 2g+h • Physical Address, k = U2h + V (0  V < G= 2h ) • U is Page Frame Number • V is the Line Number Within the Page • Yt :[0:G-1]  <U, V>  {W} • Since Every Page is Size c=2h • Page Number = U = i/c (floor) • Line Number = V = i mod c (offset)

  13. Example of Address Translation • Let 3257 is Virtual Address and c=100 Page Size • Page Number = p = 3257/100 = 32 • Line Number = l = 3257 mod 100 = 57 • Obtain Yt(p) = k= 19 • If k =  Then Load Page from Secondary Storage • Continue with Address Translation • Physical Address is • Yt(i/c ) + i mod c • Yt(32) + 57 • 1900 + 57 = 1957 Physical Address

  14. Address Translation (Continued) g bits h bits Virtual Address Page # Line # Missing Page Page Fault Yt j bits h bits Physical Address Frame # Line # CPU Memory MAR

  15. Handling Page FaultsOS Algorithm as Follows: • Interrupt Process Containing Missing Page • Memory Manager Locates Missing Page in Secondary Storage • Load Missing Page into Primary Memory Thereby Displacing (Unloading) Another Page • Update Page Table to Reflect Both New and Removed Pages • CPU Re-executes Instruction During Next Time Slice of Process

  16. Demand Paging • Prior Algorithm Describes Demand Paging Process: • Page Fault Occurs • Process With Missing Page is Interrupted • Memory Manager Locates the Missing Page • Page is Unloaded (Replacement Policy) • Page is Loaded in the Vacated Page Frame • Page Table is Updated • Process is Eligible to be Restarted • Key Policies for Any Paging Algorithm are: • Fetch, Replacement, and Placement

  17. Paging Algorithms and Policies • Static Paging: Each Process Allocated a Fixed Number of Physical Pages When Started • Dynamic Paging: Total Pages Can Change During Execution • Three Basic Policies: • Fetch: Which Page Should be Loaded into PM? • Replacement: Which Page Removed if all Page Frames are Full? • Placement: Where Fetched Page Loaded in PM? • Placement Not Relevant for Static Paging • Note: Static Algorithms Focus on Replacement Since Fetch is Determined by Process Execution

  18. Modeling Page Behavior • Let w = r1, r2, r3, …, ri, … be a page reference stream • ri is the ith page # Referenced by the Process • Subscript is the Virtual Time for the Process • Given a Page Frame Allocation of m, the Memory State at Time t, St(m), is set of Pages Loaded • St(m) = St-1(m)  Xt - Yt • Xt is the Set of Fetched Pages at Time t • Yt is the Set of Replaced Pages at Time t • Some

  19. More on Demand Paging • Demand Fetch Policy • If rt was Loaded at Time t-1, St(m) = St-1(m) • If rt was not Loaded at Time t-1 and there were Empty Page Frames • St(m) = St-1(m)  {rt} • No Need to Remove a Page • If rt was not Loaded at Time t-1 and there were no Empty Page Frames • St(m) = St-1(m)  {rt} - {y} • Must Remove Page y • Alternative is prefetch Paging Which Attempts to Predict Which Pages Can be Staged into PM • This is Difficult to Achieve in Practice!

  20. Assumptions for Static Allocation with Demand Paging • Number of Page Frames is Static Over the Life of the Process • Fetch Policy is Demand • Replacement Policy • Since St(m) = St-1(m)  {rt} - {y}, the Replacement Policy Must Choose y • This Uniquely Identifies the Paging Policy • Placement Policy Non-Issue since Number of Page Frames Fixed for Process at Start of its Execution • We’ll Explore Following Replacement Policies • Random, Belady’s Optimal Algorithm • LRU, LF(requently)U, and FIFO

  21. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 1 2

  22. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 0 0 2 3

  23. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 1 0 0 1 2 3 3

  24. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 1 0 0 1 1 1 2 3 3 3 0

  25. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 1 0 0 1 1 1 1 2 3 3 3 0 0

  26. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 3 3 1 0 0 1 1 1 1 1 1 2 3 3 3 0 0 0 2

  27. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 3 3 0 1 0 0 1 1 1 1 1 1 1 2 3 3 3 0 0 0 2 1

  28. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 3 3 0 0 1 0 0 1 1 1 1 1 1 1 3 2 3 3 3 0 0 0 2 1 1

  29. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 3 3 0 0 0 0 1 0 0 1 1 1 1 1 1 1 3 3 6 2 3 3 3 0 0 0 2 1 1 1 1

  30. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 3 3 0 0 0 0 4 1 0 0 1 1 1 1 1 1 1 3 3 6 6 2 3 3 3 0 0 0 2 1 1 1 1 1

  31. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 3 3 0 0 0 0 4 4 1 0 0 1 1 1 1 1 1 1 3 3 6 6 5 2 3 3 3 0 0 0 2 1 1 1 1 1 1

  32. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 3 3 0 0 0 0 4 4 7 1 0 0 1 1 1 1 1 1 1 3 3 6 6 5 5 2 3 3 3 0 0 0 2 1 1 1 1 1 1 1

  33. Random Replacement • Replaced Page, y, is Chosen From the m Loaded Page Frames With Probability 1/m • Easy to Implement: No Real Advantage in Practice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 3 3 3 0 0 0 0 4 4 7 1 0 0 1 1 1 1 1 1 1 3 3 6 6 5 5 2 3 3 3 0 0 0 2 1 1 1 1 1 1 1 13 page faults

  34. Belady’s Optimal Algorithm • Replace Page With Maximal Forward Distance: yt = max xeS t-1(m)FWDt(x) • Optimality Requires Complete Knowledge of w Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 0 0 2 3

  35. Belady’s Optimal Algorithm • Look Ahead in Reference for Page that Won’t be Needed for Longest Time • Replace that Page Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 0 0 2 3 FWD4(2) = 1 FWD4(0) = 2 FWD4(3) = 3

  36. Belady’s Optimal Algorithm • Look Ahead in Reference for Page that Won’t be Needed for Longest Time • Replace that Page Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 1 0 0 0 2 31 FWD4(2) = 1 FWD4(0) = 2 FWD4(3) = 3

  37. Belady’s Optimal Algorithm • Look Ahead in Reference for Page that Won’t be Needed for Longest Time • Replace that Page Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 1 0 0 0 0 0 2 31 1 1

  38. Belady’s Optimal Algorithm • Look Ahead in Reference for Page that Won’t be Needed for Longest Time • Replace that Page Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 2 1 0 0 0 0 0 3 2 31 1 1 1 FWD7(2) = 2 FWD7(0) = 3 FWD7(1) = 1

  39. Belady’s Optimal Algorithm • Look Ahead in Reference for Page that Won’t be Needed for Longest Time • Replace that Page Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 2 2 2 0 1 0 0 0 0 0 3 3 3 3 2 31 1 1 1 1 1 1 FWD10(2) =  FWD10(3) = 2 FWD10(1) = 3

  40. Belady’s Optimal Algorithm • Look Ahead in Reference for Page that Won’t be Needed for Longest Time • Replace that Page Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 2 2 2 0 0 0 1 0 0 0 0 0 3 3 3 3 3 3 2 31 1 1 1 1 1 1 1 1 FWD13(0) =  FWD13(3) =  FWD13(1) = 

  41. Belady’s Optimal Algorithm • Perfect Knowledge of w Perfect Performance • Impossible to Implement • Provides Baseline for Comparison Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 2 2 2 0 0 0 0 4 4 4 1 0 0 0 0 0 3 3 3 3 3 3 6 6 6 7 2 31 1 1 1 1 1 1 1 1 1 1 5 5 10 page faults

  42. Least Recently Used (LRU) • Replace Page with Maximal Backward Distance: yt = max xeS t-1(m)BKWDt(x) • Which Page Hasn’t Been Used “Recently”? Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 0 0 2 3 BKWD4(2) = 3 BKWD4(0) = 2 BKWD4(3) = 1

  43. Least Recently Used (LRU) • Replace Page with Maximal Backward Distance: yt = max xeS t-1(m)BKWDt(x) • Which Page Hasn’t Been Used “Recently”? Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 1 0 0 0 2 3 3 BKWD4(2) = 3 BKWD4(0) = 2 BKWD4(3) = 1

  44. Least Recently Used (LRU) • Replace Page with Maximal Backward Distance: yt = max xeS t-1(m)BKWDt(x) • Which Page Hasn’t Been Used “Recently”? Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 1 1 0 0 0 2 2 3 3 3 BKWD5(1) = 1 BKWD5(0) = 3 BKWD5(3) = 2

  45. Least Recently Used (LRU) • Replace Page with Maximal Backward Distance: yt = max xeS t-1(m)BKWDt(x) • Which Page Hasn’t Been Used “Recently”? Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 1 1 1 0 0 0 2 2 2 3 3 3 0 BKWD6(1) = 2 BKWD6(2) = 1 BKWD6(3) = 3

  46. Least Recently Used (LRU) • Replace Page with Maximal Backward Distance: yt = max xeS t-1(m)BKWDt(x) • Which Page Hasn’t Been Used “Recently”? Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 1 1 3 3 3 0 0 0 6 6 6 7 1 0 0 0 2 2 2 1 1 1 3 3 3 4 4 4 2 3 3 3 0 0 0 2 2 2 1 1 1 5 5 16 page faults

  47. Least Frequently Used (LFU) • Replace Page with Minimum Use: yt = min xeS t-1(m)FREQ(x) • Break Ties with Random Choice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 0 0 2 3 FREQ4(2) = 1 FREQ4(0) = 1 FREQ4(3) = 1

  48. Least Frequently Used (LFU) • Replace Page with Minimum Use: yt = min xeS t-1(m)FREQ(x) • Break Ties with Random Choice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 1 0 0 1 2 3 3 FREQ5(2) = 1 FREQ5(0) = 1 FREQ5(3) = 1

  49. Least Frequently Used (LFU) • Replace Page with Minimum Use: yt = min xeS t-1(m)FREQ(x) • Break Ties with Random Choice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 1 0 0 1 1 1 2 3 3 3 0 FREQ6(2) = 2 FREQ6(1) = 1 FREQ6(3) = 1

  50. Least Frequently Used (LFU) • Replace Page with Minimum Use: yt = min xeS t-1(m)FREQ(x) • Break Ties with Random Choice Let page reference stream, w = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 1 0 0 1 1 1 2 3 3 3 0 FREQ7(2) = ? FREQ7(1) = ? FREQ7(0) = ? • Complete to Yield ?? page faults • Unfairness Possible Since Count from Start of Reference Stream

More Related