Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS430 Virtual Memory Textbook Ch9

Similar presentations


Presentation on theme: "CSS430 Virtual Memory Textbook Ch9"— Presentation transcript:

1 CSS430 Virtual Memory Textbook Ch9
These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials. CSS430 Virtual Memory

2 Background Why dynamic loading and overlays are not enough
Users must handle unusual error conditions Users need to keep track of which portion of data are loaded. While programs load data portion by portion, even such a loaded portion is never referred. Virtual memory – separation of user memory from physical memory. Only part of the program needs to be in memory for execution. Logical space can be much larger than physical address space. Need to allow pages to be swapped in and out. Benefits No more constraints by physical memory Less physical memory needed, thus increasing multiprogramming degree Less I/O needed, thus improving performance CSS430 Virtual Memory

3 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 CSS430 Virtual Memory

4 Swapping v.s. Demand Paging
2 1 3 6 5 4 7 10 9 8 11 14 13 12 15 18 17 16 19 22 21 20 23 Physical memory Swapping All pages composing a program are brought into memory and taken out to disk. Demand Paging Only pages currently referenced by a program are brought into memory and taken out to disk. Program A Swap out Swap in Program B CSS430 Virtual Memory

5 Page Table Managing VM If a frame bit is valid, the frame is in memory
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. CSS430 Virtual Memory

6 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 CSS430 Virtual Memory

7 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 CSS430 Virtual Memory

8 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 CSS430 Virtual Memory

9 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) CSS430 Virtual Memory

10 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? CSS430 Virtual Memory

11 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 CSS430 Virtual Memory

12 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 CSS430 Virtual Memory

13 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 CSS430 Virtual Memory

14 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 CSS430 Virtual Memory

15 Second-Chance Algorithm
1 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. Next victim Find it! CSS430 Virtual Memory

16 Discussions 2 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 CSS430 Virtual Memory

17 Page Allocation Algorithm
#frames = m, #processes = n Equal allocation m/n frames are allocated to each process. Proportional allocation (Pi’s vm size / pi’s vm size) * m frame are allocated to each process. This allocation is achieved regardless of process priority Priority allocation A process with a higher priority receives more frames. Global versus local allocation Global: can steal frames from another process. Local: must find out a victim page among pages allocated to this process. CSS430 Virtual Memory

18 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 CSS430 Virtual Memory

19 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 CSS430 Virtual Memory

20 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 } CSS430 Virtual Memory

21 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 CSS430 Virtual Memory

22 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 CSS430 Virtual Memory

23 Exercises Programming Assignment 4: No turn-in problems:
Check the syllabus for its due date. No turn-in problems: 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 Exercise 9.7 of your textbook. Solve Exercise 9.28 of your textbook. CSS430 Virtual Memory


Download ppt "CSS430 Virtual Memory Textbook Ch9"

Similar presentations


Ads by Google