# Page Faults in LRU

Paging is a memory management process related the operating systems. It stores or retrieve some process data from the secondary data storage into the primary data storage or memory by using the page segement. The paging process happens when the process encounters any fault in a page and we can not use a new free page to satisfy the allocation process here. The LRU process generates the particular need of a replacement algorithm. It decides which page needs to be replace when a process produce a new page. Let us take an example –

Input taken for the process −

N = 9, C = 4


Present pages for the process = {5, 0, 1, 3, 2, 4, 1, 0, 5}

Output will be: 8

Explaination −

Memory allocated with the 4 pages are here 5, 0, 1, 3

The fault happening in this process = 4

Memory allocated with the value 2 is required, replaces LRU 5:

The fault happening in this process = 4+1 = 5

Memory allocated with the value 4 is required, replaces LRU 0:

The fault happening in this process = 5 + 1 = 6

Memory allocated with the value 1 is required which is already present:

The fault happening in this process = 6 + 0 = 6

Memory allocated with the value 0 is required which replaces LRU 3:

The fault happening in this process = 6 + 1 = 7

Memory allocated with the value 5 is required which replaces LRU 2:

The fault happening in this process = 7 + 1 = 8.

## Algorithm to evaluate the page faults in LRU

The LRU algorithm is a replacement process mentioned here in the field of operating system. The capacity is the number of holding pages in a memory. Now we will set the current set of pages in that particular memory. This process always least frequently used pages present in the value of the process.

• Step 1 − Start the process of LRU operation.

• Step 2 − Declare here the total count as 0.

• Step 3 − Create a vector class.

• Step 4 − Construct and declare an array with the desired array size.

• Step 5 − Start the process with the size of memory capacity.

• Step 6 − Create a map for the method.

• Step 7 − Store the frequency value to the map of the pages.

• Step 8 − Traverse page elements.

• Step 9 − If; the desired element here present in the basic storage location, then we

• will remove it and push it.

• Step 10 − Step 9 increase the process of the frequency.

• Step 11 − Else; the memory is totally full. Remove first element and decrease the frequency.

• Step 12 − Count increase.

• Step 13 − Compare the frequency result.

• Step 14 − Sort the pages as per their frequency and time based result.

• Step 15 − If we get the same frequency, then the page will arrive at first.

• Step 16 − Repeat the process.

• Step 17 − Return the result.

• Step 18 − Terminate the process.

### Syntax to evaluate the page faults in LRU

int main() {
int capacity = 4;
int arr[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};

deque<int> q(capacity);
int count=0;
int page_faults=0;
deque<int>::iterator itr;
q.clear();
for(int i:arr)
{
itr = find(q.begin(),q.end(),i);
if(!(itr != q.end()))
{

++page_faults;
if(q.size() == capacity)
{
q.erase(q.begin());
q.push_back(i);
}
else{
q.push_back(i);

}
}
else
{
q.erase(itr);
q.push_back(i);
}

}
cout<<page_faults;
}
{
int capacity = 4;
int arr[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
ArrayList<Integer> s=new ArrayList<>(capacity);
int count=0;
int page_faults=0;
for(int i:arr)
{
if(!s.contains(i))
{
if(s.size()==capacity)
{
s.remove(0);
}
else
page_faults++;
++count;
} else {
s.remove((Object)i);
}
}


In this possible syntax mentioned above, we have tried to show you the possible implementation of a LRU page fault management in the field of operating system. With this intersected syntax we are going to build some C++ codes to explain and solve the problem statement in an efficient manner.

### Approach

• Approach 1 − C++ program to demonstrate the Least Recently Used (LRU) Algorithm attached with the paging for memory management in Operating System.

• Approach 2 − C++ program to find page faults by using indexes with LRU algorithm attached with the paging for memory management in Operating System using hashing function.

## C++ program to demonstrate the Least Recently Used (LRU) Algorithm attached with the paging for memory management in Operating System

The LRU aka Least Recently Used algorithm is a strategy which handles page faults in an operating system. Here is the process -

• Start to traverse the pages.

• Insert the data into a a set.

• Request the page process.

• Maintain the simultaneous occurance.

• Declare index.

• Increment started for the page fault.

• Find the page in set.

• Replace the found page with current.

• Increment faults.

• Update index

### Example Code 1

//C++ program to demonstrate the Least Recently Used (LRU) Algorithm attached with the paging for memory management in Operating System
#include<iostream>
using namespace std;
int main ()
{
int nopages, nofaults, page, i, count = 0;
cout << "\n\t Enter no of pages for which you want to calculate page faults:>";
cin >> nopages;
//it will store the numer of Pages
cout << "\n\t Enter the Reference String:";
for (i = 0; i < nopages; i++)

{
cout << "\t"; cin >> page[i];
}
cout << "\n\t Enter the Number of frames:"; cin >> nofaults;
int frame[nofaults], fcount[nofaults];
for (i = 0; i < nofaults; i++)

{
frame[i] = -1;
fcount[i] = 0;
}
i = 0;
while (i < nopages)

{
int j = 0, flag = 0;
while (j < nofaults)

{
if (page[i] == frame[j])
{
flag = 1;
fcount[j] = i + 1;
}
j++;
}
j = 0;
cout << "\n\t***\n";
cout << "\t" << page[i] << "-->";
if (flag == 0)

{
int min = 0, k = 0;
while (k < nofaults - 1) { if (fcount[min] > fcount[k + 1])
min = k + 1;
k++;
}
frame[min] = page[i];
fcount[min] = i + 1;
count++;
while (j < nofaults)

{
cout << "\t|" << frame[j] << "|";
j++;
}
}
i++;
}
cout << "\n\t***\n";
cout << "\n\t Page Fault:" << count;
return 0;
}


### Output

 Enter no of pages for which you want to calculate page faults:>
Enter the Reference String:
Enter the Number of frames:
***

Page Fault:0


## C++ program to find page faults by using indexes with LRU algorithm attached with the paging for memory management in Operating System using hashing function

In a paging tracking process, the page fault occurs when a code tries to access a memory aka page which is not present or listed in the RAM at all. To explain this process we are going to follow these steps mentioned below

• Iterate the process and reference pages.

• Remove current.

• Increment page fault.

• Append current into pages.

• Remove first from a page.

• Use the hash string.

• Return the page hit as a number

### Example Code 2

//C++ program to find page faults by using indexes with LRU algorithm attached with the paging for memory management in Operating System using hashing function
#include<bits/stdc++.h>
using namespace std;
int pageFaults(int pages[], int n, int capacity)
{
unordered_set<int> s;
unordered_map<int, int> indexes;
int page_faults = 0;
for (int i=0; i<n; i++)
{
if (s.size() < capacity)
{
if (s.find(pages[i])==s.end())
{
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
}
else
{
if (s.find(pages[i]) == s.end())
{
int lru = INT_MAX, val;
for (auto it=s.begin(); it!=s.end(); it++)
{
if (indexes[*it] < lru)
{
lru = indexes[*it];
val = *it;
}
}
s.erase(val);
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
}
}

return page_faults;
}
int main()
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages)/sizeof(pages);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}


### Output

6


## Conclusion

The LRU replacement algorithm is a particular page that we can use for the longest time than any other algorithm. This process returns the less number of page faults and capable to complete the page analysis. In this article we have learned about the paging process and its applications. By using above mentioned algorithm and syntax, we have created some codes to solve the problem statement in an efficient manner.

Updated on: 05-May-2023

462 Views 