230 likes | 393 Views
CSS430 Virtual Memory Textbook Ch9. These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials. Background. Why dynamic loading and overlays are not enough Users must handle unusual error conditions
E N D
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
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
Page 0 Page 1 Page 2 Page n 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 Memory map Page table Page 3 Page 4 Page 5 Page 7 Page 8 Physical memory Virtual memory CSS430 Virtual Memory
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Physical memory Swapping v.s. Demand Paging • 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
0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 A frame B 0 1 2 3 4 5 6 7 C A D 4 v E C i B A 6 v F Page table i C E D i 9 v F i F Logical memory i ? Physical memory Page Table Managing VM • 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
i free frame 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 Physical memory 6. Restart instruction 4. Bring in missing page 5. Reset tables, validation bit = 1 Disk CSS430 Virtual Memory
PC PC B ? 4 v Load M M 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 0 1 2 1 H 3 v 0 1 2 3 4 5 6 7 OS Load M i 5 v OS J i D M H Process 1’s logical memory Proc2’s page table 0 1 2 3 A J 6 v B A i 2 v D E 7 v Physical memory E Process 2’s logical memory CSS430 Virtual Memory
2. Clear all bits of the swapped frame entry f v d f v c 1. If it’s dirty, swap out a victim page 4. Reset a frame entry for this new page 3. Overwrite this frame with a desired page 0 0 i i c c Page Replacement Mechanism valid/invalid frame dirty/clean Page table CSS430 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) CSS430 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? CSS430 Virtual Memory
0 0 0 0 7 7 0 0 2 4 4 7 7 2 2 2 4 0 1 1 1 1 1 2 2 3 3 2 0 0 0 0 3 2 2 2 2 3 2 2 3 3 1 0 3 1 1 1 0 0 3 Page hit Page hit Page hit Page fault Page hit Page hit 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 2 2 3 2 2 7 3 3 3 3 3 7 7 7 7 3 3 3 4 4 4 4 7 4 4 0 4 4 0 0 0 0 0 4 4 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 Page hit Page hit Page hit Page hit Page hit Page hit Page hit Page hit CSS430 Virtual Memory
1 5 4 5 5 1 1 4 4 5 5 5 3 1 1 3 2 2 2 1 1 1 3 4 2 2 2 3 3 3 2 2 4 Page fault Page hit Page hit Page hit 1 4 1 5 5 1 1 1 1 5 5 4 5 2 1 1 2 2 2 2 2 1 1 2 3 3 2 3 3 3 3 2 2 3 4 4 3 4 4 4 4 3 Page hit Page hit 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 CSS430 Virtual Memory
2 2 2 2 7 7 2 2 2 2 2 7 7 2 2 2 2 2 0 0 0 0 0 0 0 0 4 4 0 0 0 0 0 4 0 1 1 1 1 1 3 3 3 3 3 1 1 1 3 3 3 Page hit Page hit Page hit Page hit Page hit Page fault Page hit Page hit Page hit Page hit 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. CSS430 Virtual Memory
1 1 1 1 1 7 0 0 2 4 4 7 7 2 2 2 4 0 0 3 3 0 0 3 3 0 0 3 0 0 0 0 0 0 3 2 2 2 2 7 2 2 3 3 2 1 1 1 3 2 2 Page hit Page hit Page hit Page hit Page hit Page fault Page hit 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 CSS430 Virtual Memory
0 0 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. 0 0 0 1 1 0 1 Next victim Find it! CSS430 Virtual Memory
Flip diagonally 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 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[j][k]; X CSS430 Virtual Memory
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
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
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
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 … t2 t1 WS(t1) = { 1, 2, 5, 6, 7} WS(t2) = { 3, 4 } Working-Set Model • working-set window, a fixed number of page references CSS430 Virtual Memory
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 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. When = 3, m = 8 Thrashing CSS430 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 CSS430 Virtual Memory
Exercises • Programming Assignment 4: • 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