Operating System - Optimal Page Replacement Algorithm



In virtual memory systems, page replacement algorithms are used to decide which pages to remove from memory during a page fault. An Optimal Page Replacement Algorithm is considered as the best page replacement algorithm. In this chapter, we will explain how this algorithm works with the help of an example. Following topics will be covered in this chapter −

What is Optimal Page Replacement Algorithm?

An Optimal Page Replacement Algorithm is a page replacement algorithm that replaces the page that will not be used for the longest period of time in the future. This involve predicting the future page references, which is not always possible. So this algorithm is just a theoretical concept and cannot be implemented in real systems. This is often used to evaluate the performance of other page replacement algorithms.

The idea behind this algorithm is to minimize the number of page faults as less as possible. This is ensured by replacing the page that will not be used for the longest period of time in the future. This will help to keep the pages that are likely to be used in memory, which will reduce the number of page faults.

Example of Optimal Page Replacement Algorithm

Let's solve an example that involves the optimal page replacement algorithm to understand how it works.

Consider the following page reference string and frames −

 Page Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 Frames: 4 

Here, the first four pages 7, 0, 1, 2 are loaded directly into the first four empty frames. When the next page 0 is referenced, it is already in memory, so no page fault occurs. Next, page 3 is referenced, which causes a page fault. So the optimal algorithm will remove page 1 since it will not be used for the longest period of time in the future (it is not used again in the reference string). The same process continues for the rest of the page references.

The table below shows all the steps of the process −

Page Reference
7 0 1 2 0 3 0 4 2 3 0 3 2 3
Frames 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 4 4 4 4 4 4 4
0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 7 7 7 7 3 3 3 3 3 3 3 3 3
Page Fault Yes Yes Yes Yes No Yes No Yes No No No No No No
  • Total Page References: 14
  • Total Page Faults: 6
  • Total Page Hits: 8
  • Page Fault Rate: 6/14 = 0.4286 (approximately 42.86%)
  • Page Hit Rate: 8/14 = 0.5714 (approximately 57.14%)

These results clearly show that the page fault rate significantly reduced by using the optimal page replacement algorithm. After a certain number of page references, the page fault will not occur frequently. This happens because the pages that are likely to be used in the future are available in memory.

We are able to implement this algorithm as we already know the entire page reference string in advance. But that will not be the case in real-world scenarios. You never know what pages will be referenced in the future. That's why we told earlier that this algorithm is just a theoretical model.

Pros and Cons of Optimal Page Replacement Algorithm

The advantages of the optimal page replacement algorithm are −

  • This algorithm have lowest page fault rate compared to other page replacement algorithms.
  • This algorithm is theoretically optimal because it uses future knowledge to make decisions.
  • This algorithm is easy to understand and implement.

The optimal page replacement algorithm also comes with some disadvantages

  • This algorithm is not practical in real world systems. Because it require the knowledge of future page references, i.e., OS should know the entire page reference string in advance.
  • This algorithm may not be suitable for systems with large number of processes and large address spaces. Because the page table can become very large and consume a lot of memory.

Conclusion

An optimal page replacement algorithm is a theoretical concept used to evaluate the performance of other page replacement algorithms. It is considered as the best page replacement algorithm. But to implement this algorithm, you need to have the knowledge of future page references. Hence, it is not used in real-world systems. It is important to understand this algorithm is used just to improve the performance other page replacement algorithms.

Advertisements