Related Articles
Implementation of Least Recently Used (LRU) page replacement algorithm using Counters
• Last Updated : 02 Dec, 2019

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, 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 `` ` `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 > 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 >::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);``    ``int` `frame_size = 3;``    ``// Ctime represents current time,``    ``// it is incremented for every page``    ``int` `ctime` `= 0;``    ``pageFaults(frame_size, ref, len, ``ctime``);``}`
Output:
```The number of page faults is : 10
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up