Page Replacement Algorithms in Operating Systems
Last Updated :
21 Mar, 2025
In an operating system that uses paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when a new page comes in. Page replacement becomes necessary when a page fault occurs and no free page frames are in memory. in this article, we will discuss different types of page replacement algorithms.
Page Replacement Algorithms
Page replacement algorithms are techniques used in operating systems to manage memory efficiently when the physical memory is full. When a new page needs to be loaded into physical memory, and there is no free space, these algorithms determine which existing page to replace.
If no page frame is free, the virtual memory manager performs a page replacement operation to replace one of the pages existing in memory with the page whose reference caused the page fault. It is performed as follows: The virtual memory manager uses a page replacement algorithm to select one of the pages currently in memory for replacement, accesses the page table entry of the selected page to mark it as “not present” in memory, and initiates a page-out operation for it if the modified bit of its page table entry indicates that it is a dirty page.
Common Page Replacement Techniques
- First In First Out (FIFO)
- Optimal Page replacement
- Least Recently Used (LRU)
- Most Recently Used (MRU)
First In First Out (FIFO)
This is the simplest page replacement algorithm. In this algorithm, the operating system keeps track of all pages in the memory in a queue, the oldest page is in the front of the queue. When a page needs to be replaced page in the front of the queue is selected for removal.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3-page frames. Find the number of page faults using FIFO Page Replacement Algorithm.
FIFO - Page Replacement Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots ---> 3 Page Faults.
when 3 comes, it is already in memory so ---> 0 Page Faults. Then 5 comes, it is not available in memory, so it replaces the oldest page slot i.e 1. ---> 1 Page Fault. 6 comes, it is also not available in memory, so it replaces the oldest page slot i.e 3 ---> 1 Page Fault. Finally, when 3 come it is not available, so it replaces 0 1-page fault.
Implementation of FIFO Page Replacement Algorithm
Optimal Page Replacement
In this algorithm, pages are replaced which would not be used for the longest duration of time in the future.
Example: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4-page frame. Find number of page fault using Optimal Page Replacement Algorithm.
Optimal Page Replacement Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots ---> 4 Page faults
0 is already there so ---> 0 Page fault. when 3 came it will take the place of 7 because it is not used for the longest duration of time in the future---> 1 Page fault. 0 is already there so ---> 0 Page fault. 4 will takes place of 1 ---> 1 Page Fault.
Now for the further page reference string ---> 0 Page fault because they are already available in the memory. Optimal page replacement is perfect, but not possible in practice as the operating system cannot know future requests. The use of Optimal Page replacement is to set up a benchmark so that other replacement algorithms can be analyzed against it.
Least Recently Used
In this algorithm, page will be replaced which is least recently used.
Example Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4-page frames. Find number of page faults using LRU Page Replacement Algorithm.
Least Recently Used - Page Replacement Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots ---> 4 Page faults
0 is already there so ---> 0 Page fault. when 3 came it will take the place of 7 because it is least recently used ---> 1 Page fault
0 is already in memory so ---> 0 Page fault.
4 will takes place of 1 ---> 1 Page Fault
Now for the further page reference string ---> 0 Page fault because they are already available in the memory.
Implementation of LRU Page Replacement Algorithm
Most Recently Used (MRU)
In this algorithm, page will be replaced which has been used recently. Belady's anomaly can occur in this algorithm.
Example 4: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4-page frames. Find number of page faults using MRU Page Replacement Algorithm.
Most Recently Used - Page Replacement Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots ---> 4 Page faults
0 is already their so--> 0 page fault
when 3 comes it will take place of 0 because it is most recently used ---> 1 Page fault
when 0 comes it will take place of 3 ---> 1 Page fault
when 4 comes it will take place of 0 ---> 1 Page fault
2 is already in memory so ---> 0 Page fault
when 3 comes it will take place of 2 ---> 1 Page fault
when 0 comes it will take place of 3 ---> 1 Page fault
when 3 comes it will take place of 0 ---> 1 Page fault
when 2 comes it will take place of 3 ---> 1 Page fault
when 3 comes it will take place of 2 ---> 1 Page fault
Conclusion
In summary, page replacement algorithms are essential for managing a computer's memory efficiently. They help ensure that the system runs smoothly by reducing the number of times it needs to fetch data from slower storage. Different algorithms, like FIFO and LRU, have their own pros and cons, and choosing the right one depends on the specific needs of the system. Understanding these algorithms can help improve system performance and make our computers faster and more efficient.
GATE CS Corner Questions
Practicing the following questions will help you test your knowledge. All questions have been asked in GATE in previous years or in GATE Mock Tests. It is highly recommended that you practice them.
- Memory Management | Question 1
- Memory Management | Question 10
- GATE CS 2014 (Set-1), Question 65
- GATE CS 2012, Question 40
- GATE CS 2007, Question 56
- GATE CS 2007, Question 82
- GATE CS 2007, Question 83
- GATE CS 2014 (Set-3), Question 65
- GATE CS 2002 Question 23
- GATE CS 2001, Question 21
Similar Reads
Operating System Tutorial An Operating System(OS) is a software that manages and handles hardware and software resources of a computing device. Responsible for managing and controlling all the activities and sharing of computer resources among different running applications.A low-level Software that includes all the basic fu
4 min read
OS Basics
Process & Threads
CPU Scheduling
Deadlock
Memory & Disk Management
Memory Management in Operating SystemThe term memory can be defined as a collection of data in a specific format. It is used to store instructions and process data. The memory comprises a large array or group of words or bytes, each with its own location. The primary purpose of a computer system is to execute programs. These programs,
10 min read
Fixed (or static) Partitioning in Operating SystemFixed partitioning, also known as static partitioning, is one of the earliest memory management techniques used in operating systems. In this method, the main memory is divided into a fixed number of partitions at system startup, and each partition is allocated to a process. These partitions remain
8 min read
Variable (or Dynamic) Partitioning in Operating SystemIn operating systems, Memory Management is the function responsible for allocating and managing a computerâs main memory. The memory Management function keeps track of the status of each memory location, either allocated or free to ensure effective and efficient use of Primary Memory. Below are Memo
4 min read
Paging in Operating SystemPaging is the process of moving parts of a program, called pages, from secondary storage (like a hard drive) into the main memory (RAM). The main idea behind paging is to break a program into smaller fixed-size blocks called pages.To keep track of where each page is stored in memory, the operating s
8 min read
Segmentation in Operating SystemA process is divided into Segments. The chunks that a program is divided into which are not necessarily all of the exact sizes are called segments. Segmentation gives the user's view of the process which paging does not provide. Here the user's view is mapped to physical memory. Types of Segmentatio
4 min read
Segmentation in Operating SystemA process is divided into Segments. The chunks that a program is divided into which are not necessarily all of the exact sizes are called segments. Segmentation gives the user's view of the process which paging does not provide. Here the user's view is mapped to physical memory. Types of Segmentatio
4 min read
Page Replacement Algorithms in Operating SystemsIn an operating system that uses paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when a new page comes in. Page replacement becomes necessary when a page fault occurs and no free page frames are in memory. in this article, we will discus
7 min read
File Systems in Operating SystemA computer file is defined as a medium used for saving and managing data in the computer system. The data stored in the computer system is completely in digital format, although there can be various types of files that help us to store the data.File systems are a crucial part of any operating system
8 min read
File Systems in Operating SystemA computer file is defined as a medium used for saving and managing data in the computer system. The data stored in the computer system is completely in digital format, although there can be various types of files that help us to store the data.File systems are a crucial part of any operating system
8 min read
Advanced OS
Practice