Sub topic 10
Page Replacement Algorithms
This chapter assumes you know demand paging, page faults and the page-fault service sequence from the previous chapters. You should also be comfortable reading short Python.
Why replacement is needed
Under demand paging, a page is loaded only when a process touches it. When a page fault occurs and no frame is free, the OS must pick a victim page, write it back to disk if it was modified, and reuse its frame. A page replacement algorithm is the rule for choosing the victim.
The goal is the fewest faults on a reference string, which is the sequence of page numbers a process touches. A fault costs milliseconds of disk time, while a hit costs nanoseconds. Even a small drop in fault rate therefore changes effective access time a great deal.
Three conventions apply to every trace in this chapter:
- Frames start empty, so the first reference to each distinct page is always a fault.
- Fault count is the answer GATE asks for. Fault rate is faults divided by total references.
- Consecutive references to the same page are hits after the first one.
FIFO
First-In-First-Out evicts the page that has been in memory longest. It needs only a queue of pages in load order. A hit does not change the queue. This is the most common source of tracing mistakes.
FIFO is simple but ignores usage. A heavily used page that was loaded early gets evicted anyway.
Optimal (OPT)
Optimal evicts the page whose next use is farthest in the future, or that is never used again. It gives the minimum possible faults for a given string and frame count. It cannot be implemented in a real OS because it needs knowledge of the future. Its value is as a benchmark and as a GATE staple.
When two resident pages are never used again, either can be evicted. The fault count is the same.
LRU
Least Recently Used evicts the page unused for the longest time. It approximates OPT by looking backward instead of forward. The bet is that the past predicts the future (locality of reference).
A hit moves the page to the most-recent position. Exact LRU needs hardware support on every memory reference. Two implementations exist:
- Counters: each page-table entry holds a time-of-use field, and eviction scans for the smallest value.
- Stack: a doubly linked list moves the referenced page to the top, so the bottom is always the victim.
Both are too costly to update on every reference, which is why real systems use the approximations below.
A worked trace
Use 3 frames and this reference string:
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
The FIFO queue after each fault is shown below, oldest page first. Faults are marked F and hits H.
| Ref | 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 |
|---|---|---|---|---|---|---|---|---|---|---|
| Result | F | F | F | F | H | F | F | F | F | F |
| Frames | 7 | 7 0 | 7 0 1 | 0 1 2 | 0 1 2 | 1 2 3 | 2 3 0 | 3 0 4 | 0 4 2 | 4 2 3 |
| Ref | 0 | 3 | 2 | 1 | 2 | 0 | 1 | 7 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|
| Result | F | H | H | F | F | H | H | F | F | F |
| Frames | 2 3 0 | 2 3 0 | 2 3 0 | 3 0 1 | 0 1 2 | 0 1 2 | 0 1 2 | 1 2 7 | 2 7 0 | 7 0 1 |
FIFO gives 15 faults. The script below computes all three policies. It prints 15, 12 and 9 for FIFO, LRU and OPT.
def fifo_faults(refs, frames):
memory, faults = [], 0
for p in refs:
if p not in memory:
faults += 1
if len(memory) == frames:
memory.pop(0)
memory.append(p)
return faults
def lru_faults(refs, frames):
memory, faults = [], 0 # least recently used at index 0
for p in refs:
if p in memory:
memory.remove(p)
else:
faults += 1
if len(memory) == frames:
memory.pop(0)
memory.append(p)
return faults
def opt_faults(refs, frames):
memory, faults = [], 0
for i, p in enumerate(refs):
if p in memory:
continue
faults += 1
if len(memory) == frames:
future = refs[i + 1:]
def next_use(q):
return future.index(q) if q in future else len(future) + 1
memory.remove(max(memory, key=next_use))
memory.append(p)
return faults
refs = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1]
print(fifo_faults(refs, 3), lru_faults(refs, 3), opt_faults(refs, 3)) # 15 12 9Comparing the three
| Algorithm | Victim chosen by | Implementable? | Typical result |
|---|---|---|---|
| FIFO | Oldest load time | Yes, a queue | Most faults, can behave badly |
| LRU | Oldest last use | Yes, but costly in hardware | Close to OPT |
| OPT | Farthest next use | No, needs the future | Minimum faults |
On any string, OPT faults are at most LRU faults, and for the strings in this chapter LRU is no worse than FIFO. That second ordering is not guaranteed in general. FIFO can occasionally beat LRU on a specific string. The chapter on Belady's anomaly covers why adding frames can hurt FIFO but never LRU or OPT.
LRU approximations
True LRU is too expensive, so hardware provides a reference bit per page. The hardware sets it to 1 on any access, and the OS clears it periodically.
Additional-reference-bits (aging)
Each page keeps an 8-bit history byte. At every timer tick the OS shifts the byte right and puts the current reference bit in the top position. The page with the smallest byte value is the approximate LRU victim. A page used in the latest interval has a high bit set and outranks any page that was not.
Second chance (clock)
Second chance is FIFO plus the reference bit. Pages sit in a circular list with a hand pointing at the oldest. To pick a victim, the OS inspects the page under the hand:
- If the reference bit is 0, evict it.
- If the bit is 1, clear it, give the page a second chance, and advance the hand.
If every page has its bit set, the hand sweeps the whole circle clearing bits and then evicts the page it started on. In that worst case the algorithm behaves exactly like FIFO.
Enhanced second chance
This version uses the pair (reference, modify) and ranks pages into four classes:
- (0,0): not recently used, not modified. This is the best victim, because it needs no write-back.
- (0,1): not recently used, but modified, so it must be written out first.
- (1,0): recently used, clean.
- (1,1): recently used and modified. This is the worst victim.
The OS scans for the lowest non-empty class, so it prefers clean pages. This saves a disk write per eviction.
Counting-based and buffering schemes
- LFU (least frequently used) evicts the page with the smallest reference count. A page used heavily at startup and never again stays resident for a long time.
- MFU (most frequently used) assumes a page with a low count was just loaded and still needs to be used. Neither scheme is common because both approximate OPT poorly.
- Page buffering keeps a pool of free frames. On a fault, the needed page is read into a free frame immediately, and the victim is written out later. Pages in the pool can be reclaimed without disk I/O if they are referenced again.
Common errors
- Updating FIFO on a hit. FIFO order depends only on load time.
- Forgetting to update LRU on a hit. A hit changes recency, so the page must move to the most-recent position.
- Counting only replacements. Compulsory first-reference faults count too, unless the question says frames are preloaded.
- Assuming more frames always means fewer faults. This holds for LRU and OPT, but not for FIFO.
- Applying OPT with ties carelessly. Pages never used again are all equally good victims, but a page used later is not.
Exercises
Exercise
Using 3 frames and the reference string 1 2 3 4 1 2 5 1 2 3 4 5, how many page faults does Optimal replacement produce?
Show answer
7. The first three references fault (1, 2, 3). Reference 4 evicts 3, since 3's next use is farthest. Reference 5 evicts 4, whose next use is farthest. References 1, 2 hit. Reference 3 faults, evicting a page that is never used again (1 or 2). Reference 4 faults, evicting a page that is not 5. Reference 5 hits. Faults: 1, 2, 3, 4, 5, 3, 4, which is 7.
Exercise
In the clock algorithm, all pages in memory have reference bit 1 when a fault occurs. Which page is evicted, and what is the state of the reference bits afterward?
Show answer
The hand sweeps a full circle clearing every bit to 0, then returns to the page it started on and evicts it, since that page now has bit 0. This is the FIFO choice. Afterward, all remaining resident pages have reference bit 0.
Exercise
A clean page and a modified page both have reference bit 0. Why does enhanced second chance prefer the clean one?
Show answer
Evicting a clean page needs no write to disk, because the disk copy is already current. Evicting a modified page first requires writing it back, which adds a disk write to the fault service time.