Open In App

Optimal Page Replacement Algorithm

Last Updated : 14 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In operating systems, whenever a new page is referred and not present in memory, page fault occurs, and Operating System replaces one of the existing pages with newly needed page. Different page replacement algorithms suggest different ways to decide which page to replace. The target for all algorithms is to reduce number of page faults. In this algorithm, OS replaces the page that will not be used for the longest period of time in future.

  • Optimal page replacement needs to know which pages will be used in the future, which is not possible in real-world scenarios.
  • In practical systems, it is impossible to predict exactly which pages will be needed later.
  • It is mainly used to analyze and measure the efficiency of practical algorithms, not for actual implementation.

What is Optimal Page Replacement Algorithm

Optimal Page Replacement is one of the Algorithms of Page Replacement. In this algorithm, pages are replaced which would not be used for the longest duration of time in the future.

The idea is simple, for every reference we do following:

  1. If referred page is already present, increment hit count.
  2. If not present, find if a page that is never referenced in future. If such a page exists, replace this page with new page. If no such page exists, find a page that is referenced farthest in future. Replace this page with new page.

Let’s consider some examples:

Example 1:

Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4-page frame. Find number of page fault using Optimal Page Replacement Algorithm.

optimal1n

Example 2:

Input: Number of frames = 3, Reference String = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3}

Page-by-page analysis:

  • 7, 0, 1, 2: First four pages fill the frames → Misses = 4
  • 0: Already in the frame → Hit
  • 3: Replaces 7 (not used for the longest) → Miss
  • 0: Already in the frame → Hit
  • 4: Replaces 1 → Miss
  • 2, 3: All already in frames → Hits = 2
optimal2n

Final Tally:

  • Hits = 4
  • Misses = 6

Example 3:

Input: Number of frames = 3, Reference String = {6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1, 7, 9, 6}

Page-by-page analysis:

  • 6, 7, 8, 9: First four pages fill the frames → Misses = 4
  • 6: Already in the frame → Hit
  • 7: Already in the frame → Hit
  • 1: replaces 9 → Miss
  • 6, 7: Already in the frame → Hit
  • 8, 9: 8 replaces 6 and 9 replaces 8 → Misses = 2
  • 1, 7, 9: already in frames → Hits = 3
  • 6: 6 replaces 9 → Miss
optimal3n

Final Tally:

  • Hits = 6
  • Misses = 8

Code Implementation


Next Article
Article Tags :
Practice Tags :

Similar Reads