Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9: Virtual Memory CSS503 Systems Programming

Similar presentations


Presentation on theme: "Chapter 9: Virtual Memory CSS503 Systems Programming"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

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

19 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

20 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

21 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

22 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


Download ppt "Chapter 9: Virtual Memory CSS503 Systems Programming"

Similar presentations


Ads by Google