CPS 356 Lecture notes: Page Replacement Algorithms



Coverage: [OSCJ] §9.4 (pp. 405-418)


Overview

  • is it possible to over-allocate memory?
  • page fault options
    • terminate process
    • swap out a process --- reduces the degree of multiprogramming


Basic Page Replacement

see Figs. 9.9 & 9.10
  1. find the location of the desired page on disk

  2. find a free frame:
    1. if there is a free frame, use it
    2. if there is no free frame, use a page-replacement algorithm to select a victim frame
    3. write the victim frame to disk; change the page and frame tables accordingly

  3. read the desired page into the newly freed frame; change the page and frame tables

  4. restart the user process
  5. If no frames are free, "two" pages transfers (one in and one out) are required, which doubles the page-fault service time.

    This a lot of overhead without even mentioning the time spent waiting on the paging device.

    Can reduce it by using a "dirty bit" (applies to read-only pages, such as program code). This scheme can reduce I/O time by 1/2 if the page has not been modified.

    Two major issues with demand paging which must be addressed:

    • page-replacement algorithm: how to select a page to be replaced)
    • frame-allocation algorithm: how many frames to allocate to each process)

    These are important problems because disk I/O is so expensive.


Page Faults

Let's start with page-replacement algorithms.

How to evaluate algorithms?

We want one which yields the lowest page-fault rate.

Generally, as #frames increase, #page faults should decrease (see Fig. 9.11)

Given a string of memory references (called a reference string), count the number of page faults:

0100, 0432, 0101, 0612, 0102, 0103, 0104, 0101, 0611, 0102, 0103, 0104, 0101, 0610, 0102, 0103, 0104, 0101, 0609, 0102, 0105

Assuming 100 bytes/page, we have:

1, 4, 1, 6, 1, 6, 1 , 6, 1 , 6, 1

With 3 or more frames: ?

1 frame: ?


Page-Replacement Algorithms

  • first in, first out (FIFO)
  • optimal (OPT)
  • least recently used (LRU)

Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1


FIFO Page Replacement

  • run prior reference string with 3 frames of memory (Fig. 9.12): ?
  • easy to understand and implement
  • poor performance
    • initialization module
    • variable initialized early and used often

  • everything will work, but a bad replacement choice
  • increases the page fault rate and slows process execution
  • it does not result in incorrect execution

  • Belady's anomaly (Fig. 9.13): add more memory results in more page faults; consider reference string: 1,2,3,4,1,2,5,1,2,3,4,5 with 3 frames of memory and then 4


Optimal Page Replacement

  • replace the page which will not be used for the longest period of time (akin to A* search)
  • run prior reference string with 3 frames of memory (Fig. 9.14): ?
  • 9 (OPT) vs. 15 (FIFO) or really 6 (OPT) vs. 12 (FIFO) (twice as good!)
  • has lowest page fault rate, and
  • never suffers from Belady's anomaly
  • problem? recall SJF?
  • mainly used for comparison purposes (e.g., algorithm X is within 12.3% of optimal in the worst case)


LRU Page Replacement

  • idea is to try to approximate the optimum
  • key difference between FIFO and OPT (other than looking backward vs. forward in time, resp.)
    • FIFO uses the time when a page was brought into memory
    • OPT uses the time when a page is to be used
  • use recent page as an approximation of future, then we can replace the page which "has not been used" for the longest period of time
  • run prior reference string with 3 frames of memory (Fig. 9.15): ?
  • associate with each page the time of its last use
  • does not suffer from Belady's anomaly
  • major issue is implementation (need hardware support):
    • counters
      • every time a reference is made, copy value of clock register into page table entry
      • requires a search of the page table and a write to memory (time of use) for each memory access
    • stack
      • when a reference is made, remove page number from stack and push onto stack
      • most recently used page is always on top
      • least recently used page is always on bottom
      • use doubly-linked-list with head and tail pointers to implement
      • no search for replacement page number
      • Fig. 9.16
  • LRU belongs to a class of page-replacement algorithms known as stack algorithms which never exhibit Belady's anomaly.

    A stack algorithm is one for which it can be shown that the set of pages in memory for n frames is always a subset of the set of pages which would be in memory with n+1 frames.

    Need hardware support. Is too slow to simulate in software.


LRU-Approximation Page Replacement

    Often we have a reference bit associated with each entry in the page table. Can determine which pages have been used and not used by examining the reference bits, although we do not know the "order" of use.

    Can we approximate order?

    Additional-reference bits algorithm:

      00000000
      11111111

      110001000 has been used more recently than one with 01110111

    If we interpret these bytes as unsigned integers, the page with the lowest value is the LRU page.

    Problem: all numbers not unique.

    Solution: swap out all or use FIFO.


Clock Algorithms

(or second-chance page-replacement algorithms)
  • a FIFO replacement algorithm
  • number of bits used is 0; leaving only the reference bit
  • if 0, replace
  • if 1, give page a 2nd chance and move onto next FIFO page; reset reference bit to 0 and reset arrival time to current time
  • a page given a second chance will not be replaced until all other pages have been replaced (FIFO) or given second chances
  • Fig. 9.17
  • if a page is used often enough to keep its reference bit set, it will never be replaced

  • implementation: the clock algorithm using a circular queue
    • a pointer (hand on a clock) indicates which page is to be replaced next
    • when a frame is needed, the pointer advances until it finds a page with a 0 reference bit
    • as it advances, it clears the reference bits
    • once a victim page is found, the page is replaced, and the new page is inserted in the circular queue in that position
    • degenerates to FIFO replacement if all bits are set


Enhanced Second-chance Algorithm

  • by considering the reference bit and dirty bit as an ordered pair, we now have four classes:
    • (0,0): neither recently used nor modified --- best page to replace
    • (0,1): not recently used but modified --- not quite as good, because the page will need to be written out before replacement
    • (1,0): recently used, but clean --- probably will be used again soon
    • (1,1): recently used and modified --- probably will be used again soon, and the page will need to be written out to disk before it can be replaced

  • use same scheme as above, but rather than just checking if the reference bit is set to 1, we examine the class,
  • replace the first page encountered in the lowest non-empty class
  • may have to search circular queue several times before we find a page to be replaced
  • difference with above algorithm is that here we give preference to those pages which have been modified to reduce the number of I/Os required.


Further Information

  • counting-based page replacement algorithms
    • least frequently used (LFU) page-replacement algorithm
    • most frequently used (MFU) page-replacement algorithm

  • page-buffering algorithms
  • applications and page replacement


References

    [OSCJ] A. Silberschatz, P.B. Galvin, and G. Gagne. Operating Systems Concepts with Java. John Wiley and Sons, Inc., Eighth edition, 2010.

Return Home