FIFO Page Replacement Algorithm
Last Updated :
08 Jul, 2025
Page Replacement Algorithms are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newly needed page. Different page replacement algorithms suggest different ways to decide which page to replace. The target for all algorithms is to reduce number of page faults.
First In First Out (FIFO) page replacement algorithm -
This is the simplest page replacement algorithm. In this algorithm, operating system keeps track of all pages in the memory in a queue, 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 and 3 page slots. 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. —>1Page Fault.
Finally 6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>6 Page Fault.
So total page faults = 5.
Example -2. Consider the following reference string: 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1. Using FIFO page replacement algorithm -

So, total number of page faults = 9. Given memory capacity (as number of pages it can hold) and a string representing pages to be referred, write a function to find number of page faults.
Implementation - Let capacity be the number of pages that memory can hold. Let set be the current set of pages in memory.
1- Start traversing the pages.
i) If set holds less pages than capacity.
a) Insert page into the set one by one until
the size of set reaches capacity or all
page requests are processed.
b) Simultaneously maintain the pages in the
queue to perform FIFO.
c) Increment page fault
ii) Else
If current page is present in set, do nothing.
Else
a) Remove the first page from the queue
as it was the first to be entered in
the memory
b) Replace the first page in the queue with
the current page in the string.
c) Store current page in the queue.
d) Increment page faults.
2. Return page faults.
Implementation:
C++
// C++ implementation of FIFO page replacement
// in Operating Systems.
#include<bits/stdc++.h>
using namespace std;
// Function to find page faults using FIFO
int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
unordered_set<int> s;
// To store the pages in FIFO manner
queue<int> indexes;
// Start from initial page
int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (s.find(pages[i])==s.end())
{
// Insert the current page into the set
s.insert(pages[i]);
// increment page fault
page_faults++;
// Push the current page into the queue
indexes.push(pages[i]);
}
}
// If the set is full then need to perform FIFO
// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (s.find(pages[i]) == s.end())
{
// Store the first page in the
// queue to be used to find and
// erase the page from the set
int val = indexes.front();
// Pop the first page from the queue
indexes.pop();
// Remove the indexes page from the set
s.erase(val);
// insert the current page in the set
s.insert(pages[i]);
// push the current page into
// the queue
indexes.push(pages[i]);
// Increment page faults
page_faults++;
}
}
}
return page_faults;
}
// Driver code
int main()
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int n = sizeof(pages)/sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}
Java
// Java implementation of FIFO page replacement
// in Operating Systems.
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
class Test
{
// Method to find page faults using FIFO
static int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
HashSet<Integer> s = new HashSet<>(capacity);
// To store the pages in FIFO manner
Queue<Integer> indexes = new LinkedList<>() ;
// Start from initial page
int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (!s.contains(pages[i]))
{
s.add(pages[i]);
// increment page fault
page_faults++;
// Push the current page into the queue
indexes.add(pages[i]);
}
}
// If the set is full then need to perform FIFO
// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (!s.contains(pages[i]))
{
//Pop the first page from the queue
int val = indexes.peek();
indexes.poll();
// Remove the indexes page
s.remove(val);
// insert the current page
s.add(pages[i]);
// push the current page into
// the queue
indexes.add(pages[i]);
// Increment page faults
page_faults++;
}
}
}
return page_faults;
}
// Driver method
public static void main(String args[])
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int capacity = 4;
System.out.println(pageFaults(pages, pages.length, capacity));
}
}
// This code is contributed by Gaurav Miglani
Python3
# Python3 implementation of FIFO page
# replacement in Operating Systems.
from queue import Queue
# Function to find page faults using FIFO
def pageFaults(pages, n, capacity):
# To represent set of current pages.
# We use an unordered_set so that we
# quickly check if a page is present
# in set or not
s = set()
# To store the pages in FIFO manner
indexes = Queue()
# Start from initial page
page_faults = 0
for i in range(n):
# Check if the set can hold
# more pages
if (len(s) < capacity):
# Insert it into set if not present
# already which represents page fault
if (pages[i] not in s):
s.add(pages[i])
# increment page fault
page_faults += 1
# Push the current page into
# the queue
indexes.put(pages[i])
# If the set is full then need to perform FIFO
# i.e. remove the first page of the queue from
# set and queue both and insert the current page
else:
# Check if current page is not
# already present in the set
if (pages[i] not in s):
# Pop the first page from the queue
val = indexes.queue[0]
indexes.get()
# Remove the indexes page
s.remove(val)
# insert the current page
s.add(pages[i])
# push the current page into
# the queue
indexes.put(pages[i])
# Increment page faults
page_faults += 1
return page_faults
# Driver code
if __name__ == '__main__':
pages = [7, 0, 1, 2, 0, 3, 0,
4, 2, 3, 0, 3, 2]
n = len(pages)
capacity = 4
print(pageFaults(pages, n, capacity))
# This code is contributed by PranchalK
C#
// C# implementation of FIFO page replacement
// in Operating Systems.
using System;
using System.Collections;
using System.Collections.Generic;
class Test
{
// Method to find page faults using FIFO
static int pageFaults(int []pages, int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
HashSet<int> s = new HashSet<int>(capacity);
// To store the pages in FIFO manner
Queue indexes = new Queue() ;
// Start from initial page
int page_faults = 0;
for (int i = 0; i < n; i++)
{
// Check if the set can hold more pages
if (s.Count < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (!s.Contains(pages[i]))
{
s.Add(pages[i]);
// increment page fault
page_faults++;
// Push the current page into the queue
indexes.Enqueue(pages[i]);
}
}
// If the set is full then need to perform FIFO
// i.e. Remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (!s.Contains(pages[i]))
{
//Pop the first page from the queue
int val = (int)indexes.Peek();
indexes.Dequeue();
// Remove the indexes page
s.Remove(val);
// insert the current page
s.Add(pages[i]);
// push the current page into
// the queue
indexes.Enqueue(pages[i]);
// Increment page faults
page_faults++;
}
}
}
return page_faults;
}
// Driver method
public static void Main(String []args)
{
int []pages = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int capacity = 4;
Console.Write(pageFaults(pages, pages.Length, capacity));
}
}
// This code is contributed by Arnab Kundu
JavaScript
<script>
// JavaScript code for the above approach
// Method to find page faults using FIFO
function pageFaults(pages, n, capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
let s = new Set();
// To store the pages in FIFO manner
var indexes = [];
// Start from initial page
let page_faults = 0;
for (let i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (!s.has(pages[i]))
{
s.add(pages[i]);
// increment page fault
page_faults++;
// Push the current page into the queue
indexes.push(pages[i]);
}
}
// If the set is full then need to perform FIFO
// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (!s.has(pages[i]))
{
//Pop the first page from the queue
let val = indexes[0];
indexes.shift();
// Remove the indexes page
s.delete(val);
// insert the current page
s.add(pages[i]);
// push the current page into
// the queue
indexes.push(pages[i]);
// Increment page faults
page_faults++;
}
}
}
return page_faults;
}
// Driver Code
let pages = [7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2];
let capacity = 4;
document.write(pageFaults(pages, pages.length, capacity));
// This code is contributed by sanjoy_62.
</script>
Note - We can also find the number of page hits. Just have to maintain a separate count. If the current page is already in the memory then that must be count as Page-hit.
Time Complexity: O(n), where n is the number of pages.
Space Complexity: O(capacity)
Belady’s anomaly:
Belady’s anomaly proves that it is possible to have more page faults when increasing the number of page frames while using the First in First Out (FIFO) page replacement algorithm. For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots, we get 9 total page faults, but if we increase slots to 4, we get 10 page faults.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem