32 -OutlineLeast Recently Used (LRU)AnnouncementsReading:Least Recently UsedDemand Paging & PrepagingNot Frequently UsedWorking SetsRecall optimal: replace page most distantly usedApproximate optimal by examining history:LRU: replace the oldest used pageExample:Example:Example: 3 framesIssue:Issues:Issues:We can fix it though.Similar to NFU, but counter is instead a shift-right value.Example: 3 frames3 framesreferencespriority listPriority List implementationHardware Counter implementationNot Frequently Used (NFU)AgingDemand Paging vs. PrepagingWorking Sets12 page faults. not optimal, but better than FIFOAging1)a)b)a)2)MOS 3.3 - 3.7PA 5 due FridayPage Replacement---Second Chance doesn't guarantee removal ofthe oldest used pagerecently, heavily used pages are likely to beused again soonlong unused pages are not likely to be usedagain soon7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 17 7 7 2 2 4 4 4 0 1 1 1_ 0 0 0 0 0 0 3 3 3 0 0_ _ 1 1 3 3 2 2 2 2 2 7Pros:Cons:effective in terms of page faultsexact solution expensive to implement practically------most recently used at frontPure software implementationUpdating on every reference is expensive!Only evicts from current processLinear search page table for lowest counternot actually LRU: doesn't forget references.tracks overall frequency of use, not recent useSince the list is maintained by kernel, not limitedto current process pages.least recently used at endon every reference, move page to fronton page fault, evict page at end477010470000771177444Keep a hardware counter that increments on everyinstruction.Approximate LRU using clock and existing R bitinstead of dedicated counterAlgorithm:--MMU updates page table entry after eachreference with counter valueon page fault, OS replaces page with lowestcounter valuepage table6004152203471234122312340000frame #frame #evictedframe #0123......referencespage 2page 1page 2page 3page 1page 0@ C=47@ C=114@ C=220@ C=347@ C=415@ C=600------adds 1 if page was recently used, 0 otherwiseapproximates frequency of useon clock interrupt, add R to PTE counter field,then clear R biton page fault, evict page with lowest counter--(R bit only set once in an interval, regardless of # accesses)page table counters0123...references0, 1, 21, 0, 2, 11, 2, 1, 02, 00, 3interruptupdateinterruptupdateinterruptupdateinterruptupdatepagefaultAlgorithm:---on clock interrupt, shift counter field right andappend R bit to lefton page fault, evict page with lowest counterforgets bits after some number of interruptsExample: 3 frames10001100111011110111100011000110101111011000110011101111111100000000000000000000page table counters0123...referencesR bits0, 1, 2111011101110101001101, 0, 2, 11, 2, 1, 02, 032, 1, 2interruptinterruptinterruptinterruptinterruptpagefaultIssues:Example: k = 10VisualizationsWorking Sets by Window Size (k)In practice, pretty good.So far, we've seen demand paging algorithmsWe can attempt to avoid faults with prepaging.Demand and prepaging are not mutually exclusive.still not actually LRU:linear search of counters on fault------------counters can be kept on per-frame basis toconsider pages from other processespages are loaded ("fetched") on-demand,not in advance.pre-fetch pages and hope for fewer page faultswhich pages and # of pages change over timeprocess spend most time waiting for kernel tocopy pages between RAM and diskmost time blocked on disk accesses (in kernel)requires educated guessing!always eat the initial page fault--limited past horizon (finite # bits)limited information (clock interval)what if multiple counters 0? pick at randomwhat if multiple counters same?don't know which was first. pick at randomstackheapbssdatacodefaultsdiskfetchWhich pages to prefetch?A process's working set is the the set of pages itis actively using.In practice, choice of k doesn't mattersignificantly because of locality of referencesthe set of pages used by the k most recentmemory referencesprograms spend significant time in same placeIf entire working set is loaded, no page faultsNext time: Working Set AlgorithmThrashing occurs when there is insufficientmemory for the working setEvangelos P. Markatos. 1997. Visualizing working sets.SIGOPS Oper. Syst. Rev. 31, 4 (Oct. 1997), 3–11.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 4 4k241518232417182418171715241724182424, 1515, 1818, 2323, 2424, 1717, 1818, 24-18, 171717, 1515, 2424, 17-24, 182424, 1524, 15, 1815, 18, 2318, 23, 2423, 24, 1724, 17, 18-18, 2424, 18, 1718, 1717, 1517, 15, 24-24, 1717, 24, 182424, 1524, 15, 1824, 15, 18, 23-18, 23, 24, 17-24, 17, 18---18, 17, 1517, 15, 24--17, 24, 182424, 1524, 15, 1824, 15, 18, 23-15, 18, 23, 24, 1718, 23, 24, 17-24, 17, 18--24, 18, 17, 15-17, 15, 24-15, 17, 24, 18referencesk = 2k = 3k = 4k = 5Thrashing