Table of contents
1.
Introduction
2.
Page Fault
3.
FIFO
4.
How FIFO Works
5.
Implementation in C
5.1.
C
6.
Implementation
6.1.
Step-by-Step Implementation
6.1.1.
Initialize Memory Structure
6.1.2.
Simulate Page Requests
6.1.3.
Process Each Page Request
6.1.4.
Track Page Faults
6.2.
Example Code
6.3.
C
7.
Frequently Asked Questions
7.1.
What is FIFO in C programming?
7.2.
What is FIFO policy for page replacement?
7.3.
How do you use FIFO page replacement algorithm?
8.
Conclusion
Last Updated: Aug 13, 2025
Easy

Fifo Page Replacement Algorithm in C

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The FIFO (First-In, First-Out) page replacement algorithm is a simple method used by operating systems to manage memory when there are more pages than available frames. It works by replacing the page that has been in memory the longest when a new page needs to be loaded. 

Fifo Page Replacement Algorithm in C

In this article, we will learn what the FIFO page replacement algorithm is, how it works, & see an example implementation in C. 

Page Fault

Page fault is an event that occurs when a software program attempts to access a page of memory that is not currently loaded into the system's main memory, or RAM. This situation is critical because it interrupts the normal flow of execution as the operating system must take specific actions to handle the missing data.

When using the FIFO page replacement algorithm, a page fault triggers the operating system to review the pages in RAM. If the required page is not present, the operating system must retrieve it from a secondary storage device, such as a hard drive or SSD, which is significantly slower than RAM. This process involves several steps:

  1. Identifying the Missing Page: The system checks its page table (a data structure used to store the mapping between virtual and physical memory addresses) to determine if the requested page is loaded in RAM.
     
  2. Page Replacement Decision: If the memory is full and cannot accommodate the new page without removing an existing one, the FIFO algorithm comes into play. FIFO treats memory pages like a queue, removing the oldest page first — the one that was loaded into memory earliest.
     
  3. Swapping Pages: The chosen page (the oldest) is then swapped out of RAM and replaced with the newly needed page. If the old page was modified while in RAM (known as a "dirty" page), it must be written back to storage, adding to the time cost.
     
  4. Updating Tables: After swapping, the system updates its page table to reflect the changes, marking the new page's presence in RAM and adjusting any relevant management data.
     

Note -: The simplicity of the FIFO algorithm makes it easy to implement and understand; however, it can lead to suboptimal performance in scenarios where older pages are still heavily used. This inefficiency is known as the "Belady's Anomaly," where increasing the number of page frames might actually lead to more page faults.

FIFO

The First In, First Out (FIFO) algorithm is a classic method used by operating systems to manage the pages in memory when there isn't enough space to keep all the needed pages in RAM at once. It is one of the simplest forms of page replacement algorithms and operates exactly as its name suggests: the first page loaded into memory is also the first to be removed when space is needed.

How FIFO Works

To understand how FIFO functions, imagine memory as a limited set of slots where each slot can hold one page. When a program starts, these slots are empty, and pages are loaded into them as the program accesses different parts of its code or data. Here’s what happens step-by-step in a system using FIFO:

  • Loading Pages: As the program runs, it loads new pages into the available empty slots in memory.
     
  • Checking Full Memory: If a page fault occurs and all memory slots are full, the FIFO algorithm looks at the page that has been in memory the longest.
     
  • Replacing the Oldest Page: This oldest page is then removed to make space for the new page that the program needs to access.

Implementation in C

Implementing FIFO in C requires managing a queue where the index of the pages in memory is stored. Here's a basic structure of how you might write this:

  • C

C

#include <stdio.h>
#include <stdlib.h>

#define MAX_FRAMES 3 // Number of frames in memory

int frames[MAX_FRAMES];
int front = 0; // Start of the queue
int rear = -1; // End of the queue

void insert(int page) {
if (rear == MAX_FRAMES - 1) {
// Memory is full, remove the first page
for (int i = 0; i < rear; i++) {
frames[i] = frames[i + 1];
}
frames[rear] = page; // Add new page at the end
} else {
// Add new page to the queue
frames[++rear] = page;
}
}

void display() {
for (int i = 0; i <= rear; i++) {
printf("%d ", frames[i]);
}
printf("\n");
}

int main() {
// Example usage
insert(1);
insert(2);
insert(3);
display(); // Output: 1 2 3
insert(4);
display(); // Output: 2 3 4 (1 is replaced)
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

1 2 3 
2 3 4 


This simple program illustrates how FIFO manages memory. When the memory reaches its limit, the oldest page is replaced by the new page. The queue maintains the order of pages just as they were inserted.

Implementation

Implementing the FIFO page replacement algorithm in C programming involves creating a straightforward simulation that mimics how an operating system might handle memory pages when only a limited number of them can be kept in RAM. The goal is to show how the FIFO algorithm decides which page to evict when a new page needs to be loaded but memory is full.

Step-by-Step Implementation

The following steps outline the process to simulate the FIFO page replacement algorithm in a clear and concise way, using basic C programming techniques:

Initialize Memory Structure

We start by setting up an array to represent the frames available in memory. Each element of the array will hold a page number.

Simulate Page Requests

As the program runs, it simulates requests for pages. These requests are represented by an array of integers where each integer is a page number that the program needs to access.

Process Each Page Request

For each page requested, the program checks if the page is already in one of the memory frames.

  • If the page is already in memory, it's a 'hit', and no page replacement is needed.
     
  • If the page is not in memory and there is an empty frame, the page is loaded into the free frame.
     
  • If there are no free frames (i.e., memory is full), the algorithm removes the page that was loaded first (the one at the front of the queue) and loads the new page into that frame.

Track Page Faults

A 'page fault' occurs whenever a page needs to be loaded into memory from disk (either into an empty frame or replacing an existing page). The simulation keeps a count of these page faults to measure the effectiveness of the FIFO strategy.

Example Code

  • C

C

#include <stdio.h>

#define FRAME_SIZE 3

int frames[FRAME_SIZE];

int front = 0;

int rear = 0;

int pageFaults = 0;

void displayFrames() {

   for (int i = 0; i < FRAME_SIZE; i++) {

       printf("%d ", frames[i]);

   }

   printf("\n");

}

void insertPage(int page) {

   int i;

   for (i = 0; i < rear; i++) {

       if (frames[i] == page) {

           printf("Page %d already in frame.\n", page);

           displayFrames();

           return;

       }

   }

   if (rear < FRAME_SIZE) {

       frames[rear++] = page;

   } else {

       for (i = 0; i < FRAME_SIZE - 1; i++) {

           frames[i] = frames[i + 1];

       }

       frames[i] = page;

   }

   pageFaults++;

   printf("Page %d inserted:\n", page);

   displayFrames();

}

int main() {

   int requests[] = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};

   int n = sizeof(requests) / sizeof(requests[0]);

   for (int i = 0; i < n; i++) {

       insertPage(requests[i]);

   }

   printf("Total page faults: %d\n", pageFaults);

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output

Page 1 inserted:
1 0 0 
Page 2 inserted:
1 2 0 
Page 3 inserted:
1 2 3 
Page 4 inserted:
2 3 4 
Page 1 inserted:
3 4 1 
Page 2 inserted:
4 1 2 
Page 5 inserted:
1 2 5 
Page 1 already in frame.
1 2 5 
Page 2 already in frame.
1 2 5 
Page 3 inserted:
2 5 3 
Page 4 inserted:
5 3 4 
Page 5 already in frame.
5 3 4 
Total page faults: 9


This program efficiently demonstrates how FIFO handles page requests & manages memory. It includes functions to insert pages into frames and display the current state of memory, making it easy to track what's happening at each step.

Frequently Asked Questions

What is FIFO in C programming?

FIFO (First In, First Out) is a queue data structure in C where elements are processed in the order they were added, removing the oldest first.

What is FIFO policy for page replacement?

FIFO page replacement algorithm swaps out the oldest page in memory when a new page needs to be loaded and memory is full.

How do you use FIFO page replacement algorithm?

In FIFO, maintain a queue to track pages in memory. Replace the oldest page (front of the queue) when a new page is referenced.

Conclusion

In this article, we have learned about the FIFO page replacement algorithm and its implementation in C programming. We discussed how FIFO handles memory pages, its simplicity, and scenarios where it may not be the most efficient due to Belady’s Anomaly. The example provided a clear demonstration of how FIFO processes page requests and manages memory in a limited frame environment, along with tracking page faults to assess its performance. 

You can refer to our guided paths on the Code360. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass