Page Replacement Algorithms
Page Replacement Algorithms are a core concept of Operating Systems and an essential part of Virtual Memory Management. Whenever physical memory (RAM) becomes full and a new page needs to be loaded, the operating system must decide which existing page to remove. This decision is made using page replacement algorithms.
If you haven't read about Virtual Memory yet, read it here:
1. Why Page Replacement is Needed
In demand paging systems, pages are loaded only when required. However, RAM size is limited. When memory is full and a new page must be loaded, the OS must:
- Select a page currently in memory.
- Remove it (if necessary write back to disk).
- Load the new page into the freed frame.
Choosing the wrong page increases page faults and decreases performance.
2. Performance Metric: Page Fault Rate
The main goal of page replacement algorithms is to minimize Page Faults.
Lower Page Fault Rate = Better Performance
3. FIFO (First-In First-Out) Algorithm
FIFO replaces the page that has been in memory for the longest time. It works like a queue.
Steps:
- Insert pages in order of arrival.
- When memory is full, remove the oldest page.
Example:
Reference String: 1, 2, 3, 4, 1, 2, 5 Frames = 3
Page Faults occur when new pages replace oldest ones.
Advantages
- Simple implementation
- Low overhead
Disadvantages
- May remove frequently used pages
- Suffers from Belady’s Anomaly
4. Belady’s Anomaly
Belady’s Anomaly occurs when increasing the number of frames increases the number of page faults. FIFO suffers from this problem.
5. Optimal Page Replacement Algorithm
The Optimal algorithm replaces the page that will not be used for the longest time in the future.
It provides the minimum possible page faults.
Disadvantage
Future knowledge is impossible in real systems, so it is mainly theoretical.
6. LRU (Least Recently Used) Algorithm
LRU replaces the page that has not been used for the longest time in the past.
How It Works:
- Track usage history.
- Replace least recently accessed page.
Advantages
- Good performance
- No Belady’s anomaly
Disadvantages
- High implementation cost
- Requires extra hardware or counters
7. LFU (Least Frequently Used)
LFU replaces the page with the lowest access frequency.
Problem
Old pages with high count may stay forever.
8. Clock (Second Chance) Algorithm
Clock algorithm is an improved version of FIFO. It gives pages a second chance using a reference bit.
Working:
- If reference bit = 0 → Replace page
- If reference bit = 1 → Set to 0 and skip
Efficient and widely used in modern systems.
9. Comparison Table
| Algorithm | Belady's Anomaly | Implementation | Performance |
|---|---|---|---|
| FIFO | Yes | Simple | Moderate |
| Optimal | No | Theoretical | Best |
| LRU | No | Complex | Very Good |
| LFU | No | Moderate | Good |
| Clock | No | Efficient | Very Good |
10. Numerical Example (Exam Important)
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3 Frames = 3
Students are often asked to compute page faults using FIFO, LRU, and Optimal.
Practice calculating faults manually for exams.
11. Which Algorithm is Best?
- Optimal → Theoretical best
- LRU → Practical best
- Clock → Efficient real-world solution
12. Real-World Implementation
Modern operating systems use variations of:
- LRU approximation
- Clock algorithm
- Working Set Model
Conclusion
Page Replacement Algorithms are crucial for efficient memory management. Understanding FIFO, LRU, Optimal, LFU, and Clock helps in exams, interviews, and system-level programming.
Frequently Asked Questions (FAQs)
What is page replacement?
Page replacement is the process of selecting a page to remove from memory when RAM is full.
Which algorithm is best?
Optimal is theoretically best, but LRU is widely used in practice.
What is Belady’s anomaly?
It is a phenomenon where increasing frames increases page faults (seen in FIFO).
Comments
Post a Comment