When a page fault occurs and no frame is free, the OS must choose a page to remove. The choice greatly affects the number of page faults.

Page Replacement

If no frame is free, a victim page is chosen and written to disk if it has been changed (its dirty bit is set), and the new page is read into its frame. A good algorithm gives the lowest page-fault rate. Algorithms are compared by counting faults on a reference string, a sequence of page numbers.

Definition: Page replacement. The selection of a page in memory to be removed, when a page fault occurs and no frame is free, so that the needed page can be brought in.

FIFO, Optimal and LRU

FIFO replaces the page that has been in memory the longest. It is simple but may remove a heavily used page. It suffers from Belady's anomaly: for some reference strings, giving more frames increases the number of faults. Optimal (OPT) replaces the page that will not be used for the longest time in the future. It gives the fewest faults possible, but it needs future knowledge, so it is used only as a benchmark. Least Recently Used (LRU) replaces the page that has not been used for the longest time in the past, using the recent past to predict the near future. It is close to optimal and has no Belady's anomaly, but it needs hardware help, such as counters or a stack, so it is often approximated by reference bits and the second-chance (clock) algorithm.

Allocation of Frames

Each process needs a minimum number of frames, fixed by the instruction set. Frames may be allocated by equal allocation, each of n processes getting m/n frames, or by proportional allocation, in proportion to process size: a(i) = (s(i) / S) × m. Priorities can also be used. With global replacement, a process may take a frame from any process. With local replacement, it replaces only its own frames.

Thrashing

If a process does not have enough frames for the pages it is actively using, it faults very often, replacing a page that it needs again at once. It spends more time paging than executing: this is thrashing. As the degree of multiprogramming rises, CPU utilisation rises, until thrashing sets in and it falls sharply. The OS may then wrongly add more processes, making it worse. Cures are the working-set model, which gives each process enough frames for the pages it has used in a recent window Δ, and page-fault frequency control, which adds frames to a process whose fault rate is high and takes them from one whose rate is low. Suspending some processes also helps.

Table 4.5: Page replacement algorithms

Algorithm Replaces Note
FIFO Oldest page in memory Belady's anomaly
Optimal Page not used for the longest future time Best, needs future
LRU Page not used for the longest past time Near optimal, needs hardware

Example 4.5: Counting page faults

Problem: Reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 3 frames. Count the faults under FIFO, Optimal and LRU.

Solution:

  1. All three fault on 7, 0, 1 (empty frames) and on 2.
  2. FIFO continues by replacing the oldest page and gives 10 faults.
  3. Optimal looks ahead and gives 7 faults.
  4. LRU looks back and gives 9 faults.

Result: FIFO 10, Optimal 7, LRU 9 faults.

#include <stdio.h>
int ref[] = {7,0,1,2,0,3,0,4,2,3,0,3,2}, n = 13, F = 3;
int sim(int mode) {
    int fr[3], last[3], faults = 0, used = 0;
    for (int t = 0; t < n; t++) {
        int hit = -1;
        for (int j = 0; j < used; j++) if (fr[j] == ref[t]) hit = j;
        if (hit >= 0) { if (mode == 2) last[hit] = t; continue; }
        faults++;
        int v;
        if (used < F) v = used++;
        else {
            v = 0;
            for (int j = 1; j < F; j++) {
                if (mode == 1) {
                    int nj = n, nv = n;
                    for (int k = t + 1; k < n; k++)
                        if (ref[k] == fr[j]) { nj = k; break; }
                    for (int k = t + 1; k < n; k++)
                        if (ref[k] == fr[v]) { nv = k; break; }
                    if (nj > nv) v = j;
                } else if (last[j] < last[v]) v = j;
            }
        }
        fr[v] = ref[t]; last[v] = t;
    }
    return faults;
}
int main(void) {
    printf("FIFO: %d faults\n", sim(0));
    printf("OPT : %d faults\n", sim(1));
    printf("LRU : %d faults\n", sim(2));
    return 0;
}

Output

FIFO: 10 faults
OPT : 7 faults
LRU : 9 faults

Common mistakes

  • Thinking that more frames always means fewer faults with FIFO (Belady's anomaly).
  • Confusing Optimal (future) with LRU (past).
  • Adding more processes when CPU utilisation drops because of thrashing.

Key takeaways

  • FIFO replaces the oldest page, Optimal the one used farthest ahead, LRU the least recently used.
  • Belady's anomaly affects FIFO, not LRU or Optimal.
  • Frames are allocated equally or proportionally, with local or global replacement.
  • Thrashing is excessive paging, cured by the working-set model or fault-frequency control.

Practice questions

  1. What is Belady's anomaly?
  2. Why is the Optimal algorithm not used in practice?
  3. What is thrashing?

Answers to the practice questions

  1. An increase in page faults when the number of frames is increased, seen with FIFO.
  2. It needs to know future page references.
  3. A state in which a process spends more time paging than executing because it has too few frames.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit IV