300 likes | 527 Views
9.4 Page Replacement. What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same page may be brought into memory several times. Basic Page Replacement. Page Replacement. Page Replacement Algorithms. Goal:
E N D
9.4 Page Replacement • What if there is no free frame? • Page replacement –find some page in memory, but not really in use, swap it out • In this case, same page may be brought into memory several times
Page Replacement Algorithms • Goal: 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 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
FIFO • When a page must be replaced, the oldest page is chosen • In all our examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 • 3 frame (9 page faults) • 4 frame (10 page faults) • Notice that the number of faults for 4 frames is greater than the umber of faults for 3 frames!! This unexpected result is known as Belady’s anomaly
Optimal Page-Replacement Algorithm • Replace page that will not be used for longest period of time • This is a design to guarantee the lowest page-fault rate for a fixed number of frames
Optimal Page-Replacement Algorithm • Unfortunately, the optimal page-replacement is difficult to implement, because it requires future knowledge of the reference string
Least-recently-used (LRU) algorithm • LRU replacement associates with each page the time of that page’s last use • When a page must be replaced, LRU chooses the page that has not been used for the longest period of time
Least-recently-used (LRU) algorithm • The major problem is how to implement LRU replacement: • Counter: whenever a reference to a page is made, the content of the clock register are copied to the time-of-use filed in the page table entry for the page. We replace the page with the smallest time value • Stack: Whenever a page is referenced, it is removed from the stack and put on the top. In this way, the most recently used page is always at the top of the stack
Stack implementation • Note that both implementation of LRU requires hardware support • If we were to use an interrupt for every reference to allow software to update such data, it would slow every memory reference
LRU Approximation Page Replacement • The system without hardware support provide some help for LRU, in a form of a reference bit • Initially the bits are cleared (to 0) by the OS. As a user process executes, the bit associated with each page reference is set (to 1) by the hardware • After a while, the clear step is activated • In this method, although we can not get the order of page reference, we can know that whether the page is referenced or not
Second-Chance Algorithm • Basically, it’s a FIFO algorithm • When a page has been selected, we inspect its reference bit. • If the value is 0, we proceed to replace this page, otherwise, we give the page a second chance and move on to select the next FIFO page • When a page get a second chance, it’s reference bit is cleared, and its arrival time is reset to the current time
Second-Chance Algorithm • When a page get a second chance, it’s reference bit is cleared, and its arrival time is reset to the current time • If a page is used often enough to keep its reference bit set, it will never be replaced
Second-Chance Algorithm • One way to implement the second-chance algorithm is as a circular queue • In the worst case, when all bits are set, the pointer cycles through the whole queue, giving each page a second chance • Enhanced second-chance algorithm
Additional-Reference-Bits Algorithm • We can gain additional ordering information by recording the reference bits at regular intervals. • We can keep 8-bit byte for each page in a table in memory • The OS shifts the reference bit for each page into the high-order bit of its 8-bit byte, shifting the other bit right by 1 bit and discard the low-order bit
Additional-Reference-Bits Algorithm • For example: 00000000 means the page has not been used for eight-time interval • 11111111 means a page that is used at least once in each period • 11000100 has used more recently with both value of 01110111 and 00111111
Counting Based Page Replacement • Least Frequently used (LFU) page-replacement algorithm • Most frequently used (MFU) page-replacement algorithm
Page Buffering Algorithm • We can keep some amount of frames (for example 3 frames) always free • When a page fault occurs, the system does not have to wait the execution of replace algorithm. • The system can load the page into free space and swapped out frames
Allocation of Frames • When we have more than one processor, we need to allocate frames to each processor • Two major algorithms: • 1. equal allocation • 2. proportional allocation