1 / 28

4.4 Page replacement algorithms

4.4 Page replacement algorithms. Page replacement algorithms. Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches. Optimal page replacement. Page fault occurs. Scan all pages currently in memory.

jael
Download Presentation

4.4 Page replacement algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4.4 Page replacement algorithms

  2. Page replacement algorithms • Also seen in: • CPU cache • Web server cache of web pages • Buffered I/O (file) caches

  3. Optimal page replacement • Page fault occurs. • Scan all pages currently in memory. • Determine which page won’t be needed (referenced) until furthest in the future. • Replace that page. • Not really possible. (But useful as a benchmark.) • Depends on code as well as data.

  4. Algorithms we will discuss: • Optimal • NRU • FIFO • Second chance • Clock • LRU • NFU • Aging • Working set • WSClock

  5. NRU (not recently used) • Bits set by hardware after every memory reference • Cleared only by software (OS) • R bits – set when page is referenced (read or write) • M bits – set when page is modified (written) • Periodically (after k clock interrupts), R bits are cleared

  6. NRU page categories • Not referenced, not modified • Not referenced, modified • (only occurs when #4’s R bit is cleared during clock interrupt) • Referenced, not modified • Referenced, modified NRU algorithm: remove random page from lowest numbered non empty category

  7. NRU algorithm evaluation • Simple • Efficient • Not optimal but adequate

  8. FIFO page replacement • Queue pages as they are requested. • Remove page at head (front) of queue. • Oldest page is removed first + simple/efficient - might remove a heavily used page

  9. Second chance page replacement • Inspect R bit of oldest page • Recall: R bits are set when page is referenced (read or write); periodically (after k clock interrupts), R bits are cleared. • If R==0 then • page is old & unused so replace it • Else • Clear R bit • Move page from head to tail of FIFO • (treating it as a newly loaded page) • Try a different page

  10. Second chance page replacement load time page

  11. Clock page replacement • Circular list instead of queue • Clock hand points to oldest page • If (R==0) then • Page is unused so replace it • Else • Clear R • Advance clock hand • (very similar to second chance – queue instead of list)

  12. Clock page replacement

  13. LRU (least recently used) page replacement • A page recently used is likely to be used in the near future. • A page not used in ages is not likely to be used in the near future. • Algorithm: • “age” the pages • Maintain a queue of pages in memory. • Recently used at front; oldest at rear. • Every time a page is referenced, it is removed from the queue and placed at the front of the queue. • This is slow!

  14. LRU in hardware • Implementation #1: • 64 bit counter, C, incremented after every instruction • Each page also has a 64 bit counter • When a page is referenced, C is copied to its counter. • Page with lowest counter is oldest.

  15. LRU in hardware • Implementation #2: • Given n page frames, let M be a nxn matrix of bits initially all 0. • Reference to page frame k occurs. • Set all bits in row k of M to 1. • Set all bits in column k of M to 0. • Row with lowest binary value is least recently used.

  16. LRU in hardware: implementation #2 example oldest

  17. NFU (Not Frequently Used) • Hardware doesn’t often support LRU. • Software counter associated w/ each page initially set to 0. • At each clock interrupt: • Add R bit (either 0 or 1) to the counter for each page. • Page with lowest counter is NFU.

  18. NFU problem • It never forgets! • So pages that were frequently referenced (during initialization for example) but are no longer needed appear to be FU. • Solution (called “aging”): • Shift all counters to right 1 bit before R bit is added in. • Then R bit is added to MSb (leftmost bit) instead of LSb (rightmost bit). • Page w/ lowest value is chosen for removal.

  19. NFU w/ aging • Shift to right • MSb = R 00010000

  20. Differences between LRU and NFU • LRU updated after every instruction so it’s resolution is very fine. • NFU is coarse (updated after n instructions execute between clock interrupts). • A given page referenced by n-1 instruction is given equal weight to a page referenced by only 1 instruction (between clock interrupts). • n/2 references to a given page at the beginning of the interval are given equal weight with n/2 references to another page at the end of the interval.

  21. Working set page replacement algorithm • Demand paging = start up processes with 0 pages and only load what’s needed. • Locality of reference = during any phase of execution, the process references only a relatively small fraction of its pages. • Working set = set of pages that a process is currently using. • Thrashing = causing a page fault every few instructions.

  22. Working sets • Working set model = make sure a page is in memory before the process needs it. • a.k.a. prepaging • w.s. = set of pages used in the k most recent memory references.

  23. Working set algorithm • Uses current virtual time (CVT) = amount of CPU time a process has actually used since it started. • T is a threshold on CVT • R and M bits as before; clock interrupt

  24. Working set algorithm age = current virtual time (i.e., time of last use)  (greatest age/least virtual time) and choose that one if no better candidate exists. If no suitable candidate exists, pick one at random.

  25. WSClock page replacement • Previous WS algorithm requires entire page table be scanned at each page fault. • WSClock: • Simple, efficient, widely used. • Uses circular list of page frames.

  26. WSClock page replacement At each page fault… Loop once through page table: Examine PTE pointed to by clock hand. If r bit == 1 then clear r bit; advance clock hand; goto loop else If age>t If page is clean then use this page! Else write dirty page to disk; advance clock hand; goto loop If write scheduled, wait for completion and used that page. Else pick a victim at random.

  27. WSClock page replacement clear r bit and advance clock hand. Replace old and advance.

  28. Summary of page replacement algorithms In practice, random page replace typically performs better than FIFO but worse than LRU.

More Related