Optimal Page Replacement Algorithm (original) (raw)

Last Updated : 14 Jan, 2025

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.

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:

optimal2n

**Final Tally:

**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:

optimal3n

**Final Tally:

Code Implementation