0
0
Operating Systemsknowledge~6 mins

Page replacement algorithms (FIFO, LRU, Optimal) in Operating Systems - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine your computer's memory is full, but it needs to load new information. It must decide which old information to remove to make space. Page replacement algorithms help the computer choose which data to remove when memory is full.
Explanation
FIFO (First-In, First-Out)
This method removes the oldest page in memory first, like a queue where the first item added is the first removed. It does not consider how often or recently a page was used, only the order of arrival. This simplicity can sometimes cause poor choices if old pages are still needed.
FIFO removes the oldest loaded page without considering usage.
LRU (Least Recently Used)
LRU removes the page that has not been used for the longest time, assuming that pages used recently will likely be used again soon. It tracks page usage over time to make smarter decisions. This method often performs better than FIFO but requires more tracking effort.
LRU removes the page that was least recently accessed.
Optimal Page Replacement
This algorithm removes the page that will not be used for the longest time in the future. It gives the best possible performance but requires knowing future requests, which is usually impossible in real life. It is mainly used as a benchmark to compare other algorithms.
Optimal removes the page that won’t be needed for the longest future time.
Real World Analogy

Imagine a small desk with limited space for books. FIFO means you remove the book you placed first, no matter if you still need it. LRU means you remove the book you haven't looked at for the longest time. Optimal means you magically know which book you won't need for the longest time and remove that one.

FIFO (First-In, First-Out) → Removing the oldest book placed on the desk first, regardless of use.
LRU (Least Recently Used) → Removing the book you haven't read or looked at for the longest time.
Optimal Page Replacement → Removing the book you know you won’t need for the longest time in the future.
Diagram
Diagram
┌───────────────┐
│ Memory Frames │
├───────────────┤
│ FIFO          │
│ ┌───────────┐ │
│ │ Oldest    │ │
│ │ page out  │ │
│ └───────────┘ │
├───────────────┤
│ LRU           │
│ ┌───────────┐ │
│ │ Least     │ │
│ │ recently  │ │
│ │ used page │ │
│ └───────────┘ │
├───────────────┤
│ Optimal       │
│ ┌───────────┐ │
│ │ Future    │ │
│ │ longest   │ │
│ │ unused    │ │
│ │ page out  │ │
│ └───────────┘ │
└───────────────┘
Diagram showing three page replacement strategies and which page they choose to remove.
Key Facts
Page ReplacementThe process of deciding which memory page to remove when new pages need space.
FIFORemoves the oldest page loaded into memory first.
LRURemoves the page that has not been used for the longest time.
Optimal AlgorithmRemoves the page that will not be used for the longest time in the future.
Page FaultAn event when a requested page is not in memory and must be loaded.
Common Confusions
Believing FIFO always removes the least useful page.
Believing FIFO always removes the least useful page. FIFO removes the oldest page without checking if it is still needed, so it can remove useful pages.
Thinking LRU predicts future page use.
Thinking LRU predicts future page use. LRU only tracks past usage, not future requests.
Assuming Optimal algorithm is practical for real systems.
Assuming Optimal algorithm is practical for real systems. Optimal requires future knowledge, which is usually impossible, so it is mainly theoretical.
Summary
Page replacement algorithms decide which memory page to remove when space is needed.
FIFO removes the oldest page, LRU removes the least recently used page, and Optimal removes the page not needed for the longest future time.
Optimal is the best in theory but not practical; LRU often performs better than FIFO in real systems.