Page Faults in LFU | Implementation
In this, it is using 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. LFU is one such page replacement policy in which the least frequently used pages are replaced. If the frequency of pages is the same, then the page that has arrived first is replaced first.
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 Frequently Used (LFU) Algorithm.
Example-1 :
Input : N = 7, C = 3
pages = { 1, 2, 3, 4, 2, 1, 5 }
Output :
Page Faults = 6
Page Hits = 1
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 3 is required, since it is not present,
it is a page fault : page fault = 2 + 1 = 3
Page 4 is required, since it is not present,
it replaces LFU page 1 : page fault = 3 + 1 = 4
Page 2 is required, since it is present,
it is not a page fault : page fault = 4 + 0 = 4
Page 1 is required, since it is not present,
it replaces LFU page 3 : page fault = 4 + 1 = 5
Page 5 is required, since it is not present,
it replaces LFU page 4 : page fault = 5 + 1 = 6
Example-2 :
Input : N = 9, C = 4
pages = { 5, 0, 1, 3, 2, 4, 1, 0, 5 }
Output :
Page Faults = 8
Page Hits = 1
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 LFU page 5 : page fault = 4 + 1 = 5
Page 4 is required, since it is not present,
it replaces LFU 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 LFU 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.
Create a map to store frequency of pages
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
increase its frequency
else:
if(memory is full)
remove the first element and decrease frequency of 1st element
Increment count
push the element at the end and increase its frequency
Compare frequency with other pages starting from the 2nd last page
Sort the pages based on their frequency and time at which they arrive
if frequency is same, then, the page arriving first must be placed first
Following is the implementation of the above algorithm.
C#
using System;
using System.Collections.Generic;
using System.Linq;
class LFUPageFaults {
static int PageFaults( int n, int c, int [] pages)
{
int count = 0;
List< int > v = new List< int >();
Dictionary< int , int > mp
= new Dictionary< int , int >();
for ( int i = 0; i <= n - 1; i++) {
int idx = v.IndexOf(pages[i]);
if (idx == -1) {
if (v.Count == c) {
int leastFreqPage = v[0];
mp[leastFreqPage]
= mp[leastFreqPage] - 1;
v.RemoveAt(0);
}
v.Add(pages[i]);
mp[pages[i]]
= mp.GetValueOrDefault(pages[i], 0) + 1;
count++;
}
else {
int page = v[idx];
v.RemoveAt(idx);
v.Add(page);
mp[page] = mp[page] + 1;
}
int k = v.Count - 2;
while (k >= 0 && mp[v[k]] > mp[v[k + 1]]) {
int temp = v[k];
v[k] = v[k + 1];
v[k + 1] = temp;
k--;
}
}
return count;
}
static void Main( string [] args)
{
int [] pages = { 1, 2, 3, 4, 2, 1, 5 };
int n = 7, c = 3;
Console.WriteLine( "Page Faults = "
+ PageFaults(n, c, pages));
Console.WriteLine( "Page Hits = "
+ (n - PageFaults(n, c, pages)));
}
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int pageFaults( int n, int c, int pages[])
{
int count = 0;
vector< int > v;
unordered_map< int , int > mp;
int i;
for (i = 0; i <= n - 1; i++) {
auto it = find(v.begin(), v.end(), pages[i]);
if (it == v.end()) {
if (v.size() == c) {
mp[v[0]]--;
v.erase(v.begin());
}
v.push_back(pages[i]);
mp[pages[i]]++;
count++;
}
else {
mp[pages[i]]++;
v.erase(it);
v.push_back(pages[i]);
}
int k = v.size() - 2;
while (mp[v[k]] > mp[v[k + 1]] && k > -1) {
swap(v[k + 1], v[k]);
k--;
}
}
return count;
}
int main()
{
int pages[] = { 1, 2, 3, 4, 2, 1, 5 };
int n = 7, c = 3;
cout << "Page Faults = " << pageFaults(n, c, pages)
<< endl;
cout << "Page Hits = " << n - pageFaults(n, c, pages);
return 0;
}
|
Java
import java.util.*;
public class LFUPageFaults {
static int pageFaults( int n, int c, int [] pages)
{
int count = 0 ;
List<Integer> v = new ArrayList<Integer>();
Map<Integer, Integer> mp
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i <= n - 1 ; i++) {
int idx = v.indexOf(pages[i]);
if (idx == - 1 ) {
if (v.size() == c) {
int leastFreqPage = v.get( 0 );
mp.put(leastFreqPage,
mp.get(leastFreqPage) - 1 );
v.remove( 0 );
}
v.add(pages[i]);
mp.put(pages[i],
mp.getOrDefault(pages[i], 0 ) + 1 );
count++;
}
else {
int page = v.remove(idx);
v.add(page);
mp.put(page, mp.get(page) + 1 );
}
int k = v.size() - 2 ;
while (k >= 0
&& mp.get(v.get(k))
> mp.get(v.get(k + 1 ))) {
Collections.swap(v, k, k + 1 );
k--;
}
}
return count;
}
public static void main(String[] args)
{
int [] pages = { 1 , 2 , 3 , 4 , 2 , 1 , 5 };
int n = 7 , c = 3 ;
System.out.println( "Page Faults = "
+ pageFaults(n, c, pages));
System.out.println( "Page Hits = "
+ (n - pageFaults(n, c, pages)));
}
}
|
Python3
from typing import List
from collections import defaultdict
def pageFaults(n: int , c: int , pages: List [ int ]) - > int :
count = 0
v = []
mp = defaultdict( int )
for i in range (n):
if pages[i] not in v:
if len (v) = = c:
mp[v[ 0 ]] - = 1
v.pop( 0 )
v.append(pages[i])
mp[pages[i]] + = 1
count + = 1
else :
mp[pages[i]] + = 1
v.remove(pages[i])
v.append(pages[i])
k = len (v) - 2
while k > = 0 and mp[v[k]] > mp[v[k + 1 ]]:
v[k], v[k + 1 ] = v[k + 1 ], v[k]
k - = 1
return count
if __name__ = = '__main__' :
pages = [ 1 , 2 , 3 , 4 , 2 , 1 , 5 ]
n, c = 7 , 3
print ( "Page Faults = " , pageFaults(n, c, pages))
print ( "Page Hits = " , n - pageFaults(n, c, pages))
|
Javascript
function pageFaults(n, c, pages) {
let count = 0;
let v = [];
let mp = {};
for (let i = 0; i <= n - 1; i++) {
let idx = v.indexOf(pages[i]);
if (idx == -1) {
if (v.length == c) {
let leastFreqPage = v[0];
mp[leastFreqPage] = mp[leastFreqPage] - 1;
v.shift();
}
v.push(pages[i]);
mp[pages[i]] = (mp[pages[i]] || 0) + 1;
count++;
} else {
let page = v.splice(idx, 1)[0];
v.push(page);
mp[page] = mp[page] + 1;
}
let k = v.length - 2;
while (k >= 0 && mp[v[k]] > mp[v[k + 1]]) {
[v[k], v[k + 1]] = [v[k + 1], v[k]];
k--;
}
}
return count;
}
let pages = [1, 2, 3, 4, 2, 1, 5];
let n = 7,
c = 3;
console.log( "Page Faults = " + pageFaults(n, c, pages));
console.log( "Page Hits = " + (n - pageFaults(n, c, pages)));
|
Output
Page Faults = 6
Page Hits = 1
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...