Sub topic 10
Page Replacement Algorithms
Assume you know virtual memory, pages, frames, page faults, and basic Python. This chapter focuses on what happens when a process references a page that is not currently in its assigned frames.
Why Page Replacement Is Needed
A page fault occurs when a process references a page that is not in physical memory. If a free frame exists, the operating system loads the page into that frame. If all frames are occupied, it must choose a resident page to remove. This choice is called page replacement.
The operating system tries to minimize page faults because servicing a fault requires storage access, which is much slower than accessing memory.
A reference string records the pages accessed by a process. For example:
1, 2, 3, 1, 4, 2To trace an algorithm, process the references from left to right. A reference already in a frame is a hit. A reference not in a frame is a fault.
FIFO Page Replacement
First-In, First-Out (FIFO) removes the page that has been in memory the longest. It maintains a queue:
- Insert each newly loaded page at the back.
- On a fault with full frames, remove the page at the front.
- Load the requested page at the back.
FIFO is simple, but it does not consider how frequently or recently a page is used. An old page might still be heavily used.
Optimal Page Replacement
The Optimal algorithm removes the page whose next use is farthest in the future. If a page will never be used again, remove it immediately.
Optimal produces the minimum possible number of page faults for a known reference string. However, an operating system cannot generally implement it because it cannot know the future. It is mainly used as a theoretical lower bound for comparing other algorithms.
LRU Page Replacement
Least Recently Used (LRU) removes the page that has not been referenced for the longest time. It uses past behavior as an approximation of future behavior.
For example, with three empty frames and the reference string 1, 2, 3, 1, 4:
| Reference | Frames after reference | Result |
|---|---|---|
| 1 | 1 | Fault |
| 2 | 1, 2 | Fault |
| 3 | 1, 2, 3 | Fault |
| 1 | 2, 3, 1 | Hit |
| 4 | 3, 1, 4 | Fault; remove 2 |
When page 4 is requested, page 2 is the least recently used page.
def lru_faults(refs, frames):
memory = [] # least recently used page is at index 0
faults = 0
for page in refs:
if page in memory:
memory.remove(page)
else:
faults += 1
if len(memory) == frames:
memory.pop(0)
memory.append(page)
return faults
refs = [1, 2, 3, 1, 4, 2]
print(lru_faults(refs, 3))Real systems approximate LRU with reference bits, clock algorithms, or aging counters because exact LRU bookkeeping is expensive.
Comparing the Algorithms
| Algorithm | Replacement rule | Practical role |
|---|---|---|
| FIFO | Remove the oldest loaded page | Simple baseline |
| Optimal | Remove the page used farthest in the future | Theoretical comparison |
| LRU | Remove the least recently used page | Strong practical strategy |
FIFO can exhibit Belady’s anomaly, where adding more frames causes more faults. LRU and Optimal are stack algorithms, so increasing the number of frames cannot increase their fault count for the same reference string.
Solving Replacement Questions
Use this procedure:
- Draw one column for each frame.
- Process the reference string one item at a time.
- Mark a hit when the page is already present.
- On a fault, use the stated replacement rule.
- Count faults separately from hits.
- Check that the number of resident pages never exceeds the number of frames.
Exercise
With three frames and the reference string 1, 2, 3, 1, 4, which page does LRU remove when page 4 is requested?
Show answer
LRU removes page 2. Page 1 was most recently referenced, page 3 was referenced after page 2, and page 2 is the least recently used.