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

Prerequisite – Least Recently Used (LRU) Page Replacement algorithm

Least 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, 8Output :When the number of frames is : 3 The number of page faults are : 9Reference array is : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 0, 0Output :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 faults ` `void` `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` `); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The number of page faults is : 10

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Not Recently Used (NRU) page replacement algorithm
- Program for Least Recently Used (LRU) Page Replacement algorithm
- Optimal Page Replacement Algorithm
- Belady's Anomaly in Page Replacement Algorithms
- Second Chance (or Clock) Page Replacement Policy
- Program for Page Replacement Algorithms | Set 2 (FIFO)
- Page Replacement Algorithms in Operating Systems
- Advantages and Disadvantages of various Page Replacement algorithms
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Peterson's Algorithm for Mutual Exclusion | Set 1 (Basic C implementation)
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Minimize Cost with Replacement with other allowed
- Page Table Entries in Page Table
- Program for Banker's Algorithm | Set 1 (Safety Algorithm)
- Caching Page Tables
- Inverted Page Table in Operating System
- Page Fault Handling in Operating System
- Hashed Page Tables in Operating System
- Least Frequently Used (LFU) Cache Implementation
- Implementation of Access Matrix in Distributed OS

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.