VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implementation-of-optimal-page-replacement-algorithm-in-os/

⇱ Implementation of Optimal Page Replacement Algorithm in OS - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementation of Optimal Page Replacement Algorithm in OS

Last Updated : 23 Jul, 2025

The Optimal Page Replacement Algorithm is a technique used in operating systems to manage memory efficiently by replacing pages in a way that minimizes page faults. When a new page needs to be loaded into memory and all frames are already occupied, this algorithm looks into the future to decide which page will not be used for the longest time and replaces it. This ensures that the least useful page is removed, reducing the chances of page faults later. While it’s ideal in theory, this algorithm is not practical in real-life scenarios because it requires prior knowledge of future page references.

Input: Number of frames, fn = 3
Reference String, pg[] = {7, 0, 1, 2,0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
Output: No. of hits = 11
No. of misses = 9

Input: Number of frames, fn = 4
Reference String, pg[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
Output: No. of hits = 7
No. of misses = 6

Output

No. of hits = 7
No. of misses = 6

Time Complexity: O(n × f), where n is the number of pages, and f is the number of frames.
Space Complexity: O(f)

  • The above implementation can be optimized using hashing. We can use an unordered set in place of vector so that search operation can be done in O(1) time.
  • Note that optimal page replacement algorithm is not practical as we cannot predict future. However, it is used as a reference for other page replacement algorithms.

Another approach for above code is as follow: 

1.Create an empty vector to represent the frames.

2.For each page in the page reference sequence:
a. If the page is found in the current frame, it is considered a hit.
b. If the page is not found in the current frame, it is considered a miss.
i. If there is space available in the frames, the page is added to the frame.
ii. If there is no space available in the frames, find the page that will not be used for the longest duration of time in the future.
iii. Replace the page in the frame with the one that caused the miss.

3.Output the number of hits and misses.

Implementation of above approach 

Output

No. of hits = 3
No. of misses = 11

Complexity Analysis:

The time complexity of the algorithm depends on the number of page references (pn) and the number of frames (fn). The worst-case time complexity of the algorithm is O(pn * fn^2), which occurs when all page references are unique and there are no empty frames available. In this case, for each page reference, we may have to iterate through all the frames to check if the page is present and then iterate through all the remaining references to find the page that will not be needed for the longest period of time in the future.

However, in practice, the algorithm performs much better than the worst-case complexity, as it is rare to have all page references unique, and the number of frames is usually limited. Additionally, the algorithm has good performance when there are repeated page references, as it keeps such pages in the frames and minimizes page faults.

Overall, the Optimal Page Replacement algorithm is a useful algorithm for managing page frames in memory, and its time complexity is reasonable in practice.

Conclusion

Implementing the Optimal Page Replacement Algorithm provides a clear understanding of how memory management can be optimized to reduce page faults. Although this algorithm cannot be used in real-world scenarios due to its need for future knowledge, it serves as a benchmark for comparing other page replacement strategies. By learning how this algorithm works and coding it step-by-step, we gain valuable insights into designing efficient memory management systems and understanding the trade-offs involved in managing limited resources.

Comment
Article Tags: