CS Engineering Gyan

Page Replacement Algorithms in Operating System

Page replacement is an important part of virtual memory management in an operating system. When a process requests a page that is not currently available in physical memory, a page fault occurs. If all available page frames are already occupied, the operating system must decide which existing page should be removed so that the required page can be loaded.

The method used to select the page that will be removed is called a page replacement algorithm. Different algorithms make this decision using different information such as the order in which pages entered memory, their recent usage, or their expected future usage.

Simple Definition

Page Replacement Algorithm is a technique used by an operating system to select a page for removal from physical memory when a new page must be loaded and all available frames are occupied.

What is a Page Fault?

A page fault occurs when a process references a page that is not currently present in its allocated physical memory frames. The operating system must then locate the required page in secondary storage, bring it into RAM, and update the necessary memory-management information.

Page Fault

Required Page Not Found in RAM

Operating System Loads the Page

Program Execution Continues

If an empty frame is available, the required page can be loaded directly. If no free frame exists, a page replacement algorithm is required to select a victim page for removal.

What is a Page Hit?

A page hit occurs when the requested page is already present in one of the available page frames. Since the page is already in physical memory, the operating system does not need to fetch it from secondary storage.

Remember:

Page Hit = Requested page is already in memory.
Page Fault = Requested page is not in memory.

Why is Page Replacement Required?

Physical memory is limited, while a process may reference more pages than can fit into the currently available frames. Therefore, when all frames are occupied and a new page is required, the operating system needs a systematic method for deciding which page should leave memory.

Important Terms Used in Page Replacement

Term Meaning
Page A fixed-size unit of a process's virtual address space.
Frame A fixed-size block of physical memory that can hold a page.
Reference String The sequence in which pages are requested by a process.
Page Hit The requested page is already present in memory.
Page Fault The requested page is absent from physical memory.
Victim Page The page selected for removal when a replacement is required.

Types of Page Replacement Algorithms

Several page replacement algorithms have been proposed to make replacement decisions. The most commonly studied algorithms in operating-system courses are:

  1. FIFO – First In First Out
  2. LRU – Least Recently Used
  3. Optimal Page Replacement
  4. MRU – Most Recently Used

1. FIFO Page Replacement Algorithm

FIFO stands for First In First Out. It replaces the page that has been present in memory for the longest time. The algorithm follows the same basic principle as a queue: the page that entered first is considered for removal first.

How FIFO Works

  1. Initially, all page frames are empty.
  2. Pages are loaded as they are referenced.
  3. If the requested page is already present, it is a page hit.
  4. If the requested page is absent and a free frame exists, it is loaded into that frame.
  5. If all frames are occupied, the oldest loaded page is removed.
  6. The new page is then inserted into the released frame.
FIFO page replacement algorithm solved example

Figure: FIFO Page Replacement Example

FIFO Solved Example

Consider the following page reference string with three available frames.

The first three different pages can occupy the three available frames. When page 2 is requested, all frames are occupied, so FIFO removes page 7 because it was the first page loaded into memory.

The same procedure continues for every subsequent page reference. Whenever the requested page is already present, the event is counted as a hit. Otherwise, a page fault occurs and FIFO replaces the oldest resident page.

FIFO Result

Page Fault Rate
= (Number of Page Faults / Total Page References) × 100

= (12 / 15) × 100
= 80%
Page Hit Rate
= (Number of Page Hits / Total Page References) × 100

= (3 / 15) × 100
= 20%
Important Correction:

Page fault rate is calculated using the total number of page references, not the number of page frames.

Correct Formula: Page Fault Rate = Page Faults / Total Page References × 100

Advantages of FIFO

Disadvantages of FIFO

Exam Tip:

FIFO removes the page that entered memory first. Do not confuse oldest page with least recently used page. These are different concepts.


2. LRU Page Replacement Algorithm

LRU stands for Least Recently Used. Instead of looking at the arrival order of pages, LRU considers their recent access history. When replacement becomes necessary, it removes the page that has not been used for the longest period of time.

How LRU Works

  1. Check whether the requested page is already in memory.
  2. If it is present, count a page hit and update its recent-use information.
  3. If it is absent and a free frame exists, load the page.
  4. If memory is full, identify the page whose previous access occurred farthest in the past.
  5. Replace that page with the requested page.
LRU page replacement algorithm solved example

Figure: LRU Page Replacement Example

LRU Solved Example

During execution, every page reference is checked against the four available frames. If the requested page is already present, the reference is a hit. Otherwise, a page fault occurs.

Once all four frames are occupied, LRU examines the previous access history. The page that has remained unused for the longest time is selected as the victim.

LRU Result

Page Fault Rate
= (8 / 20) × 100
= 40%
Page Hit Rate
= (12 / 20) × 100
= 60%
Key Idea:

LRU does not remove the oldest page merely because it arrived first. It removes the page whose most recent use is farthest in the past.

Advantages of LRU

Disadvantages of LRU


3. Optimal Page Replacement Algorithm

The Optimal Page Replacement Algorithm, also known as OPT or MIN, replaces the page whose next use occurs farthest in the future.

If a page will never be referenced again, it becomes an ideal candidate for replacement. Because the algorithm assumes knowledge of future references, it provides a theoretical lower bound on the number of page faults for a particular reference string and number of frames.

How Optimal Replacement Works

  1. Check whether the requested page is already in memory.
  2. If it is present, count a hit.
  3. If a free frame exists, load the requested page.
  4. If all frames are full, inspect future references.
  5. Find the resident page whose next use is farthest in the future.
  6. Replace that page with the requested page.
Optimal page replacement algorithm solved example

Figure: Optimal Page Replacement Example

Optimal Algorithm Example

When all four frames are occupied and a new page is required, the algorithm looks ahead in the reference string. It determines which currently resident page will be required farthest in the future and replaces that page.

Optimal Result

Page Fault Rate
= (7 / 15) × 100
46.67%
Page Hit Rate
= (8 / 15) × 100
53.33%
Important Limitation:

An actual operating system cannot normally know the exact future page references of a running program. Therefore, Optimal replacement is mainly used as a theoretical benchmark for comparing other algorithms.

Advantages of Optimal Replacement

Disadvantages of Optimal Replacement


4. MRU Page Replacement Algorithm

MRU stands for Most Recently Used. It follows a strategy opposite to LRU in one important respect. When replacement is required, MRU selects the page that was accessed most recently.

This approach may be useful for workloads where recently accessed pages are less likely to be referenced again immediately. However, it is not generally the default choice for every workload.

How MRU Works

  1. Check whether the requested page is present.
  2. If present, count a hit and mark it as recently used.
  3. If a free frame exists, load the requested page.
  4. If all frames are occupied, identify the most recently accessed page.
  5. Remove that page and load the new page.
MRU page replacement algorithm example

Figure: MRU Page Replacement Example

MRU Example

The reference string contains 20 page requests. Using the MRU replacement rule, a page fault occurs whenever the requested page is not in memory. Once the frames become full, the most recently accessed resident page is selected for replacement.

MRU Result

Page Fault Rate
= (12 / 20) × 100
= 60%
Page Hit Rate
= (8 / 20) × 100
= 40%

Important Point About MRU

MRU should not be considered universally better or worse than LRU. Its usefulness depends on the access pattern of the application. The main distinction is simple: LRU removes the least recently accessed page, whereas MRU removes the most recently accessed page.


Page Fault Rate and Page Hit Rate

Page fault rate and page hit rate are useful measures for analyzing the behavior of a page replacement algorithm. They must be calculated with respect to the total number of page references.

Page Fault Rate

= (Number of Page Faults / Total Number of Page References) × 100
Page Hit Rate

= (Number of Page Hits / Total Number of Page References) × 100
Relationship

Page Fault Rate + Page Hit Rate = 100%
Common Formula Mistake:

Do not calculate page fault rate as Page Faults / Number of Frames. The number of frames describes the memory configuration, while the rate describes how many references resulted in faults.

Comparison of Page Replacement Algorithms

Algorithm Replacement Basis Future Knowledge Required? Main Advantage Main Limitation
FIFO Oldest loaded page No Simple implementation May remove frequently used pages
LRU Least recently used page No Uses recent access behavior Requires usage tracking
Optimal Page used farthest in future Yes Minimum theoretical page faults Future references are not known in practice
MRU Most recently used page No Useful for selected access patterns Can perform poorly for common locality patterns

FIFO vs LRU vs Optimal vs MRU

The main difference between these algorithms is the information used to select the victim page.

Why Does the Number of Page Faults Matter?

A page fault can involve accessing secondary storage, which is considerably slower than accessing RAM. Therefore, excessive page faults can increase the time required to satisfy memory references.

A good replacement strategy attempts to keep useful pages in memory and avoid unnecessary transfers. However, the best algorithm depends on the characteristics of the workload, available hardware support, implementation cost, and access pattern.

Important Relationship:

Fewer page faults generally mean fewer expensive page transfers, but the replacement algorithm itself may introduce computational or hardware overhead. Therefore, algorithm selection is a trade-off rather than a single universal rule.

Belady's Anomaly

Belady's Anomaly is a well-known phenomenon associated with some page replacement strategies, particularly FIFO. It describes a situation where increasing the number of available page frames can unexpectedly increase the number of page faults for a particular reference string.

This result may seem counterintuitive because more frames normally appear to provide more room for pages. The phenomenon demonstrates that the behavior of a replacement algorithm depends on its replacement policy and the reference pattern.

Exam Point:

Remember the association: FIFO → Belady's Anomaly.

Locality of Reference and Page Replacement

Programs often exhibit locality of reference, meaning that a process tends to access a relatively small group of pages repeatedly during a particular period.

Temporal locality means that recently used data or instructions are likely to be used again soon. Spatial locality means that addresses near a recently accessed location may also be accessed.

LRU is particularly connected with temporal locality because it attempts to retain pages that have been accessed recently.

Common Mistakes in Page Replacement Problems

  1. Counting a page hit as a page fault.
    If the requested page already exists in a frame, it is a hit.
  2. Using the wrong denominator for page fault rate.
    Use total page references, not the number of frames.
  3. Confusing FIFO and LRU.
    FIFO considers arrival order, while LRU considers recent usage.
  4. Using future information incorrectly in Optimal.
    The page whose next reference occurs farthest in the future is selected.
  5. Forgetting that empty frames are used before replacement.
    Replacement is necessary only after all available frames are occupied.
  6. Using frame count as total references.
    Frames and references represent two different quantities.

Exam-Oriented Short Notes

FIFO: Replaces the page that entered memory earliest. It is simple and queue based.

LRU: Replaces the page that has not been used for the longest period.

Optimal: Replaces the page whose next use is farthest in the future. It gives the theoretical minimum number of page faults.

MRU: Replaces the page that was accessed most recently.

Page Fault Rate: Page Faults divided by total page references, multiplied by 100.

Page Hit Rate: Page Hits divided by total page references, multiplied by 100.

Frequently Asked Questions

1. What is a page replacement algorithm?

It is a method used by an operating system to select a page for removal when a new page must be loaded and all available frames are occupied.

2. What is a page fault?

A page fault occurs when a requested page is not currently present in physical memory.

3. What is a page hit?

A page hit occurs when the requested page is already present in a physical memory frame.

4. Which page does FIFO replace?

FIFO replaces the page that entered memory first among the currently resident pages.

5. Which page does LRU replace?

LRU replaces the page that has not been accessed for the longest period.

6. Why is Optimal called the best theoretical algorithm?

Because, when the complete future reference sequence is known, it can select replacements that produce the minimum possible number of page faults.

7. Can Optimal page replacement be directly implemented in a real OS?

Exact Optimal replacement is generally impractical because the operating system does not know the complete future reference sequence of a process.

8. What is the correct formula for page fault rate?

Page Fault Rate = (Page Faults / Total Page References) × 100.

9. Which algorithm can suffer from Belady's Anomaly?

FIFO is the standard example associated with Belady's Anomaly.

10. Is LRU always better than FIFO?

LRU often performs well for workloads with strong temporal locality, but no replacement strategy should be assumed to be universally optimal for every workload.

Interview Questions on Page Replacement

  1. What happens when all page frames are full and a page fault occurs?
    The operating system selects a victim page using the configured replacement algorithm and loads the required page into the released frame.
  2. What is the difference between FIFO and LRU?
    FIFO uses page arrival order, whereas LRU uses the time since a page was last accessed.
  3. Why can FIFO suffer from Belady's Anomaly?
    FIFO's queue-based replacement order does not necessarily preserve the pages that are most useful for future references.
  4. Why is Optimal replacement not normally used in practice?
    Because exact future page references are unavailable during normal execution.
  5. What is the difference between a page fault and a page hit?
    A fault means the page is absent from memory, while a hit means the page is already present.
  6. What is a victim page?
    It is the resident page selected for removal to make room for the requested page.
  7. What is page fault rate?
    It is the percentage of page references that result in page faults.
  8. What is MRU?
    MRU is Most Recently Used and selects the most recently accessed page for replacement.

Practice Problems

Practice Problem 1: Page Fault Rate

A reference string contains 25 page requests. An algorithm produces 10 page faults. Calculate the page fault rate and page hit rate.

Show Answer
Page Fault Rate = (10 / 25) × 100 = 40%

Page Hits = 25 − 10 = 15
Page Hit Rate = (15 / 25) × 100 = 60%

Practice Problem 2: FIFO Concept

Three frames contain pages A, B and C in that order. A new page D arrives and memory is full. Which page will FIFO replace?

Show Answer
FIFO replaces page A because it entered memory before B and C.

Practice Problem 3: LRU Concept

Four pages are currently present in memory. Page A was accessed 10 references ago, B was accessed 5 references ago, C was accessed 2 references ago and D was accessed 1 reference ago. Which page would LRU select for replacement?

Show Answer
LRU selects page A because it was used least recently.

Practice Problem 4: Page Fault and Hit Count

A page reference sequence contains 30 references. If 9 references result in page faults, determine the number of page hits and both rates.

Show Answer
Page Hits = 30 − 9 = 21

Page Fault Rate = (9 / 30) × 100 = 30%
Page Hit Rate = (21 / 30) × 100 = 70%

Quick Revision: Page Replacement Algorithms

Conclusion

Page replacement algorithms are an essential component of virtual memory management because physical memory cannot hold every page that a process may need during execution. Whenever a requested page is absent and no free frame is available, the operating system must select a resident page for replacement.

FIFO makes this decision using the arrival order of pages. LRU uses recent access history, while Optimal examines future references and therefore serves as an ideal theoretical benchmark. MRU uses the opposite principle of LRU by selecting the most recently accessed page.

For numerical questions, students should carefully distinguish between a page fault and a page hit, correctly maintain the contents of each frame, and use the total number of page references when calculating page fault or hit rates. Understanding these fundamentals makes problems involving FIFO, LRU, Optimal and MRU much easier to solve.

One-Line Revision:

FIFO = Oldest Page | LRU = Least Recently Used | Optimal = Farthest Future Use | MRU = Most Recently Used
← Previous: Virtual Memory Next: File Management →
Home Visit Our YouTube Channel