Sub topic 11
Belady's Anomaly and Stack Algorithms
This chapter assumes you can trace FIFO and LRU page replacement on a reference string and count page faults. It also assumes you know what a frame is. Here we look at a surprising property: for some algorithms, adding frames can increase the number of page faults.
The anomaly
Intuition says more physical memory means fewer or equal page faults. Belady's anomaly is a case where this fails. The same reference string produces more faults with more frames.
The standard reference string for FIFO is:
1 2 3 4 1 2 5 1 2 3 4 5FIFO gives 9 faults with 3 frames and 10 faults with 4 frames. The next section shows why.
Tracing the FIFO case
Each cell lists the resident pages, oldest first. "F" marks a fault and "H" marks a hit.
| Reference | 3 frames | 4 frames |
|---|---|---|
| 1 | 1 F | 1 F |
| 2 | 1 2 F | 1 2 F |
| 3 | 1 2 3 F | 1 2 3 F |
| 4 | 2 3 4 F | 1 2 3 4 F |
| 1 | 3 4 1 F | 1 2 3 4 H |
| 2 | 4 1 2 F | 1 2 3 4 H |
| 5 | 1 2 5 F | 2 3 4 5 F |
| 1 | 1 2 5 H | 3 4 5 1 F |
| 2 | 1 2 5 H | 4 5 1 2 F |
| 3 | 2 5 3 F | 5 1 2 3 F |
| 4 | 5 3 4 F | 1 2 3 4 F |
| 5 | 5 3 4 H | 2 3 4 5 F |
| Total faults | 9 | 10 |
With 4 frames, pages 1 and 2 stay resident long enough to hit early. That makes them older in the FIFO queue when page 5 arrives, so they are evicted just before they are needed again. With 3 frames the queue lines up differently, and 1 and 2 are still resident at the right moment.
The cause is that FIFO ranks pages by load time, not by use. The order in which pages leave memory depends on the frame count in a way that is not monotonic.
You can confirm both counts with the code below.
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) # evict the oldest loaded page
memory.append(p)
return faults
refs = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
print(fifo_faults(refs, 3), fifo_faults(refs, 4)) # 9 10Stack algorithms
A page replacement algorithm is a stack algorithm if, for every reference string and at every moment, this holds:
> The set of pages in memory with n frames is a subset of the set of pages in memory with n + 1 frames.
This is the inclusion property. If it holds, a reference that hits with n frames must also hit with n + 1 frames, because its page is in the smaller set and so in the larger one. Fault counts therefore can never increase as frames are added. A stack algorithm cannot show Belady's anomaly.
The name comes from the idea that the algorithm keeps one ordered list of all pages (a stack). Its priority order does not depend on the frame count. The resident set for n frames is the top n entries of that list.
Why LRU is a stack algorithm
LRU orders pages by time of last use. That order depends only on the reference string, not on memory size. With n frames, memory holds the n most recently used distinct pages. With n + 1 frames, it holds the n + 1 most recently used. The first set is always contained in the second.
Why Optimal is a stack algorithm
Optimal orders pages by the time of their next use, which also depends only on the reference string. Memory holds the n pages that will be needed soonest, so inclusion holds again.
Why FIFO is not
FIFO's order depends on when each page was loaded. Load times depend on which references faulted, and that depends on the frame count. The two orderings are not nested.
Checking the inclusion property in code
The program below records the resident set after every reference and tests whether the smaller memory is always a subset of the larger one.
def resident_sets(refs, frames, policy):
memory, history = [], [] # index 0 = next victim
for p in refs:
if p in memory:
if policy == "lru":
memory.remove(p) # refresh recency on a hit
memory.append(p)
else:
if len(memory) == frames:
memory.pop(0)
memory.append(p)
history.append(set(memory))
return history
def inclusion_holds(refs, policy, max_frames):
for n in range(1, max_frames):
small = resident_sets(refs, n, policy)
big = resident_sets(refs, n + 1, policy)
if not all(a <= b for a, b in zip(small, big)):
return False
return True
refs = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
for policy in ("fifo", "lru"):
print(policy, inclusion_holds(refs, policy, 6))
s3 = resident_sets(refs, 3, "fifo")[6]
s4 = resident_sets(refs, 4, "fifo")[6]
print(sorted(s3), sorted(s4))This prints:
fifo False
lru True
[1, 2, 5] [2, 3, 4, 5]The last line shows the FIFO failure. After the 7th reference (page 5), the 3-frame memory holds page 1 but the 4-frame memory does not. Page 1 is requested next, so the larger memory faults where the smaller one hits.
A passing test on one string does not prove an algorithm is a stack algorithm. A failing test does prove it is not. Stack-ness is a property of the policy, shown by an argument over all strings.
Which algorithms are immune
| Algorithm | Stack algorithm? | Can show the anomaly? |
|---|---|---|
| Optimal | Yes | No |
| LRU | Yes | No |
| FIFO | No | Yes |
| Second chance (clock) | No | Yes, since it falls back to FIFO order |
| Random | No | Can, for particular outcomes |
Immunity does not mean being optimal. LRU on our string gives 10 faults with 3 frames, which is worse than FIFO's 9. Stack algorithms guarantee only that adding frames never hurts. They do not guarantee they beat other policies on a given input.
Common errors
- Claiming the anomaly appears on every string. It needs specific strings. Most strings give fewer faults with more frames, even under FIFO.
- Claiming it needs a large increase in frames. The classic case goes from 3 to 4 frames.
- Saying "LRU is better than FIFO" from one trace. On this string with 3 frames, FIFO wins, 9 faults to 10.
- Counting hits wrongly after a refresh. A hit does not change FIFO order. It does change LRU order.
- Mixing up the two sets. Inclusion says the n-frame set is a subset of the n + 1 set, never the reverse.
GATE-style approach
Questions in this area usually take one of these forms:
- Give a reference string and ask for FIFO faults at two frame counts. Trace carefully and count each. Check whether the larger memory has more faults.
- Ask which of several algorithms can suffer from the anomaly. Pick FIFO and its FIFO-like variants. Exclude LRU and Optimal.
- Ask for a property that must hold for a stack algorithm. The answer is the inclusion property, which implies a non-increasing fault count.
For a trace, write the memory contents after every reference, as in the table above. Mistakes usually come from forgetting that a FIFO hit leaves the queue unchanged.
Exercise
A system uses FIFO replacement. The reference string is 1 2 3 4 1 2 5 1 2 3 4 5. Moving from 3 frames to 4 frames changes the number of page faults from what to what? Name the effect.
Show answer
It changes from 9 faults to 10 faults. This is Belady's anomaly, where more frames give more faults.
Exercise
Define a stack algorithm. Why does the definition guarantee that fault counts never increase as frames are added?
Show answer
A stack algorithm has the inclusion property. At every point in any reference string, the set of pages resident with n frames is a subset of the set resident with n + 1 frames. A reference that hits with n frames has its page in the smaller set, so it is in the larger set too and also hits. Every hit with n frames is therefore a hit with n + 1 frames, so faults with n + 1 frames are at most the faults with n frames.
Exercise
Using the reference string 1 2 3 4 1 2 5 1 2 3 4 5 and 4 frames, how many faults does LRU produce? Is this fewer than FIFO with 4 frames?
Show answer
LRU produces 8 faults. FIFO with 4 frames produces 10, so LRU is fewer. Since LRU is a stack algorithm, its 4-frame count (8) is also no greater than its 3-frame count (10).
Exercise
Explain why FIFO is not a stack algorithm, using the 3-frame and 4-frame memories after the 7th reference of the standard string.
Show answer
After the 7th reference (page 5), FIFO with 3 frames holds {1, 2, 5}. FIFO with 4 frames holds {2, 3, 4, 5}. Page 1 is in the smaller set but not the larger one, so inclusion fails. FIFO orders pages by load time, which depends on the frame count, so the two orderings are not nested.