Page Replacement Algorithms in OS: FIFO, LRU, Optimal and Solved Examples
Demand Paging allows the operating system to load a page only when it is needed. But what happens when a page fault occurs and every physical frame is already occupied?
The operating system must choose one page already present in RAM, remove or replace it, and use that frame for the incoming page.
The rule used to choose that page is called a Page Replacement Algorithm.
A Page Replacement Algorithm decides which page should be removed from physical memory when a page fault occurs and no free frame is available.
FIFO removes the page that entered memory first.
LRU removes the page that has not been used for the longest time in the past.
Optimal removes the page whose next use is farthest in the future.
The main objective is to reduce the number of page faults while keeping the replacement decision practical.
Why Do Page Replacement Algorithms Feel Difficult?
Students usually understand the definition of FIFO, LRU and Optimal. The real confusion starts when a long reference string is given and frames must be updated one page at a time.
FIFO = Who came first?
LRU = Who was used least recently?
Optimal = Who will be needed farthest in the future?
Why Do We Need Page Replacement?
In demand paging, a page fault occurs when the required page is not currently present in physical memory.
If a free frame exists, the solution is simple. The operating system can load the required page into that free frame.
But physical memory may already be full.
How Page Replacement Works
Important Terms Before Solving Page Replacement Problems
What Is a Page Hit?
Suppose RAM contains:
If the CPU now requests Page 4, Page 4 is already resident.
Hit Ratio = Number of Page Hits / Total Page References
What Is a Page Fault in Replacement Problems?
Again assume the frames contain:
If Page 5 is requested, it is not currently resident.
Page Fault Ratio = Number of Page Faults / Total Page References
Do We Replace a Page When a Free Frame Exists?
No.
This is an important exam rule.
Suppose there are three frames but currently only two frames are occupied.
If Page 7 arrives, it is simply loaded into the empty frame.
Required page is absent
AND
No usable free frame is available.
Three Main Page Replacement Algorithms
First In First Out
Remove the page that has stayed in memory for the longest time.
Think of a normal queue.
Least Recently Used
Remove the page that was used least recently in the past.
Think of recent usage history.
Optimal Replacement
Remove the page whose next use is farthest in the future.
Think of perfect future knowledge.
FIFO Page Replacement Algorithm
FIFO stands for First In First Out.
The page that entered physical memory first is selected for replacement first.
Oldest loaded page leaves first.
If a new page must enter and memory is full, P2 will be removed first because it entered the queue first.
FIFO Algorithm Step by Step
- Start with empty frames.
- Read the next page from the reference string.
- If the page is already present, count a Page Hit.
- If the page is absent, count a Page Fault.
- If a free frame exists, place the page there.
- If all frames are full, remove the page that entered memory earliest.
- Add the new page to the newest position of the FIFO queue.
- Continue until the entire reference string is processed.
Reference String Used in Our Solved Examples
We will use the same reference string for FIFO, LRU and Optimal so that the difference becomes very clear.
FIFO Solved Example with 3 Frames
| Reference | Frame 1 | Frame 2 | Frame 3 | Result |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault |
| 0 | 7 | 0 | - | Fault |
| 1 | 7 | 0 | 1 | Fault |
| 2 | 2 | 0 | 1 | Fault |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 3 | 1 | Fault |
| 0 | 2 | 3 | 0 | Fault |
| 4 | 4 | 3 | 0 | Fault |
| 2 | 4 | 2 | 0 | Fault |
| 3 | 4 | 2 | 3 | Fault |
| 0 | 0 | 2 | 3 | Fault |
| 3 | 0 | 2 | 3 | Hit |
| 2 | 0 | 2 | 3 | Hit |
Total Page Faults = 10
Advantages and Disadvantages of FIFO
Advantages
- Very simple to understand.
- Easy to implement using a queue.
- Replacement decision has low conceptual complexity.
- Does not require tracking detailed usage history.
Disadvantages
- Oldest page may still be heavily used.
- Ignores recent usage.
- Can generate more page faults than smarter policies.
- Can suffer from Belady's Anomaly.
What Is Belady's Anomaly?
Normally, students expect that increasing the number of frames should reduce page faults.
Surprisingly, with FIFO there are reference strings where increasing the number of frames can actually increase the number of page faults.
Belady's Anomaly is the phenomenon in which increasing the number of page frames causes more page faults for certain page replacement algorithms such as FIFO.
Do LRU and Optimal Suffer from Belady's Anomaly?
LRU and Optimal have an important theoretical property known as the stack property.
Informally, pages held with fewer frames form a subset of pages that can be held with more frames under these algorithms.
Optimal Page Replacement Algorithm
Optimal replacement is also called OPT or sometimes MIN.
It replaces the page whose next use occurs farthest in the future.
Look into future references.
Remove the resident page that will be needed latest in the future.
If a page will never be used again, that page is an excellent victim.
How Optimal Chooses a Victim Page
Suppose frames contain:
And the future reference string is:
Among pages 2, 0 and 3:
- Page 2 will be required relatively soon.
- Page 3 will also be required.
- Page 0 is needed later than some alternatives.
Optimal examines these future positions and selects the page whose next use is farthest away.
Optimal Algorithm Step by Step
- Read the next page reference.
- If the page is already present, count a Hit.
- If absent, count a Page Fault.
- If a free frame exists, use it.
- If memory is full, inspect future references of all resident pages.
- Find the resident page whose next use is farthest in the future.
- If a resident page is never referenced again, it can be selected.
- Replace the victim and continue.
Optimal Solved Example with 3 Frames
| Reference | Frame 1 | Frame 2 | Frame 3 | Result |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault |
| 0 | 7 | 0 | - | Fault |
| 1 | 7 | 0 | 1 | Fault |
| 2 | 2 | 0 | 1 | Fault |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 0 | 3 | Fault |
| 0 | 2 | 0 | 3 | Hit |
| 4 | 2 | 4 | 3 | Fault |
| 2 | 2 | 4 | 3 | Hit |
| 3 | 2 | 4 | 3 | Hit |
| 0 | 2 | 0 | 3 | Fault |
| 3 | 2 | 0 | 3 | Hit |
| 2 | 2 | 0 | 3 | Hit |
Total Page Faults = 7
Why Is Optimal Not Normally Implemented Exactly?
Optimal requires the operating system to know the exact future memory references of a process.
In a real running system, the operating system generally does not know the complete future reference sequence in advance.
It tells us the lowest page-fault count achievable for a given reference string and number of frames under the ideal future-knowledge model.
Advantages and Limitations of Optimal Replacement
Advantages
- Produces the minimum possible page faults for the given reference string.
- Excellent benchmark for comparing other algorithms.
- Easy to understand in solved examination problems.
- Does not suffer from Belady's Anomaly.
Limitations
- Requires knowledge of future references.
- Exact implementation is not generally practical online.
- Mainly used for analysis and comparison.
LRU Page Replacement Algorithm
LRU stands for Least Recently Used.
Instead of checking when a page entered memory, LRU checks when the page was last used.
Replace the resident page that has not been referenced for the longest time in the past.
Understanding "Least Recently Used"
Suppose frames contain:
Their recent usage order is:
Oldest Use
Most Recent
If another page must enter, LRU selects P2 because P2 was used least recently.
LRU Algorithm Step by Step
- Read the next reference.
- Check whether the page is already resident.
- If present, record its latest use.
- If absent, count a Page Fault.
- Use a free frame if available.
- If all frames are full, inspect the past use of resident pages.
- Replace the page whose last use occurred farthest in the past.
- Update usage information and continue.
LRU Solved Example with 3 Frames
| Reference | Frame 1 | Frame 2 | Frame 3 | Result |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault |
| 0 | 7 | 0 | - | Fault |
| 1 | 7 | 0 | 1 | Fault |
| 2 | 2 | 0 | 1 | Fault |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 0 | 3 | Fault |
| 0 | 2 | 0 | 3 | Hit |
| 4 | 4 | 0 | 3 | Fault |
| 2 | 4 | 0 | 2 | Fault |
| 3 | 4 | 3 | 2 | Fault |
| 0 | 0 | 3 | 2 | Fault |
| 3 | 0 | 3 | 2 | Hit |
| 2 | 0 | 3 | 2 | Hit |
Total Page Faults = 9
How Can LRU Be Implemented?
LRU needs information about recent page usage. There are several conceptual ways to maintain this information.
Why Can Exact LRU Be Expensive?
Exact LRU needs the system to know the precise recency order of memory references.
Updating detailed recency information for every memory access can create implementation overhead.
Advantages and Disadvantages of LRU
Advantages
- Uses actual recent reference behavior.
- Often performs better than simple FIFO.
- Works well with locality of reference.
- Does not exhibit Belady's Anomaly.
Disadvantages
- Exact recency tracking adds overhead.
- Requires additional implementation support.
- More complex than FIFO.
- Practical systems often approximate it.
FIFO vs LRU vs Optimal: Same Example Comparison
For the reference string:
Difference Between FIFO, LRU and Optimal
| Basis | FIFO | LRU | Optimal |
|---|---|---|---|
| Full Form | First In First Out | Least Recently Used | Optimal Page Replacement |
| Decision Basis | Arrival time | Past usage | Future usage |
| Victim Page | Oldest loaded page | Least recently referenced page | Page needed farthest in future |
| Main Data Needed | Queue order | Recency information | Future reference string |
| Implementation | Simple | More complex | Not exactly practical online |
| Belady's Anomaly | Can occur | Does not occur | Does not occur |
| Page Fault Quality | May be relatively high | Often good | Minimum possible for given reference string |
| Real-Time Future Knowledge | Not required | Not required | Required |
| Main Use | Simple replacement concept | Practical replacement principle | Benchmark and theoretical comparison |
What Happens to the Victim Page?
Choosing a victim page is only one part of replacement.
The operating system must also determine whether the victim page has been modified.
Local vs Global Page Replacement
Replacement can also be discussed based on which frames are eligible to be selected.
Local Replacement
A process chooses a victim only from the frames currently allocated to that process.
One process cannot directly take an arbitrary frame belonging to another process under the local policy.
Global Replacement
The system may choose a victim from a wider set of frames, potentially including frames currently used by other processes, according to the system's allocation policy.
How Does the Number of Frames Affect Page Faults?
In general, having more useful physical frames gives a process more room to keep its actively referenced pages resident.
This can reduce page faults.
However, FIFO teaches us an important exception: increasing frames does not guarantee fewer faults for every algorithm and every reference string.
FIFO may show Belady's Anomaly.
Page Replacement and System Performance
Every page fault can require expensive operating-system work and potentially storage I/O.
Therefore, a page replacement algorithm should attempt to keep pages that are likely to be needed and remove pages that are less useful.
Best Method to Solve Page Replacement Numericals
- Write the reference string clearly from left to right.
- Draw exactly the number of frames given in the question.
- Start with all frames empty.
- Process one reference at a time.
- First check whether the page is already present.
- Mark a Hit if present.
- Mark a Fault if absent.
- Use a free frame before performing replacement.
- When memory is full, apply only the rule of the selected algorithm.
- Do not change frame contents on a normal hit.
- Count faults only after completing the complete reference string.
- Recheck every replacement if the final answer looks suspicious.
Common Mistakes Students Make
Mistake 1: Replacing a Page on a Hit
If the requested page is already present, there is no page fault and no normal replacement is required.
Mistake 2: Using FIFO Order as LRU Order
FIFO asks when the page entered memory. LRU asks when the page was last referenced. These are not the same thing.
Mistake 3: Looking Backward in Optimal
Optimal looks forward into future references, not backward into past references.
Mistake 4: Looking Forward in LRU
LRU only needs past usage information.
Mistake 5: Ignoring Empty Frames
Do not select a victim page if an unused frame is still available.
Mistake 6: Counting First Loads as Hits
Loading a page into an empty frame because it was absent still produces a page fault.
Mistake 7: Updating FIFO Order on Every Hit
In standard FIFO, a hit does not make the page "new". FIFO order is based on when pages entered memory.
Mistake 8: Not Updating LRU Recency on Hits
In LRU, every reference matters. A hit makes that page recently used.
Interactive Page Replacement Simulator
Enter a reference string, number of frames and algorithm.
You can write the reference string with commas or spaces.
Are FIFO, LRU and Optimal the Only Page Replacement Algorithms?
No.
They are the three most important algorithms for understanding the basic concept, but several other policies also exist.
One-Minute Revision
Page Replacement Algorithms Exam Preparation
2-Mark Definition
A Page Replacement Algorithm is a policy used by the operating system to select a victim page when a required page must be loaded into physical memory and no free frame is available.
FIFO Short Answer
FIFO replaces the page that entered memory first. It can be implemented using queue order.
LRU Short Answer
LRU replaces the page that has not been referenced for the longest time in the past.
Optimal Short Answer
Optimal replaces the page whose next reference is farthest in the future. It produces the minimum page faults for the given reference string but requires future knowledge.
Belady's Anomaly
Belady's Anomaly is the condition in which increasing the number of frames increases the number of page faults for certain algorithms such as FIFO.
Important Numerical Rules
For a 5-Mark Question
- Define Page Replacement.
- Explain when replacement is required.
- Define FIFO, LRU and Optimal.
- Write basic advantages and disadvantages.
- Explain Belady's Anomaly.
For a 10-Mark Question
- Demand Paging background.
- Need for Page Replacement.
- Page Fault and Page Hit.
- Reference String and Frames.
- Victim Page.
- FIFO Algorithm.
- FIFO Solved Example.
- Belady's Anomaly.
- Optimal Algorithm.
- Optimal Solved Example.
- LRU Algorithm.
- LRU Solved Example.
- LRU Implementation.
- Dirty Page Effect.
- Comparison of FIFO, LRU and Optimal.
Frequently Asked Questions
What is a Page Replacement Algorithm?
A Page Replacement Algorithm selects a victim page when a required page must enter physical memory and no free frame is available.
What is FIFO Page Replacement?
FIFO replaces the page that entered memory first. It normally maintains pages in first-in-first-out order.
What is LRU Page Replacement?
LRU replaces the page that has not been referenced for the longest time in the past.
What is Optimal Page Replacement?
Optimal replacement selects the page whose next reference is farthest in the future.
Which Page Replacement Algorithm Gives Minimum Page Faults?
For a known reference string, the Optimal replacement algorithm gives the minimum possible number of page faults under its future-knowledge model.
Why Is Optimal Page Replacement Not Normally Practical?
Exact Optimal replacement requires knowledge of future page references, which a running operating system generally does not know in advance.
What Is Belady's Anomaly?
Belady's Anomaly is a situation where increasing the number of page frames causes more page faults for certain replacement algorithms such as FIFO.
Does LRU Have Belady's Anomaly?
LRU has the stack property and does not exhibit Belady's Anomaly.
What Is a Victim Page?
A victim page is the page selected by the replacement algorithm to leave physical memory so that its frame can be reused.
Does a Page Hit Cause Replacement?
No. If the requested page is already present in memory, it is a page hit and normal page replacement is not required.
Does a Page Fault Always Cause Replacement?
No. If a free frame is available, the required page can be loaded into that frame without replacing an existing resident page.
What Is the Main Difference Between FIFO and LRU?
FIFO uses page arrival order, while LRU uses recent page-reference history.
What Is the Main Difference Between LRU and Optimal?
LRU examines past references to find the least recently used page, while Optimal examines future references to find the page needed farthest in the future.
Conclusion
Page Replacement Algorithms become necessary when a page fault occurs, the required page is not resident, and no free physical frame is available.
The operating system must then select a victim page.
FIFO makes the decision using page arrival order. The oldest loaded page leaves first.
LRU uses recent history. The page that has not been used for the longest time is replaced.
Optimal uses future references. It selects the page whose next use is farthest away, producing the minimum possible page faults for the known reference string.
FIFO is simple but can suffer from Belady's Anomaly. LRU usually makes better locality-based decisions, but exact recency tracking has a cost.
Optimal provides the ideal benchmark but is not generally available as an exact online algorithm because the operating system does not know future references perfectly.
Page Hit = Page Already in RAM
Page Fault = Page Missing
Free Frame Available = No Victim Needed
Memory Full = Replacement Required
FIFO = Oldest Loaded Page
LRU = Least Recently Used Page
Optimal = Page Needed Farthest in Future
FIFO = Belady's Anomaly Possible
LRU and Optimal = No Belady's Anomaly
