FIFO Page Replacement and Belady's Anomaly
Evict the page that has been in memory longest. It is the simplest policy there is, and the one that can get worse when you give it more memory.
How FIFO Chooses a Victim
The FIFO page replacement algorithm, short for first in first out page replacement, evicts the page that has been in memory the longest. Resident pages form a queue: a newly loaded page joins the tail, and when a frame is needed the page at the head, the earliest arrival, is removed.
It is the cheapest policy to build. A circular array of frames and one pointer to the oldest slot are enough; a fault replaces that slot and moves the pointer on. Nothing happens on a hit, so the cost is O(1) per fault and zero per memory access.
The weakness is in what it measures. Arrival time says little about future use: a page loaded at startup and used on every loop iteration since is, to FIFO, the oldest page and the first to go. Using a page never moves it, so a heavily used page and an idle one leave in loading order.
- Rule: evict the oldest arrival
- Hits change nothing in the queue
- O(1) per fault, no hardware support needed
Worked Example: 10 Faults in 3 Frames
The string is the same one the LRU and OPT lessons use, so the counts line up. Read the queue column with the head on the left: that page is the next to go.
The decisive moment is t = 5 to t = 7. Page 0 hits at t = 5 but stays at the head, so at t = 6 FIFO evicts it, one step after it was used. At t = 7 the program asks for 0 again and faults. LRU keeps 0 at t = 6 and gets a hit at t = 7, which is its whole one-fault lead.
From t = 8 to t = 10 the pattern repeats: each evicted page (2, 3, 0) is needed within two references. Total: 10 faults and 3 hits, against 9 for LRU and 7 for OPT. With four frames FIFO takes 7 on this string.
| t | Ref | Queue after (head → tail) | Result |
|---|---|---|---|
| 1 | 7 | 7 | fault |
| 2 | 0 | 7 0 | fault |
| 3 | 1 | 7 0 1 | fault |
| 4 | 2 | 0 1 2 | evict 7 |
| 5 | 0 | 0 1 2 | hit, queue unchanged |
| 6 | 3 | 1 2 3 | evict 0, used at t = 5 |
| 7 | 0 | 2 3 0 | evict 1; 0 faults straight back |
| 8 | 4 | 3 0 4 | evict 2 |
| 9 | 2 | 0 4 2 | evict 3 |
| 10 | 3 | 4 2 3 | evict 0 |
| 11 | 0 | 2 3 0 | evict 4 |
| 12 | 3 | 2 3 0 | hit |
| 13 | 2 | 2 3 0 | hit |
- Count check: 3 compulsory + 7 replacements = 10
- Ignoring the hit at t = 5 causes the fault at t = 7
- Only 2 of the 7 victims (7 and 4) were never needed again
Belady's Anomaly: More Frames, More Faults
You would expect more memory never to hurt. Belady's anomaly is the case where it does: under FIFO, adding a frame can increase the number of page faults. Laszlo Belady and colleagues reported it in 1969.
The classic string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. FIFO takes 9 faults with three frames and 10 with four. After t = 7 the three-frame memory holds 1, 2, 5, while the four-frame memory holds 2, 3, 4, 5 and has already pushed out page 1. References 1 and 2 at t = 8 and 9 are hits in the small memory and faults in the big one.
The cause is that FIFO's resident sets do not nest: what three frames hold is not always a subset of what four frames hold. Policies whose sets always nest are called stack algorithms, and they can never show the anomaly. LRU and OPT are stack algorithms, as the table shows. Clock is not: when every reference bit is set it behaves exactly like FIFO, and on this string it also goes from 9 to 10.
- FIFO: 3 frames give 9 faults, 4 frames give 10
- Cause: the resident sets do not nest as frames grow
- LRU and OPT are stack algorithms and immune
Terms, operations, and practical uses
The rule
- DecisionEvict the page at the head of the queue, the oldest arrival.
- HitsIgnored: using a page never moves it.
- CostO(1) per fault; one pointer into a circular array.
- StructureA queue of frames; new pages join the tail.
Belady's anomaly
- The string1,2,3,4,1,2,5,1,2,3,4,5.
- 3 frames9 faults.
- 4 frames10 faults: more memory, worse result.
- CauseResident sets do not nest as frames are added.
Numbers
- 10 faultsOn 7,0,1,2,0,3,0,4,2,3,0,3,2 with 3 frames; 7 with 4.
- OthersLRU 9, OPT 7 on the same string.
- Lost faultEvicting page 0 at t = 6, one step after it was used.
- DescendantsSecond chance and clock add a reference bit.
FIFO on 7,0,1,2,0,3,0,4,2,3,0,3,2 with 3 frames
from collections import deque
def fifo(refs, frames):
"""FIFO page replacement. Returns (faults, hits).
The queue records arrival order only. A hit deliberately does not reorder
it -- that omission is the algorithm, and the reason it can be beaten.
"""
queue, resident, faults, hits = deque(), set(), 0, 0
for page in refs:
if page in resident:
hits += 1
continue # a hit changes nothing about eviction order
faults += 1
if len(queue) == frames:
evicted = queue.popleft()
resident.remove(evicted)
queue.append(page)
resident.add(page)
return faults, hits
refs = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
faults, hits = fifo(refs, 3)
print(f"{faults} faults, {hits} hits") # 10 faults, 3 hits
# Belady's anomaly: more frames, more faults.
belady = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
print(f"3 frames: {fifo(belady, 3)[0]} faults, 4 frames: {fifo(belady, 4)[0]} faults")#include <deque>
#include <iostream>
#include <unordered_set>
#include <vector>
// FIFO page replacement: evict the oldest arrival. A hit does not reorder
// the queue, which is exactly why FIFO evicts pages that are still hot.
int fifo(const std::vector<int>& refs, int frames, int& hits) {
std::deque<int> queue;
std::unordered_set<int> resident;
int faults = 0;
hits = 0;
for (int page : refs) {
if (resident.count(page)) {
++hits;
continue;
}
++faults;
if (static_cast<int>(queue.size()) == frames) {
resident.erase(queue.front());
queue.pop_front();
}
queue.push_back(page);
resident.insert(page);
}
return faults;
}
int main() {
std::vector<int> refs = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int hits = 0;
int faults = fifo(refs, 3, hits);
std::cout << faults << " faults, " << hits << " hits\n"; // 10 faults, 3 hits
// Belady's anomaly.
std::vector<int> belady = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
std::cout << "3 frames: " << fifo(belady, 3, hits) << " faults, 4 frames: " << fifo(belady, 4, hits) << " faults\n";
return 0;
}import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Set;
public class Main {
/** Evicts the oldest arrival. Hits never reorder the queue. */
static int fifo(int[] refs, int frames) {
Deque<Integer> queue = new ArrayDeque<>();
Set<Integer> resident = new HashSet<>();
int faults = 0;
for (int page : refs) {
if (resident.contains(page)) {
continue;
}
faults++;
if (queue.size() == frames) {
resident.remove(queue.removeFirst());
}
queue.addLast(page);
resident.add(page);
}
return faults;
}
public static void main(String[] args) {
int[] refs = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int faults = fifo(refs, 3);
System.out.println(faults + " faults, " + (refs.length - faults) + " hits");
// Belady's anomaly: 3 frames beat 4.
int[] belady = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
System.out.println("3 frames: " + fifo(belady, 3) + " faults, 4 frames: " + fifo(belady, 4) + " faults");
}
}Step through it
Running on refs 7,0,1,2,0,3,0,4,2,3,0,3,2 · 3 frames
Where FIFO Still Fits
FIFO is fine when there is no reuse to protect. In a one-pass sequential scan each block is used once, so recency predicts nothing and LRU's bookkeeping buys nothing over FIFO.
Its bigger role is as a base. Second chance keeps the FIFO queue but checks a reference bit at the head: a recently used page is sent to the tail instead of evicted. Clock is the same idea on a circular buffer, and it is what real kernels approximate.
It is also the usual first algorithm in an operating systems course: easy to trace by hand, and its mistake at t = 6 above shows why every later policy looks at use rather than arrival.
- Reasonable when pages are used once and never again
- Second chance and clock are FIFO plus a reference bit
- The baseline every other policy is compared with