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.
using System;
using System.Collections.Generic;
using System.Linq;
class LFUPageFaults {
/* Counts no. of page faults */
static int PageFaults( int n, int c, int [] pages)
{
// Initialise count to 0
int count = 0;
// To store elements in memory of size c
List< int > v = new List< int >();
// To store frequency of pages
Dictionary< int , int > mp
= new Dictionary< int , int >();
for ( int i = 0; i <= n - 1; i++) {
// Find if element is present in memory or not
int idx = v.IndexOf(pages[i]);
// If element is not present
if (idx == -1) {
// If memory is full
if (v.Count == c) {
// Decrease the frequency
int leastFreqPage = v[0];
mp[leastFreqPage]
= mp[leastFreqPage] - 1;
// Remove the first element as
// It is least frequently used
v.RemoveAt(0);
}
// Add the element at the end of memory
v.Add(pages[i]);
// Increase its frequency
mp[pages[i]]
= mp.GetValueOrDefault(pages[i], 0) + 1;
// Increment the count
count++;
}
else {
// If element is present
// Remove the element
// And add it at the end
// Increase its frequency
int page = v[idx];
v.RemoveAt(idx);
v.Add(page);
mp[page] = mp[page] + 1;
}
// Compare frequency with other pages
// starting from the 2nd last page
int k = v.Count - 2;
// 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
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 total page faults
return count;
}
/* Driver program to test pageFaults function*/
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++ program to illustrate // page faults in LFU #include <bits/stdc++.h> using namespace std;
/* Counts no. of page faults */ int pageFaults( int n, int c, int pages[])
{ // Initialise count to 0
int count = 0;
// To store elements in memory of size c
vector< int > v;
// To store frequency of pages
unordered_map< int , int > mp;
int i;
for (i = 0; i <= n - 1; i++) {
// Find if element is present in memory or not
auto it = find(v.begin(), v.end(), pages[i]);
// If element is not present
if (it == v.end()) {
// If memory is full
if (v.size() == c) {
// Decrease the frequency
mp[v[0]]--;
// Remove the first element as
// It is least frequently used
v.erase(v.begin());
}
// Add the element at the end of memory
v.push_back(pages[i]);
// Increase its frequency
mp[pages[i]]++;
// Increment the count
count++;
}
else {
// If element is present
// Remove the element
// And add it at the end
// Increase its frequency
mp[pages[i]]++;
v.erase(it);
v.push_back(pages[i]);
}
// Compare frequency with other pages
// starting from the 2nd last page
int k = v.size() - 2;
// 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
while (mp[v[k]] > mp[v[k + 1]] && k > -1) {
swap(v[k + 1], v[k]);
k--;
}
}
// Return total page faults
return count;
} /* Driver program to test pageFaults function*/ 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;
} // This code is contributed by rajsanghavi9. |
import java.util.*;
public class LFUPageFaults {
/* Counts no. of page faults */
static int pageFaults( int n, int c, int [] pages)
{
// Initialise count to 0
int count = 0 ;
// To store elements in memory of size c
List<Integer> v = new ArrayList<Integer>();
// To store frequency of pages
Map<Integer, Integer> mp
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i <= n - 1 ; i++) {
// Find if element is present in memory or not
int idx = v.indexOf(pages[i]);
// If element is not present
if (idx == - 1 ) {
// If memory is full
if (v.size() == c) {
// Decrease the frequency
int leastFreqPage = v.get( 0 );
mp.put(leastFreqPage,
mp.get(leastFreqPage) - 1 );
// Remove the first element as
// It is least frequently used
v.remove( 0 );
}
// Add the element at the end of memory
v.add(pages[i]);
// Increase its frequency
mp.put(pages[i],
mp.getOrDefault(pages[i], 0 ) + 1 );
// Increment the count
count++;
}
else {
// If element is present
// Remove the element
// And add it at the end
// Increase its frequency
int page = v.remove(idx);
v.add(page);
mp.put(page, mp.get(page) + 1 );
}
// Compare frequency with other pages
// starting from the 2nd last page
int k = v.size() - 2 ;
// 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
while (k >= 0
&& mp.get(v.get(k))
> mp.get(v.get(k + 1 ))) {
Collections.swap(v, k, k + 1 );
k--;
}
}
// Return total page faults
return count;
}
/* Driver program to test pageFaults function*/
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)));
}
} |
# Python program to illustrate page faults in LFU from typing import List
from collections import defaultdict
def pageFaults(n: int , c: int , pages: List [ int ]) - > int :
# Initialise count to 0
count = 0
# To store elements in memory of size c
v = []
# To store frequency of pages
mp = defaultdict( int )
for i in range (n):
# Find if element is present in memory or not
if pages[i] not in v:
# If element is not present
if len (v) = = c:
# If memory is full
# Decrease the frequency
mp[v[ 0 ]] - = 1
# Remove the first element as it is least frequently used
v.pop( 0 )
# Add the element at the end of memory
v.append(pages[i])
# Increase its frequency
mp[pages[i]] + = 1
# Increment the count
count + = 1
else :
# If element is present
# Remove the element and add it at the end
# Increase its frequency
mp[pages[i]] + = 1
v.remove(pages[i])
v.append(pages[i])
# Compare frequency with other pages starting from the 2nd last page
k = len (v) - 2
# 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
while k > = 0 and mp[v[k]] > mp[v[k + 1 ]]:
v[k], v[k + 1 ] = v[k + 1 ], v[k]
k - = 1
# Return total page faults
return count
# Driver program to test pageFaults function 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 program to illustrate // page faults in LFU function pageFaults(n, c, pages) {
// Initialise count to 0
let count = 0;
// To store elements in memory of size c
let v = [];
// To store frequency of pages
let mp = {};
for (let i = 0; i <= n - 1; i++) {
// Find if element is present in memory or not
let idx = v.indexOf(pages[i]);
// If element is not present
if (idx == -1) {
// If memory is full
if (v.length == c) {
// Decrease the frequency
let leastFreqPage = v[0];
mp[leastFreqPage] = mp[leastFreqPage] - 1;
// Remove the first element as
// It is least frequently used
v.shift();
}
// Add the element at the end of memory
v.push(pages[i]);
// Increase its frequency
mp[pages[i]] = (mp[pages[i]] || 0) + 1;
// Increment the count
count++;
} else {
// If element is present
// Remove the element
// And add it at the end
// Increase its frequency
let page = v.splice(idx, 1)[0];
v.push(page);
mp[page] = mp[page] + 1;
}
// Compare frequency with other pages
// starting from the 2nd last page
let k = v.length - 2;
// 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
while (k >= 0 && mp[v[k]] > mp[v[k + 1]]) {
[v[k], v[k + 1]] = [v[k + 1], v[k]];
k--;
}
}
// Return total page faults
return count;
} // Driver program to test pageFaults function 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)));
|
Page Faults = 6 Page Hits = 1