Sub topic 11
Belady's Anomaly and Stack Algorithms
Prerequisites
Assume you know paging, page frames, page faults, and how to trace a page-replacement algorithm on a reference string. A reference string is the sequence of page numbers requested by a process.
What Is Belady's Anomaly?
Belady's anomaly is the counterintuitive case in which giving a process more page frames causes more page faults. A page fault occurs when the requested page is not currently in memory.
The anomaly can occur with FIFO, or first-in-first-out, replacement. FIFO evicts the page that has been in memory the longest. It does not consider how recently or how frequently a page was used.
Consider this reference string:
1 2 3 4 1 2 5 1 2 3 4 5With three frames, FIFO produces 9 faults. With four frames, it produces 10 faults. The extra frame changes the order in which pages are evicted, so the larger memory can have a worse result.
Stack Algorithms
A stack algorithm has a special property: for every point in the reference string, the pages held with k frames are a subset of the pages held with k + 1 frames.
Therefore, adding a frame cannot increase the number of page faults. The contents differ in order or metadata, but the smaller-frame memory never contains a page that is absent from the larger-frame memory.
LRU, or least recently used, is a stack algorithm. At every reference, it ranks pages by their most recent use. The k most recently used pages form the contents of a k-frame memory.
Optimal replacement is also a stack algorithm. It evicts the page whose next use is farthest in the future. This algorithm cannot be implemented exactly in a real operating system because the future reference string is unknown, but it provides a lower bound for comparing practical algorithms.
| Algorithm | Stack algorithm? | Belady's anomaly possible? |
|---|---|---|
| FIFO | No | Yes |
| LRU | Yes | No |
| Optimal | Yes | No |
| Clock approximation | Not generally | Possible |
The following program compares FIFO and LRU on the same reference string:
def fifo_faults(refs, frames):
memory, faults = [], 0
for page in refs:
if page not in memory:
faults += 1
if len(memory) == frames:
memory.pop(0)
memory.append(page)
return faults
def lru_faults(refs, frames):
memory, 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, 4, 1, 2, 5, 1, 2, 3, 4, 5]
print(fifo_faults(refs, 3), fifo_faults(refs, 4))
print(lru_faults(refs, 3), lru_faults(refs, 4))The output is:
9 10
10 8FIFO shows the anomaly. LRU has fewer faults when it receives four frames.
Solving GATE Questions
For a page-replacement question:
- Write the reference string in order.
- Draw one row for each available frame.
- Mark a fault whenever the requested page is absent.
- Apply the algorithm exactly at each fault.
- Repeat with the other frame count.
- Check whether the larger memory has more faults.
If the algorithm is LRU or Optimal, Belady's anomaly cannot occur. If it is FIFO, do not assume that more frames reduce faults.
Exercise
Why can FIFO have more page faults with four frames than with three, while LRU cannot?
Show answer
FIFO is not a stack algorithm, so changing the frame count can change its eviction order and remove pages that would otherwise be retained. LRU is a stack algorithm, so the pages in three frames are always contained in the pages in four frames.