ENع
←Page replacement
Lesson 35 · Memory management

Page replacement: FIFO, LRU and clock

14 min11 steps6 questionsIntermediate
About this lesson

When a page fault needs a frame of RAM and every frame is full, the kernel must evict a page to make room. Which page it picks decides how many slow disk reads the program suffers. Follow one program with three frames through the same nine page touches under three policies: FIFO evicts the oldest arrival, LRU the page unused the longest, and clock approximates LRU cheaply with one hardware use bit per frame. The lesson Page faults and swapping to disk covers how a fault is handled and how pages move to and from swap; here we look only at the choice of victim.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 35 · Interactive
01 / 11

More pages than frames

A program uses four pages, numbered 0 to 3, but the kernel has given it only three frames of RAM: A, B and C. Every page has a copy on disk, in swap. A page must sit in a frame to be used.

pages    0 1 2 3          (4 pages of the program)
frames   A B C            (3 frames of RAM)
disk     a copy of every page, in swap

a page fault that must read the disk is a "major" fault.
a program can count its own faults (C99, POSIX + BSD/Linux):

#include <stdio.h>
#include <sys/resource.h>

int main(void) {
    struct rusage u;
    if (getrusage(RUSAGE_SELF, &u) != 0) return 1;
    printf("minor faults: %ld\n", u.ru_minflt);
    printf("major faults: %ld\n", u.ru_majflt);
    return 0;
}
Loading figure…
or ← →
Read the lesson step by step
  1. More pages than frames

    A program uses four pages, numbered 0 to 3, but the kernel has given it only three frames of RAM: A, B and C. Every page has a copy on disk, in swap. A page must sit in a frame to be used.

    pages    0 1 2 3          (4 pages of the program)
    frames   A B C            (3 frames of RAM)
    disk     a copy of every page, in swap
    
    a page fault that must read the disk is a "major" fault.
    a program can count its own faults (C99, POSIX + BSD/Linux):
    
    #include <stdio.h>
    #include <sys/resource.h>
    
    int main(void) {
        struct rusage u;
        if (getrusage(RUSAGE_SELF, &u) != 0) return 1;
        printf("minor faults: %ld\n", u.ru_minflt);
        printf("major faults: %ld\n", u.ru_majflt);
        return 0;
    }
  2. The reference string

    The program touches one page per tick, in this order, its reference string: 0 1 2 0 1 3 1 0 1. A hit: the page is in a frame. A page fault: it is not, so the kernel reads it from disk.

    reference string   0 1 2 0 1 3 1 0 1
    tick               1 2 3 4 5 6 7 8 9
    
    hit     the page is in a frame: fast, no kernel work
    fault   it is not: the kernel reads it from disk
            (OSTEP: about 100 ns for RAM, 10 ms for a disk)
    
    goal of every policy: as few faults as possible
  3. Ticks 1 to 5: filling up

    Ticks 1 to 3: pages 0, 1 and 2 fault, because memory starts empty. The kernel copies each one from disk into a free frame. Ticks 4 and 5: pages 0 and 1 are already in frames, so both are hits.

    tick  page  result   frames A B C
      1   0     fault    0 - -
      2   1     fault    0 1 -
      3   2     fault    0 1 2
      4   0     hit      0 1 2
      5   1     hit      0 1 2
    
    no frame was full yet, so no page had to leave:
    every policy does exactly this
  4. Tick 6: memory is full

    Tick 6 touches page 3. It faults, and all three frames are full, so the kernel must evict one page, the victim. The replacement policy picks it. The ideal pick is the page needed furthest ahead, but the kernel cannot see ahead.

    tick 6   page 3   fault   frames A B C = 0 1 2 (all full)
    
    which page leaves?   that is the replacement policy
    
    optimal (Belady's MIN): evict the page whose next use
    is furthest in the future. Here page 2 is never used
    again. MIN gets 5 hits, the most possible, but needs the future.
    
    real policies guess the future from the past.
  5. FIFO: oldest arrival leaves

    FIFO, first in first out, evicts the page that arrived first. Each frame notes when its page came in. Page 0 came in at tick 1, the earliest, so it leaves, even though tick 4 just used it. Page 3 takes frame A.

    FIFO   each frame notes when its page came in
    
    frame  page  came in
      A     0     t1     <- earliest: victim
      B     1     t2
      C     2     t3
    
    page 0 leaves; page 3 is read into frame A (in t6)
    a hit never changes the note: use does not count
  6. FIFO pays for it

    Tick 7: page 1 hits. Tick 8: page 0 faults, right after FIFO threw it out, and evicts page 1, now the oldest. Tick 9: page 1 faults again and evicts page 2. FIFO ends with 3 hits and 6 faults.

    tick  page  result   victim   frames A B C
      6   3     fault    page 0   3 1 2
      7   1     hit      -   3 1 2
      8   0     fault    page 1   3 0 2
      9   1     fault    page 2   3 0 1
    
    FIFO   3 hits   6 faults
  7. LRU: longest unused leaves

    Same string, new policy. LRU, least recently used, evicts the page left unused the longest. Each frame notes its page’s last use, updated on every hit. At tick 6, page 2 was last used at tick 3, the earliest, so page 2 leaves.

    LRU    each frame notes when its page was last used
           (ticks 4 and 5 moved pages 0 and 1 forward)
    
    frame  page  last used
      A     0     t4
      B     1     t5
      C     2     t3     <- earliest: victim
    
    page 2 leaves; page 3 is read into frame C (used t6)
  8. LRU keeps what is used

    Ticks 7, 8 and 9 touch pages 1, 0 and 1. All are still in memory, so all three hit. LRU ends with 5 hits and 4 faults. Here that equals the best possible, because page 2 was never needed again.

    tick  page  result   frames A B C   last used
      6   3     fault    0 1 3          t4 t5 t6
      7   1     hit      0 1 3          t4 t7 t6
      8   0     hit      0 1 3          t8 t7 t6
      9   1     hit      0 1 3          t8 t9 t6
    
    LRU    5 hits   4 faults
    MIN    5 hits   (the best possible here)
  9. Clock: a cheap LRU

    Exact LRU updates a note on every memory access: too slow. Instead, hardware sets a frame’s use bit on each touch. Clock sweeps a hand: a 1 is cleared and skipped, a 0 evicted. At tick 6 all are 1, so page 0 goes.

    use bit   set to 1 by the hardware on every access
              (the accessed bit in the page table entry);
              only the kernel clears it
    
    tick 6: page 3 faults, hand at A, bits A B C = 1 1 1
      A: 1 -> clear, move on
      B: 1 -> clear, move on
      C: 1 -> clear, move on
      A: 0 -> evict page 0, load page 3, bit 1
    a full lap: this time clock picks what FIFO picked
  10. Page 1 gets a second chance

    Tick 7: page 1 hits, so B's bit becomes 1. Tick 8: page 0 faults. The hand is at B, bit 1: page 1 stays, its bit cleared. C's bit is 0: page 2 leaves. Clock: 4 hits, 5 faults.

    tick 7   page 1 hit     bits A B C = 1 1 0
    tick 8   page 0 fault   hand at B
      B: 1 -> clear (second chance for page 1)
      C: 0 -> evict page 2, load page 0, bit 1
    tick 9   page 1 hit     page 1 was kept: no fault
    
    clock  4 hits   5 faults
  11. Three rules, one string

    Same nine touches: FIFO 3 hits, clock 4, LRU 5, the best possible here. Clock costs one bit per frame yet beats FIFO. That is why real kernels, Linux included, approximate LRU with the hardware's accessed bits.

    the three policies as a Python simulation:
    
    refs = [0, 1, 2, 0, 1, 3, 1, 0, 1]
    N = 3                                  # frames
    
    def fifo(refs):
        mem, hits = [], 0                  # oldest first
        for p in refs:
            if p in mem: hits += 1; continue
            if len(mem) == N: mem.pop(0)   # evict first in
            mem.append(p)
        return hits
    
    def lru(refs):
        mem, hits = [], 0                  # least recent first
        for p in refs:
            if p in mem: hits += 1; mem.remove(p)
            elif len(mem) == N: mem.pop(0) # evict least recent
            mem.append(p)                  # now most recent
        return hits
    
    def clock(refs):
        page, use, hand, hits = [], [], 0, 0
        for p in refs:
            if p in page: hits += 1; use[page.index(p)] = 1; continue
            if len(page) < N: page.append(p); use.append(1); continue
            while use[hand]:                # second chance
                use[hand] = 0; hand = (hand + 1) % N
            page[hand], use[hand] = p, 1    # evict, load
            hand = (hand + 1) % N
        return hits
    
    for f in (fifo, lru, clock):
        h = f(refs)
        print(f.__name__, h, "hits", len(refs) - h, "faults")
    
    prints:
    fifo 3 hits 6 faults
    lru 5 hits 4 faults
    clock 4 hits 5 faults

Quiz Unlocks after the last step.