Lecture 12:Lecture 12:Demand PagingDemand PagingCSE 120: Principles of Operating SystemsAlex C. SnoerenHW 3 due 11/10CSE 120 – Lecture 122Memory ManagementMemory ManagementLast lecture on memory management: Goals of memory management◆ To provide a convenient abstraction for programming◆ To allocate scarce memory resources among competingprocesses to maximize performance with minimal overhead Mechanisms◆ Physical and virtual addressing (1)◆ Techniques: Partitioning, paging, segmentation (1)◆ Page table management, TLBs, VM tricks (2) Policies◆ Page replacement algorithms (3)CSE 120 – Lecture 123Lecture OverviewLecture Overview Review paging and page replacement Survey page replacement algorithms Discuss local vs. global replacement Discuss thrashingCSE 120 – Lecture 124LocalityLocality All paging schemes depend on locality◆ Processes reference pages in localized patterns Temporal locality◆ Locations referenced recently likely to be referenced again Spatial locality◆ Locations near recently referenced locations are likely to bereferenced soon Although the cost of paging is high, if it is infrequentenough it is acceptable◆ Processes usually exhibit both kinds of locality during theirexecution, making paging practicalCSE 120 – Lecture 125Demand Paging (OS)Demand Paging (OS) Recall demand paging from the OS perspective:◆ Pages are evicted to disk when memory is full◆ Pages loaded from disk when referenced again◆ References to evicted pages cause a TLB miss» PTE was invalid, causes fault◆ OS allocates a page frame, reads page from disk◆ When I/O completes, the OS fills in PTE, marks it valid, andrestarts faulting process Dirty vs. clean pages◆ Actually, only dirty pages (modified) need to be written to disk◆ Clean pages do not – but you need to know where on disk toread them from againCSE 120 – Lecture 126Demand Paging (Process)Demand Paging (Process) Demand paging is also used when a process firststarts up When a process is created, it has◆ A brand new page table with all valid bits off◆ No pages in memory When the process starts executing◆ Instructions fault on code and data pages◆ Faulting stops when all necessary code and data pages are inmemory◆ Only code and data needed by a process needs to be loaded◆ This, of course, changes over time…CSE 120 – Lecture 127Page ReplacementPage Replacement When a page fault occurs, the OS loads the faultedpage from disk into a page frame of memory At some point, the process has used all of the pageframes it is allowed to use◆ This is likely less than all of available memory When this happens, the OS must replace a page foreach page faulted in◆ It must evict a page to free up a page frame The page replacement algorithm determines how thisis done◆ And they come in all shapes and sizesCSE 120 – Lecture 128Evicting the Best PageEvicting the Best Page The goal of the replacement algorithm is to reduce thefault rate by selecting the best victim page to remove The best page to evict is the one never touched again◆ Will never fault on it Never is a long time, so picking the page closest to“never” is the next best thing◆ Evicting the page that won’t be used for the longest period oftime minimizes the number of page faults◆ Proved by Belady We’re now going to survey various replacementalgorithms, starting with Belady’sCSE 120 – Lecture 129BeladyBelady’’s s AlgorithmAlgorithm Belady’s algorithm is known as the optimal pagereplacement algorithm because it has the lowest faultrate for any page reference stream◆ Idea: Replace the page that will not be used for the longesttime in the future◆ Problem: Have to predict the future Why is Belady’s useful then? Use it as a yardstick◆ Compare implementations of page replacement algorithmswith the optimal to gauge room for improvement◆ If optimal is not much better, then algorithm is pretty good◆ If optimal is much better, then algorithm could use some work» Random replacement is often the lower boundCSE 120 – Lecture 1210First-In First-Out (FIFO)First-In First-Out (FIFO) FIFO is an obvious algorithm and simple to implement◆ Maintain a list of pages in order in which they were paged in◆ On replacement, evict the one brought in longest time ago Why might this be good?◆ Maybe the one brought in the longest ago is not being used Why might this be bad?◆ Then again, maybe it’s not◆ We don’t have any info to say one way or the other FIFO suffers from “Belady’s Anomaly”◆ The fault rate might actually increase when the algorithm isgiven more memory (very bad)CSE 120 – Lecture 1211BeladyBelady’’s s Anomaly Anomaly w/FIFOw/FIFOPageReferences1234125123451 1232341412525121234345121 1212342341341251253253491 12123123452345134512451234123452310CSE 120 – Lecture 1212Least Recently Used (LRU)Least Recently Used (LRU) LRU uses reference information to make a moreinformed replacement decision◆ Idea: We can’t predict the future, but we can make a guessbased upon past experience◆ On replacement, evict the page that has not been used for thelongest time in the past (Belady’s: future)◆ When does LRU do well? When does LRU do poorly? Implementation◆ To be perfect, need to time stamp every reference (ormaintain a stack) – much too costly◆ So we need to approximate itCSE 120 – Lecture 1213Approximating LRUApproximating LRU LRU approximations use the PTE reference bit◆ Keep a counter for each page◆ At regular intervals, for every page do:» If ref bit = 0, increment counter» If ref bit = 1, zero the counter» Zero the reference bit◆ The counter will contain the number of intervals since the lastreference to the page◆ The page with the largest counter is the least recently used Some architectures don’t have a reference bit◆ Can simulate reference bit using the valid bit to induce faults◆ What happens when we make a page invalid?CSE 120 – Lecture 1214LRU ClockLRU Clock Not Recently Used (NRU) – Used by Unix◆ Replace page that is “old enough”◆ Arrange all of physical page frames in a big circle (clock)◆ A clock hand is used to select a good LRU candidate» Sweep through the pages in circular order like a clock» If the ref bit is off, it hasn’t been used recently What is the minimum “age” if ref bit is off?» If the ref bit is on, turn it off and
View Full Document