Skip to content
Related Articles

Related Articles

Page Faults in LRU | Implementation

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 14 Nov, 2022
Improve Article
Save Article

LRU uses the concept of paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when the new page comes in. Whenever a new page is referred to and is not present in memory, the page fault occurs and the Operating System replaces one of the existing pages with a newly needed page. LRU is one such page replacement policy in which the least recently used pages are replaced.

For example:
Given a sequence of pages in an array of pages[] of length N and memory capacity C, find the number of page faults using the Least Recently Used (LRU) Algorithm.

Example-1 :

Input : N = 7, C = 3
pages = {1, 2, 1, 4, 2, 3, 5}
Output : 5

Explanation :

Capacity is 3, thus, we can store maximum 3 pages at a time.
Page 1 is required, since it is not present, 
it is a page fault: page fault = 1

Page 2 is required, since it is not present, 
it is a page fault: page fault = 1 + 1 = 2

Page 1 is required, since it is present, 
it is not a page fault: page fault = 2 + 0 = 2

Page 4 is required, since it is not present, 
it is a page fault: page fault = 2 + 1 = 3

Page 2 is required, since it is present, 
it is not a page fault: page fault = 3 + 0 = 3

Page 3 is required, since it is not present, 
it replaces LRU page 2: page fault = 3 + 1 = 4

Page 5 is required, since it is not present, 
it replaces LRU page 1: page fault = 4 + 1 = 5

Example-2 :

Input : N = 9, C = 4
Pages = {5, 0, 1, 3, 2, 4, 1, 0, 5}
Output : 8

Explanation :

Capacity is 4, thus, we can store maximum 4 pages at a time.
Page 5 is required, since it is not present, 
it is a page fault: page fault = 1

Page 0 is required, since it is not present, 
it is a page fault: page fault = 1 + 1 = 2

Page 1 is required, since it is not present, 
it is a page fault: page fault = 2 + 1 = 3

Page 3 is required, since it is not present, 
it is a page fault: page fault = 3 + 1 = 4

Page 2 is required, since it is not present, 
it replaces LRU page 5: page fault = 4 + 1 = 5

Page 4 is required, since it is not present, 
it replaces LRU page 0: page fault = 5 + 1 = 6

Page 1 is required, since it is present, 
it is not a page fault: page fault = 6 + 0 = 6

Page 0 is required, since it is not present, 
it replaces LRU page 3: page fault = 6 + 1 = 7

Page 5 is required, since it is not present, 
it replaces LRU page 2: page fault = 7 + 1 = 8

Algorithm :

step-1 : Initialize count as 0.
step-2 : Create a vector / array of size equal to memory capacity.
step-3 : Traverse elements of pages[]
step-4 : In each traversal:
        if(element is present in memory):
            remove the element and push the element at the end  
        else:
            if(memory is full) remove the first element
            Increment count 
            push the element at the end       

Implementation of the algorithm :

Following is the implementation of the algorithm in C++ as follows.

C++




// C++ program to illustrate
// page faults in LRU
 
#include <bits/stdc++.h>
using namespace std;
 
/* Counts no. of page faults */
int pageFaults(int n, int c, int pages[])
{
    // Initialise count to 0
    int count = 0;
 
    // To store elements in memory of size c
    vector<int> v;
    int i;
    for (i = 0; i <= n - 1; i++) {
 
        // Find if element is present in memory or not
        auto it = find(v.begin(), v.end(), pages[i]);
 
        // If element is not present
        if (it == v.end()) {
 
            // If memory is full
            if (v.size() == c) {
               
                // Remove the first element
                // As it is least recently used
                v.erase(v.begin());
            }
 
            // Add the recent element into memory
            v.push_back(pages[i]);
 
            // Increment the count
            count++;
        }
        else {
 
            // If element is present
            // Remove the element
            // And add it at the end as it is
            // the most recent element
            v.erase(it);
            v.push_back(pages[i]);
        }
    }
 
    // Return total page faults
    return count;
}
 
/* Driver program to test pageFaults function*/
int main()
{
 
    int pages[] = { 1, 2, 1, 4, 2, 3, 5 };
    int n = 7, c = 3;
 
    cout << "Page Faults = " << pageFaults(n, c, pages);
    return 0;
}
 
// This code is contributed by rajsanghavi9.

Java




// Java program to illustrate
// page faults in LRU
import java.util.*;
class GFG {
  public static void main(String[] args)
  {
    int pages[] = new int[] { 1, 2, 1, 4, 2, 3, 5 };
    int n = 7, c = 3;
 
    System.out.println("Page Faults = "
                       + pageFaults(n, c, pages));
  }
  static int pageFaults(int N, int C, int pages[])
  {
    // a queue to maintain page frames
    Queue<Integer> q = new LinkedList<>();
 
    // starting with 0 page faults
    int i = 0, c = 0;
    while (i < N)
    {
 
      // if queue is empty or the current page is
      // absent in the page frame then page fault
      // occurs
      if (q.isEmpty() || !q.contains(pages[i]))
      {
        if (q.size()== C) // if frame is full we remove the
          // least recently used page
          q.poll(); // removing the front element
        // which is not replaced for
        // long time
 
        q.add(pages[i]); // now we add the page to
        // frame
        c++; // incrementing page faults
      }
      else
      {
        q.remove(pages[i]); // if the page already exists
        // we'll remove it from
        // previous position and add
        // it to the end of the queue
        q.add(pages[i]);
      }
 
      i++;
    }
    return c;
  }
}
// This code is contributed by tharunmadishetti1.

Output :

Page Faults = 5

Time Complexity: O(N*C)

Auxiliary Space:  O(C)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!