Implementation of Least Recently Used (LRU) page replacement algorithm using Counters - GeeksforGeeks

02 Dec, 2019
Prerequisite – Least Recently Used (LRU) Page Replacement algorithmLeast Recently Used page replacement algorithm replaces the page which is not used recently.
Implementation:In this article, LRU is implemented using counters, a ctime (i.e., counter) variable is used to represent the current time, it is incremented for every page of the reference array. A vector of pair is used to represent the page frames, the 1’st variable of the pair is the page number and the second variable is the current time. When a new page is to be inserted and the frames are full, the page with minimum ctime is deleted (as it is the least recently used page). If the page is accessed again the value of ctime is updated.
Note:Least ctime (counter / current time) value represents the least recently used page.
Examples:
Reference array is : 0, 0, 0, 2, 3, 0, 5, 7, 1, 2, 0, 8
Output :
When the number of frames is : 3
The number of page faults are : 9

Reference array is : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 0, 0
Output :
When the number of frames is : 3
The number of page faults are : 15 
Code:
#include <bits/stdc++.h>  using namespace std;  // To calculate the number of page faultsvoid pageFaults(int frame_size, int* ref, int len, int ctime){    // Count variable to count the    // number of page faults    int cnt = 0;    // Arr to simulate frames    vector<pair<int, int> > arr;      // To initialise the array    for (int i = 0; i < frame_size; i++) {        arr.push_back(make_pair(-1, ctime));    }      int page;      for (int i = 0; i < len; i++) {        page = ref[i];        auto it = arr.begin();          for (it = arr.begin(); it != arr.end(); it++) {            if (it->first == page) {                break;            }        }          // If page is found        if (it != arr.end()) {            // update the value of            // current time            it->second = ctime;        }          // If page is not found        else {            // Find the page with min value of ctime,            // as it will be the least recently used            vector<pair<int, int> >::iterator pos;            pos = arr.begin();            int min = pos->second;            for (auto itr = arr.begin(); itr != arr.end(); itr++) {                if (itr->second < min) {                    pos = itr;                    min = pos->second;                }            }              // Erase this page from the frame vector            arr.erase(pos);              // Insert the new page            arr.push_back(make_pair(page, ctime));            cnt++;        }        ctime++;    }    cout << "The number of page faults is : " << cnt << endl;}  int main(){    // This is the reference array    int ref[] = { 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };    int len = sizeof(ref) / sizeof(ref[0]);    int frame_size = 3;    // Ctime represents current time,    // it is incremented for every page    int ctime = 0;    pageFaults(frame_size, ref, len, ctime);}
The number of page faults is : 10

Operating Systems-Memory Management
Greedy
Operating Systems
Operating Systems
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Job Sequencing Problem
Difference between Prim's and Kruskal's algorithm for MST
Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
Shortest Remaining Time First (Preemptive SJF) Scheduling Algorithm
Banker's Algorithm in Operating System
Types of Operating Systems
Program for FCFS CPU Scheduling | Set 1
Paging in Operating System
Program for Round Robin scheduling | Set 1