- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- Operating System Processes
- Process Control Block
- Operations on Processes
- Inter Process Communication
- Context Switching
- Multi-threading
- Scheduling Algorithms
- Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- Turn Around Time & Waiting Time
- Burst Time in SJF Scheduling
- Process Synchronization
- Process Synchronization
- Critical Section Problem
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Semaphores
- Counting Semaphores
- Mutex
- Turn Variable
- Bounded Buffer Problem
- Reader Writer Locks
- Test and Set Lock
- Peterson's Solution
- Monitors
- Sleep and Wake
- Race Condition
- OS Deadlock
- Introduction to Deadlock
- Conditions for Deadlock
- Deadlock Handling
- Deadlock Prevention
- Deadlock Avoidance (Banker's Algorithm)
- Deadlock Detection and Recovery
- Deadlock Ignorance
- Memory Management
- Memory Management
- Contiguous Memory Allocation
- Non-Contiguous Memory Allocation
- First Fit Algorithm
- Next Fit Algorithm
- Best Fit Algorithm
- Worst Fit Algorithm
- Fragmentation
- Virtual Memory
- Segmentation
- Buddy System
- Slab Allocation
- Overlays
- Paging and Page Replacement
- Paging
- Demand Paging
- Page Table
- Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Belady's Anomaly
- Thrashing
- Storage and File Management
- File Systems
- File Attributes
- Structures of Directory
- Linked Index Allocation
- Indexed Allocation
- Disk Scheduling Algorithms
- FCFS Disk Scheduling
- SSTF Disk Scheduling
- SCAN Disk Scheduling
- LOOK Disk Scheduling
- I/O Systems
- I/O Hardware
- I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Operating System - Page Replacement Algorithms
If an operating system uses paging for memory management, there is a chance for page faults to occur. During a page fault, if no free frame is available in physical memory(RAM), the operating system should decide which page to remove from physical memory to make space for the new page. This decision is made using a page replacement algorithms.
There are several page replacement algorithms used by operating systems. Some of these algorithms are good at reducing the number of page faults and some are easy to implement. So the choice of page replacement algorithm can affect the performance of your system. In this chapter, we will discuss following page replacement algorithms −
- First-In-First-Out (FIFO) Page Replacement
- Least Recently Used (LRU) Page Replacement
- Least Frequently Used (LFU) Page Replacement
- Optimal Page Replacement
First-In-First-Out (FIFO) Page Replacement
In the FIFO page replacement algorithm, the page that has entered in the memory earliest will be replaced first. Meaning the oldest page in memory will be removed to make space for the new page. This algorithm is simple to implement using a queue data structure. The operating system maintains a queue of pages in memory. When a page needs to be replaced, the page at the front of the queue (i.e., the oldest page) is removed, and the new page is added to the back of the queue.
Example
The image below shows how main memory behaves when a page reference string of 5, 0, 1, 7, 2, 0, 2, 8 is processed using FIFO page replacement algorithm with 4 frames −
In this example, the initial pages 5, 0, 1, 7 are loaded directly into the empty frames. Then, the next page 2 is referenced. Now, the frames are full, so according to FIFO, the oldest page 5 will be removed and 2 will be loaded into its place.
This process continues for the rest of the page references. The table below shows each step of the process, including the current page reference −
| Step | Reference | Frames (Oldest to Newest) | Page fault? | Removed page |
|---|---|---|---|---|
| 1 | 5 | 5 | Yes (1) | â |
| 2 | 0 | 5, 0 | Yes (2) | â |
| 3 | 1 | 5, 0, 1 | Yes (3) | â |
| 4 | 7 | 5, 0, 1, 7 | Yes (4) | â |
| 5 | 2 | 0, 1, 7, 2 | Yes (5) | 5 (oldest) |
| 6 | 0 | 0, 1, 7, 2 | No | â |
| 7 | 2 | 0, 1, 7, 2 | No | â |
| 8 | 8 | 1, 7, 2, 8 | Yes (6) | 0 (oldest) |
- Total Page Faults = 6
- Page Fault Rate = 6/8 = 75%
- Pages Removed = 5, 0
The main drawback of FIFO page replacement algorithm is Belady's Anomaly. It states that increasing the number of page frames can sometimes increase the number of page faults. We have a complete chapter on Belady's Anomaly where we explained this concept with examples.
Least Recently Used (LRU) Page Replacement
The LRU is another popular page replacement algorithm. In this algorithm, the page that has not been used for the longest period of time will be replaced first. Meaning, the page that was least recently used will be removed to make space for the new page. This work like memory cache, where the item that has not been touched for the longest time is removed first.
This algorithm uses timestamps to keep track of page usage. When a page is accessed, its timestamp is updated to the current time. When a page needs to be replaced, the page with the oldest timestamp (i.e., the least recently used page) is removed from memory. That's how the LRU algorithm works.
Example
Consider the a page reference string of 1, 2, 3, 1, 4, 5 and with 3 frames of physical memory. That is,
Page Reference String: 1, 2, 3, 1, 4, 5 Number of Frames: 3
The initial pages 1, 2, 3 are loaded directly into the 3 empty frames. When the next page 1 is referenced, it is already in memory, so no page fault occurs. Next, page 4 is referenced, this can cause a page fault. So the LRU algorithm will remove page 2. (If it was FIFO, it would have removed page 1, because 2 is least recently used and 1 is oldest page).
This process continues for the rest of the page references. The table below shows all the steps of the process −
| Step | Reference | Frames (Oldest to Newest) | Page fault? | Removed page |
|---|---|---|---|---|
| 1 | 1 | 1 | Yes (1) | â |
| 2 | 2 | 1, 2 | Yes (2) | â |
| 3 | 3 | 1, 2, 3 | Yes (3) | â |
| 4 | 1 | 1, 2, 3 | No | â |
| 5 | 4 | 1, 3, 4 | Yes (4) | 2 |
| 6 | 5 | 3, 4, 5 | Yes (5) | 1 |
- Total Page Faults = 5
- Page Fault Rate = 5/6 = 83.33%
- Pages Removed = 2, 1
The main drawback of LRU algorithm is that it can be expensive and difficult to implement. We need to keep track of the order of page usage, which requires data structures like stacks or linked lists. Also, updating the timestamps for each page access can be costly in terms of time and space complexity. However, LRU is the most popular page replacement algorithm used in many operating systems.
Least Frequently Used (LFU) Page Replacement
The LFU page replacement algorithm removes the page that has been used the least number of times. In this algorithm, each page has a counter that keeps track of how many times it has been accessed. When a page needs to be replaced, the page with the lowest access count is removed from memory. If the access counts are the same, then you can use FIFO or LRU to break the tie ( depending on the implementation).
Example
Consider the following page reference string and frames −
Reference string: 1, 2, 3, 1, 2, 4, 1, 2, 5 Frames = 3
The initial pages 1, 2, 3 are loaded directly into the empty frames. All of these pages will have an access count of 1. When the next page 1 and 2 are referenced, they are already in memory, so their access counts will be incremented to 2. Next page 4 is referenced, which causes a page fault. So the LFU algorithm will remove page 3 since the access count of page 3 is 1 (lowest).
This process continues for the rest of the page references. The table below shows all the steps of the process. Here the 1:2 means page 1 has a frequency of 2.
| Step | Reference | Frequency Table (1,2,3,4,5) | Frames | Page Fault? |
|---|---|---|---|---|
| 1 | 1 | 1:1 | 1 | Yes |
| 2 | 2 | 1:1, 2:1 | 1,2 | Yes |
| 3 | 3 | 1:1, 2:1, 3:1 | 1,2,3 | Yes |
| 4 | 1 | 1:2, 2:1, 3:1 | 1,2,3 | No |
| 5 | 2 | 1:2, 2:2, 3:1 | 1,2,3 | No |
| 6 | 4 | 1:2, 2:2, 3:1 → Remove 3 (lowest freq=1) | 1,2,4 | Yes |
| 7 | 1 | 1:3, 2:2, 4:1 | 1,2,4 | No |
| 8 | 2 | 1:3, 2:3, 4:1 | 1,2,4 | No |
| 9 | 5 | 1:3, 2:3, 4:1 → Remove 4 (lowest freq=1) | 1,2,5 | Yes |
- Total Page Faults = 5
- Page Fault Rate = 5/9 = 55.56%
- Pages Removed = 3, 4
Compared to FIFO and LRU, the LFU algorithm have improved page fault rate (55.56% vs 75% and 83.33%). But there is a drawback of LFU algorithm. It can lead to starvation of pages that are not regularly used. For example, if a page is accessed once at the beginning and then not used again, it will hard to get back into memory because its access count will remain low.
Optimal Page Replacement
The optimal page replacement algorithm is a theoretical concept which is very difficult to be implemented in real operating systems. This algorithm works by replacing the page that will not be used for the longest period of time in the future. Meaning, it involves predicting what pages will be used in the future and replacing the one that will not be used for the longest time. This is considered as the best page replacement algorithm.
Let's solve an example that involves the optimal page replacement algorithm to understand how it works.
Example
Consider the following page reference string and frames −
Page Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 Frames: 4
Here, the first four pages 7, 0, 1, 2 are loaded directly into the first four empty frames. When the next page 0 is referenced, it is already in memory, so no page fault occurs. Next, page 3 is referenced, which causes a page fault. So the optimal algorithm will remove page 1 since it will not be used for the longest period of time in the future (it won't be used again).
The table below shows all the steps of the process −
| 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | 0 | 3 | 2 | 3 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | ||||
| 1 | 1 | 1 | 1 | 1 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | ||
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 7 | 7 | 7 | 7 | 7 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
| Miss | Miss | Miss | Miss | Hit | Miss | Hit | Miss | Hit | Hit | Hit | Hit | Hit | Hit |
We are able to implement this algorithm as we already know the entire page reference string. But that will not be the case in real-world scenarios. You never know what pages will be referenced in the future. That's why we told earlier that this algorithm is just a theoretical model.
Conclusion
In this chapter, we covered four page replacement algorithms: FIFO, LRU, LFU, and Optimal. FIFO algorithm is used when you want a simple and easy to implement algorithm. LRU is the most popular algorithm used in many operating systems. It provides a good balance between performance and complexity. LFU algorithm can be useful in scenarios where certain pages are accessed more frequently than others. Optimal algorithm is the best possible page replacement algorithm, but it is not practical for real-world use.