Loading lessons...
Page Replacement Algorithms (FIFO, LRU, Optimal) & Belady's Anomaly: Numericals
Page Replacement Algorithms (FIFO, LRU, Optimal) & Belady's Anomaly: Numericals
When a page fault occurs and every block of main memory is full, one page must be removed to make room. The choice matters: removing a page that is needed again soon causes another fault. This lesson works through the three standard page replacement algorithms with full traces, and shows why FIFO can get worse with more memory.
1. The Replacement Problem
On a page fault the operating system:
- finds the wanted page on disk,
- finds a free block; if none is free, uses the replacement algorithm to choose a victim page,
- writes the victim back to disk only if it was modified (its dirty bit is set); otherwise its copy on disk is still correct,
- reads the wanted page into the block and updates the page table.
Algorithms are compared by counting page faults on a reference string: the list of page numbers a program uses, in order. Fewer faults means a higher hit ratio.
Hit ratio = (references − faults) / references.
2. FIFO (First-In, First-Out)
Replace the page that has been in memory longest. The pages form a queue: a new page joins the back and the victim is taken from the front. It is easy to implement but ignores how often a page is used; a heavily used page that came in early is still removed.
Reference string 4 2 0 1 2 6 1 4 0 1 0 2 3 5 7 with 3 blocks. Count the page faults.
| Ref | 4 | 2 | 0 | 1 | 2 | 6 | 1 | 4 | 0 | 1 | 0 | 2 | 3 | 5 | 7 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Block 1 | 4 | 4 | 4 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 3 | 3 | 3 |
| Block 2 | 2 | 2 | 2 | 2 | 6 | 6 | 6 | 6 | 1 | 1 | 1 | 1 | 5 | 5 | |
| Block 3 | 0 | 0 | 0 | 0 | 0 | 4 | 4 | 4 | 4 | 2 | 2 | 2 | 7 | ||
| Fault | F | F | F | F | F | F | F | F | F | F | F | F |
At reference 1 the oldest page is 4 (loaded first), so 1 replaces it; at 6 the oldest is 2, and so on.
3. Optimal (OPT)
Replace the page that will not be used for the longest time in the future. This gives the fewest possible faults for any string, but it needs to know the future, so it cannot be built. It is used as a yardstick: an algorithm is judged by how close it gets to OPT.
4. LRU (Least Recently Used)
Replace the page that has not been used for the longest time in the past. It uses the recent past as a guess of the near future (temporal locality), and in practice it comes close to OPT.
LRU needs to know the order of use. Two hardware approaches:
- Counters: each page table entry gets a time-of-use field, written from a clock on every reference; the victim has the smallest value.
- Stack: keep the page numbers in a stack; a referenced page moves to the top, and the victim is at the bottom.
Because exact LRU is costly, many systems use approximations with a reference bit set by hardware on every use (for example, second chance or the clock algorithm: FIFO that skips, and clears, pages whose reference bit is 1).
Same string and 3 blocks, with LRU.
| Ref | 4 | 2 | 0 | 1 | 2 | 6 | 1 | 4 | 0 | 1 | 0 | 2 | 3 | 5 | 7 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Block 1 | 4 | 4 | 4 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3 | 3 | 3 |
| Block 2 | 2 | 2 | 2 | 2 | 2 | 2 | 4 | 4 | 4 | 4 | 2 | 2 | 2 | 7 | |
| Block 3 | 0 | 0 | 0 | 6 | 6 | 6 | 0 | 0 | 0 | 0 | 0 | 5 | 5 | ||
| Fault | F | F | F | F | F | F | F | F | F | F | F |
At reference 6, the pages in memory were last used at: 0 (third reference), 1 (fourth), 2 (fifth). Page 0 is the least recently used, so 6 replaces it. FIFO would have removed 2, which was just used.
5. All Three on One String
Reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1. Count the faults for each algorithm.
| Algorithm | Faults | Hits | Hit ratio |
|---|---|---|---|
| FIFO | 15 | 5 | 0.25 |
| LRU | 12 | 8 | 0.40 |
| OPT | 9 | 11 | 0.55 |
A few decisions explain the difference. At reference 3 (the sixth), memory holds 2, 0, 1:
- FIFO removes 0, the oldest, though 0 is used next.
- LRU removes 1, used least recently.
- OPT also removes 1, because 1 is not needed until the 14th reference.
Answer: FIFO 15, LRU 12, OPT 9
The simulator below runs all three algorithms on the same string, then checks Belady's anomaly (next section) and Mano's string from Examples 1 and 2:
// Page replacement with FIFO, LRU and OPT (optimal), run side by side on
// one reference string. Each column shows the frames after the reference
// after an F when the reference caused a page fault. -1 marks an empty frame.
module tb;
parameter MAXF = 4;
integer refs [0:19];
integer nrefs;
integer frame [0:2][0:MAXF-1]; // [algorithm][frame]
integer stamp [0:2][0:MAXF-1]; // load time (FIFO) or last use (LRU)
integer faults [0:2];
integer nframes;
integer a, t, f, p, hit, victim, best, dist, k, j;
task reset;
begin
for (a = 0; a < 3; a = a + 1) begin
faults[a] = 0;
for (f = 0; f < MAXF; f = f + 1) begin
frame[a][f] = -1;
stamp[a][f] = 0;
end
end
end
endtask
// reference page p at time t with algorithm a (0 FIFO, 1 LRU, 2 OPT)
task access(input integer alg);
begin
hit = -1;
for (f = 0; f < nframes; f = f + 1)
if (frame[alg][f] == p)
hit = f;
if (hit >= 0) begin
if (alg == 1)
stamp[alg][hit] = t; // LRU: record the use
end else begin
faults[alg] = faults[alg] + 1;
victim = -1;
for (f = 0; f < nframes; f = f + 1)
if (victim < 0 && frame[alg][f] == -1)
victim = f; // a free frame first
if (victim < 0 && alg < 2) begin // oldest stamp
victim = 0;
for (f = 1; f < nframes; f = f + 1)
if (stamp[alg][f] < stamp[alg][victim])
victim = f;
end
if (victim < 0) begin // OPT: used farthest ahead
best = -1;
for (f = 0; f < nframes; f = f + 1) begin
dist = 1000; // never used again
for (j = nrefs - 1; j > t; j = j - 1)
if (refs[j] == frame[alg][f])
dist = j;
if (dist > best) begin
best = dist;
victim = f;
end
end
end
frame[alg][victim] = p;
stamp[alg][victim] = t;
end
end
endtask
task show(input integer alg);
begin
if (hit < 0)
$write(" F"); // page fault
else
$write(" ");
for (f = 0; f < nframes; f = f + 1)
if (frame[alg][f] < 0)
$write(" -");
else
$write(" %0d", frame[alg][f]);
end
endtask
task run(input integer n, input integer frames, input integer verbose);
begin
nrefs = n;
nframes = frames;
reset;
if (verbose)
$display("ref FIFO LRU OPT");
for (t = 0; t < nrefs; t = t + 1) begin
p = refs[t];
if (verbose)
$write(" %0d ", p);
for (k = 0; k < 3; k = k + 1) begin
access(k);
if (verbose)
show(k);
end
if (verbose)
$display("");
end
end
endtask
initial begin
// 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 with 3 frames
refs[0] = 7; refs[1] = 0; refs[2] = 1; refs[3] = 2; refs[4] = 0;
refs[5] = 3; refs[6] = 0; refs[7] = 4; refs[8] = 2; refs[9] = 3;
refs[10] = 0; refs[11] = 3; refs[12] = 2; refs[13] = 1; refs[14] = 2;
refs[15] = 0; refs[16] = 1; refs[17] = 7; refs[18] = 0; refs[19] = 1;
run(20, 3, 1);
$display("faults: FIFO %0d, LRU %0d, OPT %0d", faults[0], faults[1], faults[2]);
// Belady's anomaly: 1 2 3 4 1 2 5 1 2 3 4 5 with FIFO
refs[0] = 1; refs[1] = 2; refs[2] = 3; refs[3] = 4; refs[4] = 1;
refs[5] = 2; refs[6] = 5; refs[7] = 1; refs[8] = 2; refs[9] = 3;
refs[10] = 4; refs[11] = 5;
run(12, 3, 0);
$write("Belady: FIFO with 3 frames %0d faults, ", faults[0]);
run(12, 4, 0);
$display("with 4 frames %0d faults", faults[0]);
// Mano: 4 2 0 1 2 6 1 4 0 1 0 2 3 5 7 with 3 frames
refs[0] = 4; refs[1] = 2; refs[2] = 0; refs[3] = 1; refs[4] = 2;
refs[5] = 6; refs[6] = 1; refs[7] = 4; refs[8] = 0; refs[9] = 1;
refs[10] = 0; refs[11] = 2; refs[12] = 3; refs[13] = 5; refs[14] = 7;
run(15, 3, 0);
$display("Mano's string: FIFO %0d, LRU %0d, OPT %0d faults", faults[0], faults[1], faults[2]);
$finish;
end
endmoduleEach column shows the three frames after the reference, with F marking a fault. The totals match the examples:
ref FIFO LRU OPT
7 F 7 - - F 7 - - F 7 - -
0 F 7 0 - F 7 0 - F 7 0 -
1 F 7 0 1 F 7 0 1 F 7 0 1
2 F 2 0 1 F 2 0 1 F 2 0 1
0 2 0 1 2 0 1 2 0 1
3 F 2 3 1 F 2 0 3 F 2 0 3
0 F 2 3 0 2 0 3 2 0 3
4 F 4 3 0 F 4 0 3 F 2 4 3
2 F 4 2 0 F 4 0 2 2 4 3
3 F 4 2 3 F 4 3 2 2 4 3
0 F 0 2 3 F 0 3 2 F 2 0 3
3 0 2 3 0 3 2 2 0 3
2 0 2 3 0 3 2 2 0 3
1 F 0 1 3 F 1 3 2 F 2 0 1
2 F 0 1 2 1 3 2 2 0 1
0 0 1 2 F 1 0 2 2 0 1
1 0 1 2 1 0 2 2 0 1
7 F 7 1 2 F 1 0 7 F 7 0 1
0 F 7 0 2 1 0 7 7 0 1
1 F 7 0 1 1 0 7 7 0 1
faults: FIFO 15, LRU 12, OPT 9
Belady: FIFO with 3 frames 9 faults, with 4 frames 10 faults
Mano's string: FIFO 12, LRU 11, OPT 10 faults6. Belady's Anomaly
You would expect more frames to mean fewer faults. For FIFO this is not always true: for some strings, adding a frame increases the faults. This is Belady's anomaly.
Reference string 1 2 3 4 1 2 5 1 2 3 4 5. Count FIFO faults with 3 and with 4 frames.
3 frames:
| Ref | 1 | 2 | 3 | 4 | 1 | 2 | 5 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Frame 1 | 1 | 1 | 1 | 4 | 4 | 4 | 5 | 5 | 5 | 5 | 5 | 5 |
| Frame 2 | 2 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 3 | 3 | 3 | |
| Frame 3 | 3 | 3 | 3 | 2 | 2 | 2 | 2 | 2 | 4 | 4 | ||
| Fault | F | F | F | F | F | F | F | F | F |
4 frames:
| Ref | 1 | 2 | 3 | 4 | 1 | 2 | 5 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Frame 1 | 1 | 1 | 1 | 1 | 1 | 1 | 5 | 5 | 5 | 5 | 4 | 4 |
| Frame 2 | 2 | 2 | 2 | 2 | 2 | 2 | 1 | 1 | 1 | 1 | 5 | |
| Frame 3 | 3 | 3 | 3 | 3 | 3 | 3 | 2 | 2 | 2 | 2 | ||
| Frame 4 | 4 | 4 | 4 | 4 | 4 | 4 | 3 | 3 | 3 | |||
| Fault | F | F | F | F | F | F | F | F | F | F |
Belady's anomaly: FIFO faults against number of frames
LRU and OPT never show the anomaly. They are stack algorithms: the set of pages in memory with n frames is always a subset of the set with n + 1 frames, so a hit with n frames is also a hit with n + 1 frames. FIFO's set does not have this property.
| FIFO | LRU | OPT | |
|---|---|---|---|
| Replaces | oldest page loaded | page unused longest (past) | page unused longest (future) |
| Information needed | load order | order of use | future references |
| Can be built | yes, easily | yes, with counters or a stack (costly) | no (yardstick only) |
| Faults | most | close to OPT | fewest possible |
| Belady's anomaly | possible | never | never |
7. Page Faults and Access Time
Memory access takes 100 ns. Servicing a fault takes 8 ms if the victim is clean and 20 ms if it is dirty (it must be written back first); 70% of victims are dirty. What fault rate p keeps the effective access time at 200 ns?
- Average service time = 0.3 × 8 + 0.7 × 20 = 16.4 ms = 16,400,000 ns.
- EAT = (1 - p) × 100 + p × 16{,}400{,}000 = 200.
- 100 + p × 16{,}399{,}900 = 200, so p = 100 / 16,399,900 ≈ 6.1 × 10-6.
If the program keeps faulting because it has too few frames for the pages it is actively using (its working set), the system spends more time moving pages than running: this is called thrashing.
Key points to remember
- A dirty victim must be written back to disk; a clean one is simply overwritten.
- FIFO: oldest loaded. LRU: least recently used. OPT: used farthest in the future.
- OPT gives the fewest faults but cannot be built; LRU comes close.
- Belady's anomaly: FIFO can fault more with more frames (1 2 3 4 1 2 5 1 2 3 4 5: 9 faults with 3, 10 with 4).
- LRU and OPT are stack algorithms: no anomaly.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain the FIFO and LRU page replacement algorithms. For 4 2 0 1 2 6 1 4 0 1 0 2 3 5 7 with 3 blocks, find the faults for each. (10 Marks)
- Write: a frame table for each algorithm and the fault count (FIFO 12, LRU 11).
- Q2. Find the page faults for FIFO, LRU and optimal replacement for 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 with 3 frames. (10 Marks)
- Q3. What is Belady's anomaly? Show it with an example. (5 Marks)
- Q4. Why is the optimal algorithm not used in practice? How is LRU implemented? (5 Marks)
- Q5. What is a page fault? What happens when one occurs, and what is the role of the dirty bit? (5 Marks)