Chapter 9: Virtual Memory CSS503 Systems Programming

Slides:



Advertisements
Similar presentations
9.4 Page Replacement What if there is no free frame?
Advertisements

Virtual Memory (Chapter 4.3)
Module 10: Virtual Memory
Chapter 10: Virtual Memory
Page Replacement Algorithms (Virtual Memory)
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Chapter 9: Virtual Memory
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Virtual Memory Management G. Anuradha Ref:- Galvin.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 38 Frame Allocation Read.
03/31/2004CSCI 315 Operating Systems Design1 Allocation of Frames & Thrashing (Virtual Memory)
9.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Virtual Memory OSC: Chapter 9. Demand Paging Copy-on-Write Page Replacement.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 15: Background Information for the VMWare ESX Memory Management.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Module 9: Virtual Memory
Module 10: Virtual Memory Background Demand Paging Performance of Demand Paging Page Replacement Page-Replacement Algorithms Allocation of Frames Thrashing.
Chapter 9: Virtual-Memory Management. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 9: Virtual Memory Background Demand Paging.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
Gordon College Stephen Brinton
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 9: Virtual Memory Background.
Chapter 10: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 10: Virtual Memory.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 9: Virtual Memory.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
CSS430 Virtual Memory Textbook Ch9
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Page 19/17/2015 CSE 30341: Operating Systems Principles Optimal Algorithm  Replace page that will not be used for longest period of time  Used for measuring.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples Operating.
Operating Systems CMPSC 473 Virtual Memory Management (3) November – Lecture 20 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
9.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts Chapter 9: Virtual-Memory Management Background Demand Paging Page Replacement Allocation.
Operating Systems (CS 340 D) Princess Nora University Faculty of Computer & Information Systems Computer science Department.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 9: Virtual-Memory Management.
Operating Systems (CS 340 D) Princess Nora University Faculty of Computer & Information Systems Computer science Department.
CSC 360, Instructor: Kui Wu Memory Management II: Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory.
Virtual Memory The address used by a programmer will be called a virtual address or logical address. An address in main memory is called a physical address.
Chapter 9: Virtual-Memory Management. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 9: Virtual-Memory Management 9.1 Background.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
10.1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
Chapter 9: Virtual Memory
Chapter 9: Virtual Memory
OPERATING SYSTEM CONCEPTS AND PRACTISE
Chapter 9: Virtual Memory – Part II
Chapter 9: Virtual Memory – Part I
Chapter 9: Virtual Memory
Chapter 9: Virtual Memory
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Chapter 6 Virtual Memory
Chapter 9: Virtual Memory
Operating Systems CMPSC 473
CSS430 Virtual Memory Textbook Ch10
Chapter 9 Virtual Memory
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Virtual Memory.
Presentation transcript:

Chapter 9: Virtual Memory CSS503 Systems Programming Prof. Munehiro Fukuda Computing & Software Systems University of Washington Bothell CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Page is referenced Check memory map If the corresponding entry points a physical memory frame, Reference it. Otherwise, the page does not exit in physical memory Bring it from to disk to memory If physical memory is full, Find a victim page Swap it out to disk Page 0 Page 1 Page 2 Page n Memory map Page table Page 3 Page 4 Page 5 Page 7 Page 8 Physical memory Virtual memory CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Page Table Managing VM C D E F B A 1 2 3 4 5 6 7 8 9 10 11 v i frame Page table Logical memory Physical memory ? If a frame bit is valid, the frame is in memory Otherwise, a page fault occurs. The corresponding page is loaded from disk A new memory space is allocated. CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Page Fault 3. OS looks at another table to decide: Invalid reference  abort. Just not in memory. Operating System 2. Trap on page fault 1. Reference to M (Does M’s page exists?) Page table load M i Physical memory 6. Restart instruction free frame 4. Bring in missing page 5. Reset tables, validation bit = 1 Disk CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Page Replacement No more free frame Find a victim page Paging out M cause another page fault Paging out H may be okay. Algorithm to minimize page faults Proc1’s page table 1 2 H PC 3 v 1 2 3 4 5 6 7 OS Load M 4 v i 5 v OS J i D M H Process 1’s logical memory B ? Load M M 1 2 3 Proc2’s page table A J PC 6 v B i A D 2 v E 7 v E Physical memory Process 2’s logical memory CSS503 Chapter 9: Virtual Memory

Page Replacement Mechanism valid/invalid frame dirty/clean i c 2. Clear all bits of the swapped frame entry 1. If it’s dirty, swap out a victim page f v d 3. Overwrite this frame with a desired page 4. Reset a frame entry for this new page f v c i c Page table CSS503 Chapter 9: Virtual Memory

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, the reference string is 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7. Algorithms FIFO (The oldest page may be no longer used.) OPT (All page references are known a priori.) LRU (The least recently used page may be no longer used.) Second Chance (A simplest form of LRU) CSS503 Chapter 9: Virtual Memory

First-In-First-Out (FIFO) Algorithm Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7. 3 frames (3 pages can be in memory at a time per process) 4 frames more frames  less page faults 7 2 3 1 4 Page fault hit 7 7 7 7 7 3 3 3 3 3 3 3 3 3 2 2 2 2 4 4 4 4 4 4 4 4 4 4 7 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 Page hit Page hit Page hit Page hit Page hit Page hit Page hit Page hit CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory How about this example? 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 process) 4 frames If more frames  more page faults Belady’s anomaly 1 5 3 4 2 Page fault hit CSS503 Chapter 9: Virtual Memory

Optimal (OPT) Algorithm Replace page that will not be used for longest period of time. 3 frames example 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7. How do you know this? Used for measuring how well your algorithm performs. 7 2 3 1 4 Page fault hit CSS503 Chapter 9: Virtual Memory

Least Recently Used (LRU) Algorithm Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7. Counter implementation Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. When a page needs to be changed, look at the counters to determine which are to change. Stack implementation – keep a stack of page numbers in a double link form: Page referenced, move it to the top No search for replacement 7 3 2 1 4 Page fault hit CSS503 Chapter 9: Virtual Memory

Second-Chance Algorithm Reference bit Each page has a reference bit, initially = 0 When page is referenced, bit set to 1. Algorithm All pages are maintained in a circular list. If page to be replaced, If the next victim pointer points to a page whose reference bit = 0 Replace it with a new brought-in page Otherwise, (i.e.,if the reference bit = 1), reset the bit to 0. Advance the next victim pointer. Go to 1. 1 Next victim Find it! CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Linux Paging Policy algorithm Choosing the process with the largest # of pages (in swap_cnt or mm->rss) Choosing pages with Accessed == 0 in their page table entries (2nd chance algorithm) Paging mechanism Disk partitions and normal files Swap out pages to contiguous runs of disk blocks CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Discussions 1 Consider data structures and algorithms that fits LRU better than FIFO. Repetitive binary tree operations Quick sort over a large array Sieve of Eratosthenes using a large array Are there any examples? In terms of the worst-case analysis, which is better? CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Discussions 1 Cont’d Consider the matrix multiplication The first algorithm is an ordinary multiplication, (i.e., row * column) The second multiplication is to flip the matrix B in diagonal and perform the multiplication in row basis, (i.e., row * row). Which performs better? Why? X // Matrix Multiplication 1 for ( int i = 0; i < SIZE; i++ ) for ( int j = 0; j < SIZE; j++ ) for ( int k = 0; k < SIZE; k++ ) c[i][j] += a[i][k] * b[k][j]; // Matrix Multiplication 2 c[i][j] += a[i][k] * b[j][k]; Flip diagonally X CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Thrashing If a process does not have “enough” pages, the page-fault rate goes very high. This leads to: low CPU utilization. operating system thinks that it needs to increase the degree of multiprogramming. another process added to the system. The new process requesting pages Thrashing increased CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Preventing Thrashing Local (or priority) replacement algorithm Effect: A thrashing process cannot steal frames from another process and thus trashing will not be disseminated. Problem: Thrashing processes causes frequent page faults and thus degrade the average service time. Locality model Process migrates from one locality to another. When  size of locality > total memory size, a thrashing occurs. Suspend some processes CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Working-Set Model working-set window,   a fixed number of page references Page reference sequence …2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4 1 3 2 3 4 44 4 3 4 4 4 …  t1 t2 WS(t1) = { 1, 2, 5, 6, 7} WS(t2) = { 3, 4 } CSS503 Chapter 9: Virtual Memory

Controlling Multiprogramming Level by Working-Set Model WSSi (working set size of Process Pi) = total number of pages referenced in the most recent  if  too small will not encompass entire locality. if  too large will encompass several localities. if  =   will encompass entire program. D =  WSSi  total frames demanded by all processes if D > m  Thrashing Policy if D > m, then suspend one of the processes. P0: 1, 2, 3, 2, 3, 3, 2, 2, 1, 2, 3, 2, 1, 2, 3, 1 WSS0 1, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 3, 3 P1: 5, 5, 5, 6, 7, 5, 7, 7, 7, 6, 6, 7, 5, 7, 6, 7 WSS1 1, 1, 1, 2, 3, 3, 2, 2, 1, 2, 2, 2, 3, 2, 3, 2 P2: 8, 9, 9, 8, 4, 8, 8, 8, 4, 9, 8, 4, 9, 8, 4, 9 WSS2 1, 2, 2, 2, 3, 2, 2, 1, 2, 3, 3, 3, 3, 3, 3, 3  WSSi 3, 5, 6, 6, 8, 7, 6, 5, 4, 7, 8, 7, 9, 7, 9, 8 When  = 3, m = 8 Thrashing CSS503 Chapter 9: Virtual Memory

Page-Fault Frequency Scheme Establish “acceptable” page-fault rate. If actual rate too low, Process may have too many frames, and thus it loses frames. If actual rate too high, Process needs more frames, and thus it gains frames CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Discussion 2 Consider that two processes A and B share page P through fork( ) (before copy-on-write) or shmget( ). What if A was a chosen as a victim process and P was chosen as a victim page from A’s page table? Can P be swapped out? Can P be released from physical space? What if B claims P later? What if B also choose P as a victim page later? Can P be swapped out twice? CSS503 Chapter 9: Virtual Memory

Chapter 9: Virtual Memory Exercises Assuming a physical memory of four page frames, give the number of page faults for the reference string 1,2,6,1,4,5,1,2,1,4,5,6,4,5 for each of the following replacement algorithms. (Initially, all frames are empty.) OPT FIFO Second-chance LRU Working Set with =3 (A page not reference within  will be paged out.) Solve Textbook Exercise 9.27. CSS503 Chapter 9: Virtual Memory