1 / 30

Virtual Memory

Virtual Memory. 12. Virtual Memory Organization. Primary Memory. Secondary Memory. Memory Image for p i. Executable Image. Physical Address Space. B t : Virtual Address Space  Physical Address Space. Names, Virtual Addresses & Physical Addresses. Dynamically. Source Program.

edward
Download Presentation

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. VirtualMemory 12 Operating Systems: A Modern Perspective, Chapter 12

  2. Virtual Memory Organization Primary Memory Secondary Memory Memory Image for pi Operating Systems: A Modern Perspective, Chapter 12

  3. Executable Image Physical Address Space Bt: Virtual Address Space  Physical Address Space Names, Virtual Addresses & Physical Addresses Dynamically Source Program Absolute Module Name Space Pi’s Virtual Address Space Operating Systems: A Modern Perspective, Chapter 12

  4. <1% 15% 35% 20% <1% 30% Execution time Locality Address Space for pi • Address space is logically partitioned • Text, data, stack • Initialization, main, error handle • Different parts have different reference patterns: Initialization code (used once) Code for 1 Code for 2 Code for 3 Code for error 1 Code for error 2 Code for error 3 Data & stack Operating Systems: A Modern Perspective, Chapter 12

  5. Virtual Memory • Every process has code and data locality • Code tends to execute in a few fragments at one time • Tend to reference same set of data structures • Dynamically load/unload currently-used address space fragments as the process executes • Uses dynamic address relocation/binding • Generalization of base-limit registers • Physical address corresponding to a compile-time address is not bound until run time Operating Systems: A Modern Perspective, Chapter 12

  6. Virtual Memory (cont) • Since binding changes with time, use a dynamic virtual address map, Bt Virtual Address Space Bt Operating Systems: A Modern Perspective, Chapter 12

  7. Physical Address Space 0 n-1 • Each address space is fragmented Primary Memory • Fragments of the virtual address space are dynamically loaded into primary memory at any given time Virtual Memory Secondary Memory Virtual Address Space for pi Virtual Address Space for pj Virtual Address Space for pk • Complete virtual address space is stored in secondary memory Operating Systems: A Modern Perspective, Chapter 12

  8. Paging • A page is a fixed size, 2h, block of virtual addresses • A page frame is a fixed size, 2h, block of physical memory (the same size as a page) • When a virtual address, x, 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 is not loaded, the OS interrupts the process and loads the page into a page frame Operating Systems: A Modern Perspective, Chapter 12

  9. Page-Based 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 • Bt:[0:G-1]  <U, V>  {W} • Since every page is size c=2h • page number = U = i/c • line number = V = i mod c Operating Systems: A Modern Perspective, Chapter 12

  10. Modeling Page Behavior • Let R = r1, r2, r3, …, ri, … be a page reference stream • ri is the ith page # referenced by the process • The 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 Operating Systems: A Modern Perspective, Chapter 12

  11. Static Allocation, Demand Paging • Number of page frames is static over the life of the process • Fetch policy is demand • Since St(m) = St-1(m)  {rt} - {y}, the replacement policy must choose y -- which uniquely identifies the paging policy Operating Systems: A Modern Perspective, Chapter 12

  12. Address Translation with Paging g bits h bits Virtual Address Page # Line # “page table” Missing Page Bt j bits h bits Physical Address Frame # Line # CPU Memory MAR Operating Systems: A Modern Perspective, Chapter 12

  13. 13 page faults • No knowledge of R not perform well • Easy to implement Random Replacement • Replaced page, y, is chosen from the m loaded page frames with probability 1/m Let page reference stream,R = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 1 2 2 2 0 2 0 3 2 1 3 2 1 3 2 1 0 3 1 0 3 1 0 3 1 2 0 1 2 0 3 2 0 3 2 0 6 2 4 6 2 4 5 2 7 5 2 Operating Systems: A Modern Perspective, Chapter 12

  14. Belady’s Optimal Algorithm • Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x) Let page reference stream,R = 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 Operating Systems: A Modern Perspective, Chapter 12

  15. Belady’s Optimal Algorithm • Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x) Let page reference stream,R = 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 • Perfect knowledge of R perfect performance • Impossible to implement Operating Systems: A Modern Perspective, Chapter 12

  16. Least Recently Used (LRU) • Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x) Let page reference stream,R = 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 Operating Systems: A Modern Perspective, Chapter 12

  17. Least Recently Used (LRU) • Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x) Let page reference stream,R = 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 3 2 2 2 2 6 6 6 6 1 0 0 0 0 0 0 0 0 0 0 0 0 4 4 4 2 3 3 3 3 3 3 3 3 3 3 3 3 5 5 3 1 1 1 1 1 1 1 1 1 1 1 1 7 • Backward distance is a good predictor of forward distance -- locality Operating Systems: A Modern Perspective, Chapter 12

  18. Least Frequently Used (LFU) • Replace page with minimum use: yt = min xeS t-1(m)FREQ(x) Let page reference stream,R = 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 Operating Systems: A Modern Perspective, Chapter 12

  19. Least Frequently Used (LFU) • Replace page with minimum use: yt = min xeS t-1(m)FREQ(x) Let page reference stream,R = 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 Operating Systems: A Modern Perspective, Chapter 12

  20. First In First Out (FIFO) • Replace page that has been in memory the longest: yt = max xeS t-1(m)AGE(x) Let page reference stream,R = 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 Operating Systems: A Modern Perspective, Chapter 12

  21. First In First Out (FIFO) • Replace page that has been in memory the longest: yt = max xeS t-1(m)AGE(x) Let page reference stream,R = 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 AGE4(2) = 3 AGE4(0) = 2 AGE4(3) = 1 Operating Systems: A Modern Perspective, Chapter 12

  22. Stack Algorithms • Some algorithms are well-behaved • Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 0 0 0 0 3 1 1 1 1 2 2 2 LRU Frame 0 1 2 3 0 1 4 0 1 2 3 4 0 0 0 0 0 1 1 1 1 2 2 2 3 3 Operating Systems: A Modern Perspective, Chapter 12

  23. Stack Algorithms • Some algorithms are well-behaved • Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 4 0 0 0 0 3 3 3 4 4 4 4 4 4 1 1 1 1 0 0 0 0 0 2 2 2 2 2 2 2 1 1 1 1 1 3 3 FIFO Frame 0 1 2 3 0 1 4 0 1 2 3 4 0 0 0 0 0 0 0 4 4 4 4 3 3 1 1 1 1 1 1 1 0 0 0 0 4 2 2 2 2 2 2 2 1 1 1 1 3 3 3 3 3 3 3 2 2 2 Operating Systems: A Modern Perspective, Chapter 12

  24. Reference to Address k in Page i (User space) • Lookup (Page i, Addr k) • Reference (Page Framej, Addr k) • Translate (Page i, Addr k) to (Page Framej, Addr k) Windows NT Paging System Virtual Address Space Primary Memory Paging Disk (Secondary Memory) Supv space User space Operating Systems: A Modern Perspective, Chapter 12

  25. Windows Address Translation Virtual page number Line number Page Directory Page Table Byte Index a b A Page Directory c Page Tables B C Target Page Target Byte Operating Systems: A Modern Perspective, Chapter 12

  26. Linux Virtual Address Translation Operating Systems: A Modern Perspective, Chapter 12

  27. Segment Address Translation • Bt: segments x offsets  physical address  {W} • Bt(i, j) = k • S: segments  segment addresses • Bt(S(segName), j) = k • N: offset names  offset addresses • Bt(S(segName), N(offsetName)) = k • Read implementation in Section 12.6 Operating Systems: A Modern Perspective, Chapter 12

  28. Address Translation in Segmentation <segmentName, offsetName> S N segment # offset ? Limit Bt Relocation Missing segment + Limit Base P To Memory Address Register Operating Systems: A Modern Perspective, Chapter 12

  29. NT Memory-mapped Files Secondary memory Ordinary file • Open the file • Create a section object (that maps file) • Identify point in address space to place the file Executable memory Memory mapped files Section object Operating Systems: A Modern Perspective, Chapter 12

  30. NT Memory-mapped Files(2) Operating Systems: A Modern Perspective, Chapter 12

More Related