31 -OutlineAnnouncementsReading:Defining the ProblemAlgorithmsOptimalFIFONot Recently UsedSecond Chance1)a)b)c)d)2)MOS 3.3 - 3.7None Today. Questions?Page ReplacementPage ReplacementprocessOSdisk......free frame......page tablemain memoryi(1)(6)(2)(3)(4)(5)What happens when there is no free frame?Need some algorithm for determining which page toreplace with the one from disk.Page Replacement AlgorithmsGoal:Evaluation format:AlgorithmsDemand Paging & PrepagingLater:Assume the kernel knows the future.Example:Pros:Cons:Optimal solution will be our baseline of comparison.Replace a page that has not been recently used.The algorithm:How do we determine if this is so?Page Table Entry bits:Referenced bit (R) set by MMU on read/writeModified bit (M) set by MMU on writebits can only be reset to 0 by kernel.class 0:class 1:class 2:class 3:Replace the page in memory that will be usedfurthest in the future.Working Set ModelPage AllocationThrashingOptimalNot Recently Used2nd ChanceNot Frequently UsedClockAgingFirst In, First OutLeast Recently UsedExample:reference string:In:Out:select a victim page that will minimizefuture page faultsa string of memory references.(i.e. list of accessed pages)number of page faults the algorithm causesfor that string.replacing a page that will be needed again soon will causemore page faultsstr r0, 0x0123str r1, 0x1234str r2, 0x2345str r3, 0x3456ld r0, 0x0100ld r1, 0x1200str r4, 0x4567page 0page 1page 2page 3page 4...0x00000x10000x20000x30000x4000...0, 1, 2, 3, 0, 1, 4virtual memory------------Optimal Algorithmminimizes the overall number of page faults8 pages (0 - 7)3 physical framesHow many page faults?9 page faults---7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 17 7 7 2 2 2 2 2 7x 0 0 0 0 4 0 0 0x x 1 1 3 3 3 1 1reference stringpage framesoptimalimpossiblerunning program twice works in theory but not practice.--Not Recently Usedframe numbervalid (present/absent)protection bitsmodifiedreferenced-1)2)3)4)initially, R and M bits are set to 0periodically (on clock interrupt), R bit iscleared by kernel (M remains same)when replacement needed, pages are classifiedinto 4 categories (R and M bits)replace a random page from the set of pageswith the lowest available class--so we know which pages have been used recentlynot referenced, not modifiedreferenced, not modifiedreferenced, modifiednot referenced, modifiedPros:Cons:easy to understandperformance is adequate at bestdecision based on limited historydecision based on limited informationkernel must look at every page in page table(since last clock interrupt)(only 2 bits of data)can only remove pages from process that triggered fault.even more expensive to look at other processes' page tables.moderately efficient to implement------First In, First Out (FIFO)Instead of looking at page table entries, kernelmaintains a FIFO queue of loaded pages.replace the page at the front of the list andadd the new page to the endreplaces page that has been in RAM the longest--Example:15 page faultsConsider the reference string:Increasing the number of frames should generallyalways reduce faults.Similar to FIFO, but considers referenced bitWhile the FIFO is full:What happens if all were used?If it doesn't, the algorithm is exceptionally bad.Belady's Anomaly7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 17 7 7 2 2 2 4 4 4 0 0 0 7 7 7x 0 0 0 3 3 3 2 2 2 1 1 1 0 0x x 1 1 1 0 0 0 3 3 3 2 2 2 1reference stringpage framesPros:Cons:trivial to implementnot limited to current process's pagesage has nothing to do with frequency of useFIFO suffers from Belady's Anomalycounterintuitively, page faults can increase with more framesperformance is quite bad in practice----1 2 3 4 1 2 5 1 2 3 4 521234567468101214faults vs. framesframesfaultsBelady's Anomalymore framesthan pages,replacementunnecessaryalwaysreplaceSecond Chanceif R = 1, set R = 0 and rotate to end ofFIFO (hence, "Second Chance")if R = 0, evict it and add the new pageconsider the first page:----Simply evicts the oldest, as with original FIFO.AR=1AR=0AR=0GR=1BR=0BR=0CR=1CR=1CR=1DR=1DR=1DR=1ER=0ER=0ER=0FR=1FR=1FR=1Example:oldestnewestt=0t=14t=14t=14t=3t=3t=7t=7t=7t=8t=8t=8t=10t=10t=10t=12t=12t=12page fault at t=14 requires replacement:second chanceevictadd new pageWe can improve efficiency with a better datastructure.Use a circular list.only need to keep reference to next pageAR=1BR=0CR=1DR=1ER=0FR=1GR=0HR=1IR=0.........AR=1BR=0CR=0DR=0JR=1FR=1GR=0HR=1IR=0.........Pros:Cons:significant improvement over FIFOnot limited to current process's pagesinefficiency due to moving pagesif many pages have R=1, lots of pointer updates---Clock Implementation1ki1Mi1Gi1Ti1Pi1Ei= 2^10= 2^20= 2^30= 2^40= 2^50= 2^60assume base 2 if i forgot the "i"