1
play

1 Clock algorithm Least Recently Used (LRU) Same functionality as - PDF document

Page replacement algorithms Page fault forces a choice No room for new page (steady state) Which page must be removed to make room for an Chapter 4: Memory Management incoming page? How is a page removed from physical memory?


  1. Page replacement algorithms � Page fault forces a choice � No room for new page (steady state) � Which page must be removed to make room for an Chapter 4: Memory Management incoming page? � How is a page removed from physical memory? � If the page is unmodified, simply overwrite it: a copy Part 2: Paging Algorithms and already exists on disk Implementation Issues � If the page has been modified, it must be written back to disk: prefer unmodified pages? � Better not to choose an often used page � It’ll probably need to be brought back in soon Chapter 4 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) 2 Optimal page replacement algorithm Not-recently-used (NRU) algorithm Each page has reference bit and dirty bit � What’s the best we can possibly do? � � Bits are set when page is referenced and/or modified � Assume perfect knowledge of the future Pages are classified into four classes � � 0: not referenced, not dirty � Not realizable in practice (usually) � 1: not referenced, dirty � Useful for comparison: if another algorithm is within 5% � 2: referenced, not dirty of optimal, not much more can be done… � 3: referenced, dirty Clear reference bit for all pages periodically � � Algorithm: replace the page that will be used furthest � Can’t clear dirty bit: needed to indicate which pages need to be flushed to disk in the future � Class 1 contains dirty pages where reference bit has been cleared Algorithm: remove a page from the lowest non-empty class � � Only works if we know the whole sequence! � Select a page at random from that class � Can be approximated by running the program twice � Easy to understand and implement � Once to generate the reference trace Performance adequate (though not optimal) � � Once (or more) to apply the optimal algorithm � Nice, but not achievable in real systems! CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Chapter 4 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Chapter 4 3 4 First-In, First-Out (FIFO) algorithm Second chance page replacement � Maintain a linked list of all pages � Modify FIFO to avoid throwing out heavily used pages � If reference bit is 0, throw the page out � Maintain the order in which they entered memory � If reference bit is 1 � Page at front of list replaced � Reset the reference bit to 0 � Move page to the tail of the list � Advantage: (really) easy to implement � Continue search for a free page � Disadvantage: page in memory the longest may be � Still easy to implement, and better than plain FIFO often used referenced unreferenced � This algorithm forces pages out regardless of usage � Usage may be helpful in determining which pages to keep A B C D E F G H A t=0 t=4 t=8 t=15 t=21 t=22 t=29 t=30 t=32 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Chapter 4 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Chapter 4 5 6 1

  2. Clock algorithm Least Recently Used (LRU) � Same functionality as � Assume pages used recently will used again soon A A second chance t=32 t=0 � Throw out page that has been unused for longest time H B B � Simpler implementation t=30 t=32 t=4 � Must keep a linked list of pages � “Clock” hand points to next � Most recently used at front, least at rear page to replace G C C � Update this list every memory reference! � If R=0, replace page t=29 t=32 t=8 � If R=1, set R=0 and advance � This can be somewhat slow: hardware has to update a linked list the clock hand on every reference! F D J � Continue until page with � Alternatively, keep counter in each page table entry t=22 t=32 t=15 E R=0 is found � Global counter increments with each CPU cycle t=21 � This may involve going all � Copy global counter to PTE counter on a reference to the the way around the clock… page referenced unreferenced � For replacement, evict page with lowest counter value Chapter 4 Chapter 4 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) 7 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) 8 Simulating LRU in software Aging replacement algorithm Few computers have the necessary hardware to implement full LRU Reduce counter values over time � � � Divide by two every clock cycle (use right shift) � Linked-list method impractical in hardware � More weight given to more recent references! � Counter-based method could be done, but it’s slow to find the desired page � Select page to be evicted by finding the lowest counter value Approximate LRU with Not Frequently Used (NFU) algorithm � Algorithm is: � � At each clock interrupt, scan through page table � Every clock tick, shift all counters right by 1 bit � If R=1 for a page, add one to its counter value � On reference, set leftmost bit of a counter (can be done by copying the � On replacement, pick the page with the lowest counter value reference bit to the counter at the clock tick) Problem: no notion of age—pages with high counter values will tend to � Referenced keep them! Tick 0 Tick 1 Tick 2 Tick 3 Tick 4 this tick Page 0 10000000 11000000 11100000 01110000 10111000 Page 1 00000000 10000000 01000000 00100000 00010000 Page 2 10000000 01000000 00100000 10010000 01001000 Page 3 00000000 00000000 00000000 10000000 01000000 Page 4 10000000 01000000 10100000 11010000 01101000 Page 5 10000000 11000000 01100000 10110000 11011000 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Chapter 4 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Chapter 4 9 10 Working set How big is the working set? Demand paging : bring a page into memory when it’s requested by the � process w(k,t) � How many pages are needed? � Could be all of them, but not likely � Instead, processes reference a small set of pages at any given time— locality of reference � Set of pages can be different for different processes or even different times in k the running of a single process Set of pages used by a process in a given interval of time is called the � � Working set is the set of pages used by the k most recent working set memory references � If entire working set is in memory, no page faults! � w(k,t) is the size of the working set at time t � If insufficient space for working set, thrashing may occur � Goal: keep most of working set in memory to minimize the number of page � Working set may change over time faults suffered by a process � Size of working set can change over time as well… CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Chapter 4 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Chapter 4 11 12 2

Recommend


More recommend