Operating System - Belady's Anomaly



In virtual memory systems, the page replacement algorithms are used to decide which pages to remove from memory during a page fault. The Belady's Anomaly is a major drawback of some of the page replacement algorithms like FIFO. In this chapter, we will explain this anomaly with the help of an example. Following topics will be covered in this chapter −

What is Belady's Anomaly?

Belady's Anomaly states that in some cases, when you increase the number of page frames, the number of page faults may also increase. Normally, we will expect that on increasing the number of page frames, the number of page faults should decrease as the system get more memory to work on. But that is not the case with some page replacement algorithms. Following algorithms will show Belady's Anomaly −

  • FIFO (First In First Out)
  • Random Page Replacement
  • Second Chance Page Replacement

Example of Belady's Anomaly

Let's see an example of Belady's Anomaly in FIFO page replacement algorithm. Consider the following page reference string −

Page reference string = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5}

Case 1: Number of Page Frames = 3

Let's see how many page faults will occur when the number of page frames is 3. Following table shows the memory state changes for the given page reference string −

Reference Memory State Page Fault
1 1 Yes
2 1, 2 Yes
3 1, 2, 3 Yes
4 2, 3, 4 Yes
1 3, 4, 1 Yes
2 4, 1, 2 Yes
5 1, 2, 5 Yes
1 1, 2, 5 No
2 1, 2, 5 No
3 2, 5, 3 Yes
4 5, 3, 4 Yes
5 5, 3, 4 No

In this case, the total number of page faults is 9.

Case 2: Number of Page Frames = 4

Now, let's see how many page faults will occur when the number of page frames is 4. Following table shows the steps of the process −

Reference Memory State Page Fault
1 1 Yes
2 1, 2 Yes
3 1, 2, 3 Yes
4 1, 2, 3, 4 Yes
1 1, 2, 3, 4 No
2 1, 2, 3, 4 No
5 2, 3, 4, 5 Yes
1 3, 4, 5, 1 Yes
2 4, 5, 1, 2 Yes
3 5, 1, 2, 3 Yes
4 1, 2, 3, 4 Yes
5 2, 3, 4, 5 Yes

In this case, the total number of page faults is 10.

When number of page frames was increased from 3 to 4, the number of page faults also increased from 9 to 10. This is an example of Belady's Anomaly.

Why Belady's Anomaly Occurs?

Belady's Anomaly occurs mainly in non-stack algorithms such as FIFO. Meaning, this occur in algorithm that are not maintaining the order of pages based on their usage. For example, In FIFO the oldest page is replaced even though it is frequently. This can lead to situations where increasing the number of page frames causes more page faults, as seen in the example above.

The non-stack algorithms do not guarantee that the set of pages in memory with n frames is always a subset of the set of pages in memory with n+1 frames. Meaning, when you increase the number of frames, the pages that were in memory with fewer frames may not be in memory with more frames. This can lead to more page faults and Belady's Anomaly.

Stack Algorithms

Stack algorithms refers to the page replacement algorithms that maintain the order of pages based on their usage. Examples of stack algorithms are −

  • LRU (Least Recently Used)
  • Optimal Page Replacement

These algorithms do not suffer from Belady's Anomaly. Because they satisfy the following condition −

$$\mathrm{P_{n} \:\subseteq\: P_{n+1}}$$

Where,

  • P_n is the set of pages in memory with n frames.
  • P_{n+1} is the set of pages in memory with n+1 frames.

Conclusion

Belady's Anomaly occurs in some non-stack-based page replacement, where increasing the number of page frames causes an increase in the number of page faults. This is mainly observed in FIFO, Random, and Second Chance page replacement algorithms. Also, it is not necessary that every string reference pattern cause Belady's anomaly in FIFO but there is certain kind of string references that worsen the FIFO performance on increasing the number of frames. Stack based algorithms like LRU and Optimal prevents Belady's Anomaly by maintaining the order of pages based on their usage.

Advertisements