Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Numerical on LRU, FIFO
In this article, we will explore LRU (Least Recently Used) and FIFO (First In First Out) page replacement algorithms through detailed numerical examples, examining their step-by-step execution and practical applications.
Least Recently Used Algorithm
The LRU (Least Recently Used) algorithm is a popular page replacement strategy used in operating systems and cache management. When the cache is full and a new page needs to be loaded, LRU replaces the page that has been accessed least recently, based on the principle that recently accessed pages are more likely to be accessed again.
LRU Numerical Example
Given: Cache capacity = 3 pages, Page sequence = 2, 3, 1, 4, 2, 1, 5, 2
| Step | Page Request | Cache State | Page Fault? | Action |
|---|---|---|---|---|
| 1 | 2 | [2] | Yes | Load page 2 |
| 2 | 3 | [2, 3] | Yes | Load page 3 |
| 3 | 1 | [2, 3, 1] | Yes | Load page 1 |
| 4 | 4 | [3, 1, 4] | Yes | Replace LRU page 2 with 4 |
| 5 | 2 | [1, 4, 2] | Yes | Replace LRU page 3 with 2 |
| 6 | 1 | [4, 2, 1] | No | Page 1 found, move to front |
| 7 | 5 | [2, 1, 5] | Yes | Replace LRU page 4 with 5 |
| 8 | 2 | [1, 5, 2] | No | Page 2 found, move to front |
LRU Results: Final cache state = [1, 5, 2], Total page faults = 6
First-In-First-Out Algorithm
The FIFO (First-In-First-Out) algorithm is a simple page replacement strategy that removes the oldest page in the cache when space is needed. It follows a queue-based approach where the first page loaded is the first to be replaced, regardless of recent access patterns.
FIFO Numerical Example
Given: Cache capacity = 3 pages, Page sequence = 2, 3, 1, 4, 2, 1, 5, 2
| Step | Page Request | Cache State | Page Fault? | Action |
|---|---|---|---|---|
| 1 | 2 | [2] | Yes | Load page 2 |
| 2 | 3 | [2, 3] | Yes | Load page 3 |
| 3 | 1 | [2, 3, 1] | Yes | Load page 1 |
| 4 | 4 | [3, 1, 4] | Yes | Replace oldest page 2 with 4 |
| 5 | 2 | [1, 4, 2] | Yes | Replace oldest page 3 with 2 |
| 6 | 1 | [1, 4, 2] | No | Page 1 found in cache |
| 7 | 5 | [4, 2, 5] | Yes | Replace oldest page 1 with 5 |
| 8 | 2 | [4, 2, 5] | No | Page 2 found in cache |
FIFO Results: Final cache state = [4, 2, 5], Total page faults = 6
Comparison
| Aspect | LRU | FIFO |
|---|---|---|
| Replacement Strategy | Replace least recently used page | Replace oldest page in queue |
| Implementation Complexity | More complex (requires tracking access times) | Simple (queue-based) |
| Memory Overhead | Higher (timestamp/counter storage) | Lower (only queue pointers) |
| Performance | Generally better (considers locality) | May suffer from Belady's anomaly |
| Page Faults (Example) | 6 | 6 |
Use Cases
LRU Applications
CPU Cache Management Modern processors use LRU for cache line replacement in L1, L2, and L3 caches.
Database Buffer Pools Database systems use LRU to manage which data pages remain in memory.
Web Browser Cache Browsers implement LRU to manage cached web pages and resources.
FIFO Applications
Print Queue Management Print jobs are processed in the order they are submitted.
Disk Scheduling Simple disk scheduling algorithms use FIFO for request processing.
Network Packet Buffering Routers and switches use FIFO queues for packet forwarding.
Conclusion
LRU and FIFO are fundamental page replacement algorithms with distinct characteristics. LRU provides better performance by considering access patterns but requires more overhead, while FIFO offers simplicity at the cost of potentially suboptimal replacements. The choice between them depends on system requirements, available resources, and performance priorities.
