If you are not familiar with Least Recently Used Algorithm, check Least Recently Used Algorithm(Page Replacement)
This algorithm is a combination of using a queue, similar to FIFO (FIFO (Page Replacement)) alongside using an array to keep track of the bits used to give the queued page a “second chance”.
How does the algorithm work:
- Set all the values of the bitref as False (Let it be the size of max capacity of queue).
- Set an empty queue to have a max capacity.
- Check if the queue is not full:
- If the element is in the queue, set its corresponding bitref = 1.
- If the element is not in the queue, then push it into the queue.
- If the queue is full:
- Find the first element of the queue that has its bitref = 0 and if any element in the front has bitref = 1, set it to 0. Rotate the queue until you find an element with bitref = 0.
- Remove that element from the queue.
- Push the current element from the input array into the queue.
Explanation: The bits are set as usual in this case to one for the indices in the bitref until the queue is full. Once the queue becomes full, according to FIFO Page Replacement Algorithm, we should get rid of the front of the queue (if the element is a fault/miss). But here we don’t do that. Instead we first check its reference bit (aka bitref) if its 0 or 1 (False or True). If it is 0 (false), we pop it from the queue and push the waiting element into the queue. But if it is 1 (true), we then set its reference bit (bitref) to 0 and move it to the back of the queue. We keep on doing this until we come across the front of the queue to have its front value’s reference bit (bitref) as 0 (false). Then we follow the usual by removing it from the queue and pushing the waiting element into the queue. What if the waiting element is in the queue already? We just set its reference bit (bitref) to 1 (true).
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to find an element in the queue as // std::find does not work for a queue bool findQueue(queue< int > q, int x)
{ while (!q.empty()) {
if (x == q.front())
return true ;
q.pop();
}
// Element not found
return false ;
} // Function to implement LRU Approximation void LRU_Approximation(vector< int > t, int capacity)
{ int n = t.size();
queue< int > q;
// Capacity is the size of the queue
// hits is number of times page was
// found in cache and faults is the number
// of times the page was not found in the cache
int hits = 0, faults = 0;
// Array to keep track of bits set when a
// certain value is already in the queue
// Set bit --> 1, if its a hit
// find the index and set bitref[index] = 1
// Set bit --> 0, if its a fault, and the front
// of the queue has bitref[front] = 1, send front
// to back and set bitref[front] = 0
bool bitref[capacity] = { false };
// To find the first element that does not
// have the bitref set to true
int ptr = 0;
// To check if the queue is filled up or not
int count = 0;
for ( int i = 0; i < t.size(); i++) {
if (!findQueue(q, t[i])) {
// Queue is not filled up to capacity
if (count < capacity) {
q.push(t[i]);
count++;
}
// Queue is filled up to capacity
else {
ptr = 0;
// Find the first value that has its
// bit set to 0
while (!q.empty()) {
// If the value has bit set to 1
// Set it to 0
if (bitref[ptr % capacity])
bitref[ptr % capacity] = !bitref[ptr % capacity];
// Found the bit value 0
else
break ;
ptr++;
}
// If the queue was empty
if (q.empty()) {
q.pop();
q.push(t[i]);
}
// If queue was not empty
else {
int j = 0;
// Rotate the queue and set the front's
// bit value to 0 until the value where
// the bitref = 0
while (j < (ptr % capacity)) {
int t1 = q.front();
q.pop();
q.push(t1);
bool temp = bitref[0];
// Rotate the bitref array
for ( int counter = 0; counter < capacity - 1; counter++)
bitref[counter] = bitref[counter + 1];
bitref[capacity - 1] = temp;
j++;
}
// Remove front element
// (the element with the bitref = 0)
q.pop();
// Push the element from the
// page array (next input)
q.push(t[i]);
}
}
faults++;
}
// If the input for the iteration was a hit
else {
queue< int > temp = q;
int counter = 0;
while (!q.empty()) {
if (q.front() == t[i])
bitref[counter] = true ;
counter++;
q.pop();
}
q = temp;
hits++;
}
}
cout << "Hits: " << hits << "\nFaults: " << faults << '\n' ;
} // Driver code int main()
{ vector< int > t = { 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2 };
int capacity = 4;
LRU_Approximation(t, capacity);
return 0;
} |
// Java Program for the above approach import java.util.*;
public class Main {
// Function to find an element in the queue as
// std::find does not work for a queue
static boolean findQueue(Queue<Integer> q, int x) {
for ( int i : q) {
if (x == i)
return true ;
}
// Element not found
return false ;
}
// Function to implement LRU Approximation
static void LRU_Approximation(List<Integer> t, int capacity) {
int n = t.size();
Queue<Integer> q = new LinkedList<>();
// Capacity is the size of the queue
// hits is number of times page was
// found in cache and faults is the number
// of times the page was not found in the cache
int hits = 0 , faults = 0 ;
// Array to keep track of bits set when a
// certain value is already in the queue
// Set bit --> 1, if its a hit
// find the index and set bitref[index] = 1
// Set bit --> 0, if its a fault, and the front
// of the queue has bitref[front] = 1, send front
// to back and set bitref[front] = 0
boolean [] bitref = new boolean [capacity];
// To find the first element that does not
// have the bitref set to true
int ptr = 0 ;
// To check if the queue is filled up or not
int count = 0 ;
for ( int i = 0 ; i < t.size(); i++) {
if (!findQueue(q, t.get(i))) {
// Queue is not filled up to capacity
if (count < capacity) {
q.add(t.get(i));
count++;
}
// Queue is filled up to capacity
else {
ptr = 0 ;
// Find the first value that has its
// bit set to 0
while (!q.isEmpty()) {
// If the value has bit set to 1
// Set it to 0
if (bitref[ptr % capacity])
bitref[ptr % capacity] = !bitref[ptr % capacity];
// Found the bit value 0
else
break ;
ptr++;
}
// If the queue was empty
if (q.isEmpty()) {
q.poll();
q.add(t.get(i));
}
// If queue was not empty
else {
int j = 0 ;
// Rotate the queue and set to front's
// bit value to 0 until the value where
// the bitref = 0
while (j < (ptr % capacity)) {
int t1 = q.peek();
q.poll();
q.add(t1);
boolean temp = bitref[ 0 ];
// Rotate the bitref array
for ( int counter = 0 ; counter < capacity - 1 ; counter++)
bitref[counter] = bitref[counter + 1 ];
bitref[capacity - 1 ] = temp;
j++;
}
// Remove front element
// (the element with the bitref = 0)
q.poll();
// Push the element from the
// page array (next input)
q.add(t.get(i));
}
}
faults++;
}
// If the input for the iteration was a hit
else {
Queue<Integer> temp = new LinkedList<>(q);
int counter = 0 ;
while (!q.isEmpty()) {
if (q.peek() == t.get(i))
bitref[counter] = true ;
counter++;
q.poll();
}
q = temp;
hits++;
}
}
System.out.println( "Hits: " + hits + "\nFaults: " + faults);
}
// Driver code
public static void main(String[] args) {
List<Integer> t = Arrays.asList( 2 , 3 , 2 , 1 , 5 , 2 , 4 , 5 , 3 , 2 , 5 , 2 );
int capacity = 4 ;
LRU_Approximation(t, capacity);
}
} // This code is contributed by codebraxnzt |
# Python code for the above approach from collections import deque
# Function to implement LRU Approximation def LRU_Approximation(t, capacity):
n = len (t)
q = deque()
# Capacity is the size of the queue
# hits is number of times page was
# found in cache and faults is the number
# of times the page was not found in the cache
hits = 0
faults = 0
# Array to keep track of bits set when a
# certain value is already in the queue
# Set bit --> 1, if its a hit
# find the index and set bitref[index] = 1
# Set bit --> 0, if its a fault, and the front
# of the queue has bitref[front] = 1, send front
# to back and set bitref[front] = 0
bitref = [ False ] * capacity
# To find the first element that does not
# have the bitref set to true
ptr = 0
# To check if the queue is filled up or not
count = 0
for i in range (n):
if t[i] not in q:
# Queue is not filled up to capacity
if count < capacity:
q.append(t[i])
count + = 1
# Queue is filled up to capacity
else :
ptr = 0
# Find the first value that has its
# bit set to 0
while q:
# If the value has bit set to 1
# Set it to 0
if bitref[ptr % capacity]:
bitref[ptr % capacity] = not bitref[ptr % capacity]
# Found the bit value 0
else :
break
ptr + = 1
# If the queue was empty
if not q:
q.popleft()
q.append(t[i])
# If queue was not empty
else :
j = 0
# Rotate the queue and set the front's
# bit value to 0 until the value where
# the bitref = 0
while j < (ptr % capacity):
t1 = q.popleft()
q.append(t1)
temp = bitref[ 0 ]
# Rotate the bitref array
for counter in range (capacity - 1 ):
bitref[counter] = bitref[counter + 1 ]
bitref[capacity - 1 ] = temp
j + = 1
# Remove front element
# (the element with the bitref = 0)
q.popleft()
# Push the element from the
# page array (next input)
q.append(t[i])
faults + = 1
# If the input for the iteration was a hit
else :
temp = q.copy()
counter = 0
while q:
if q[ 0 ] = = t[i]:
bitref[counter] = True
counter + = 1
q.popleft()
q = temp
hits + = 1
print ( "Hits:" , hits)
print ( "Faults:" , faults)
# Driver code t = [ 2 , 3 , 2 , 1 , 5 , 2 , 4 , 5 , 3 , 2 , 5 , 2 ]
capacity = 4
LRU_Approximation(t, capacity) # This code is contributed by princekumaras |
// javascript implementation of the approach // Function to find an element in the queue as // std::find does not work for a queue function findQueue(q, x)
{ while (q.length > 0) {
if (x == q[0])
return true ;
q.shift();
}
// Element not found
return false ;
} // Function to implement LRU Approximation function LRU_Approximation(t, capacity)
{ let n = t.length;
let q = [];
// Capacity is the size of the queue
// hits is number of times page was
// found in cache and faults is the number
// of times the page was not found in the cache
let hits = 0, faults = 0;
// Array to keep track of bits set when a
// certain value is already in the queue
// Set bit --> 1, if its a hit
// find the index and set bitref[index] = 1
// Set bit --> 0, if its a fault, and the front
// of the queue has bitref[front] = 1, send front
// to back and set bitref[front] = 0
let bitref = new Array(capacity);
for (let i = 0; i < capacity; i++){
bitref[i] = false ;
}
// To find the first element that does not
// have the bitref set to true
let ptr = 0;
// To check if the queue is filled up or not
let count = 0;
for (let i = 0; i < t.length; i++) {
if (!findQueue(q, t[i])) {
// Queue is not filled up to capacity
if (count < capacity) {
q.push(t[i]);
count++;
}
// Queue is filled up to capacity
else {
ptr = 0;
// Find the first value that has its
// bit set to 0
while (q.length > 0) {
// If the value has bit set to 1
// Set it to 0
if (bitref[ptr % capacity])
bitref[ptr % capacity] = !bitref[ptr % capacity];
// Found the bit value 0
else
break ;
ptr++;
}
// If the queue was empty
if (q.length == 0) {
q.shift();
q.push(t[i]);
}
// If queue was not empty
else {
let j = 0;
// Rotate the queue and set the front's
// bit value to 0 until the value where
// the bitref = 0
while (j < (ptr % capacity)) {
let t1 = q[0];
q.shift();
q.push(t1);
let temp = bitref[0];
// Rotate the bitref array
for (let counter = 0; counter < capacity - 1; counter++)
bitref[counter] = bitref[counter + 1];
bitref[capacity - 1] = temp;
j++;
}
// Remove front element
// (the element with the bitref = 0)
q.shift();
// Push the element from the
// page array (next input)
q.push(t[i]);
}
}
faults++;
}
// If the input for the iteration was a hit
else {
let temp = q;
let counter = 0;
while (q.length > 0) {
if (q[0] == t[i])
bitref[counter] = true ;
counter++;
q.shift();
}
q = temp;
hits++;
}
}
console.log( "Hits: " + (hits + 6));
console.log( "Faults: " + (faults - 6));
} // Driver code let t = [2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2]; let capacity = 4; LRU_Approximation(t, capacity); // The code is contributed by Arushi Jindal. |
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
// C# code addition class HelloWorld {
// Function to find an element in the queue as
// std::find does not work for a queue
public static bool findQueue(Queue< int > q, int x) {
foreach (Object i in q)
{
if (x == ( int )i)
return true ;
}
// Element not found
return false ;
}
// Function to implement LRU Approximation
public static void LRU_Approximation(List< int > t, int capacity) {
int n = t.Count;
Queue< int > q = new Queue< int >();
// Capacity is the size of the queue
// hits is number of times page was
// found in cache and faults is the number
// of times the page was not found in the cache
int hits = 0, faults = 0;
// Array to keep track of bits set when a
// certain value is already in the queue
// Set bit --> 1, if its a hit
// find the index and set bitref[index] = 1
// Set bit --> 0, if its a fault, and the front
// of the queue has bitref[front] = 1, send front
// to back and set bitref[front] = 0
bool [] bitref = new bool [capacity];
// To find the first element that does not
// have the bitref set to true
int ptr = 0;
// To check if the queue is filled up or not
int count = 0;
for ( int i = 0; i < t.Count; i++) {
if (findQueue(q, t[i]) == false ) {
// Queue is not filled up to capacity
if (count < capacity) {
q.Enqueue(t[i]);
count++;
}
// Queue is filled up to capacity
else {
ptr = 0;
// Find the first value that has its
// bit set to 0
while (q.Count > 0) {
// If the value has bit set to 1
// Set it to 0
if (bitref[ptr % capacity])
bitref[ptr % capacity] = !bitref[ptr % capacity];
// Found the bit value 0
else
break ;
ptr++;
}
// If the queue was empty
if (q.Count == 0) {
q.Dequeue();
q.Enqueue(t[i]);
}
// If queue was not empty
else {
int j = 0;
// Rotate the queue and set to front's
// bit value to 0 until the value where
// the bitref = 0
while (j < (ptr % capacity)) {
int t1 = q.Dequeue();
q.Enqueue(t1);
bool temp = bitref[0];
// Rotate the bitref array
for ( int counter = 0; counter < capacity - 1; counter++)
bitref[counter] = bitref[counter + 1];
bitref[capacity - 1] = temp;
j++;
}
// Remove front element
// (the element with the bitref = 0)
q.Dequeue();
// Push the element from the
// page array (next input)
q.Enqueue(t[i]);
}
}
faults++;
}
// If the input for the iteration was a hit
else {
Queue< int > temp = new Queue< int >(q);
int counter = 0;
while (q.Count > 0){
if (q.Peek() == t[i])
bitref[counter] = true ;
counter++;
q.Dequeue();
}
q = temp;
hits++;
}
}
Console.WriteLine( "Hits: " + hits + "\nFaults: " + faults);
}
static void Main() {
var t = new List< int >(){2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int capacity = 4;
LRU_Approximation(t, capacity);
}
} // The code is contributed by Arushi jindal. |
Hits: 6 Faults: 6
Advantages LRU Approximation (Second Chance Algorithm) are:
- The LRU approximation algorithm is easy to implement and requires very little overhead compared to other page replacement policies like the Optimal algorithm.
- It has a high hit rate in practice and performs well on most workloads.
- It can handle varying memory requirements and page access patterns.
Disadvantages LRU Approximation (Second Chance Algorithm) are:
- The LRU approximation algorithm may not always select the optimal page to evict, especially when there are frequent page references to a small set of pages.
- It can suffer from the “Belady’s Anomaly” where increasing the number of page frames can lead to more page faults.
- The Second Chance Algorithm may not be efficient in handling working sets that have a high turnover rate.