You are on page 1of 11

UI20EC27 CAO LAB REPORT

CAO Lab 6
Cache Replacement Policies
Onkar Joshi
ECE A1
UI20EC27

1. LRU (Least Recently Used) :

Code :
#include <bits/stdc++.h>
using namespace std;
int getIndex(vector<int> v, int K)

{
auto it = find(v.begin(), v.end(), K);
// If element was found
if (it != v.end())

{
// calculating the index of K
int index = it - v.begin();
return index;
}

else {
// If the element is not present in the vector
return -1;
}

}
void printData(vector<int> v, int n)
{
UI20EC27 CAO LAB REPORT

cout<<endl;
cout<<"CACHE : ";
for (int i :v)

cout << i << " ";


cout << endl;
}
int main(){

vector<int> cache;
int q_size,f_size,n,val;
cout<<"Enter Frame Size : ";
cin>>f_size;

cout<<"Enter Query Size : ";


cin>>q_size;
for(int i=0;i<q_size;i++)
{
cout<<"Enter Query : ";

cin>>val;
int index = getIndex(cache, val);
// Element found in Cache
if(index != -1)
{
cout<<" *** CACHE HIT *** "<<endl;
// Place the recently used element to front
cache.erase(cache.begin()+index);

cache.push_back(val);
printData(cache, q_size);
}
UI20EC27 CAO LAB REPORT

// Element not found but cache has space


else if(i<f_size && index == -1)
{

cout<<" *** CACHE MISS *** "<<endl<<" Element added in


Cache"<<endl;
cache.push_back(val);
printData(cache, q_size);

}
// Element not found & Cache Full
else
{

cout<<" *** CACHE MISS *** "<<" LRU"<<endl;


cache.erase(cache.begin());
cache.push_back(val);
printData(cache, q_size);
}

}
}
UI20EC27 CAO LAB REPORT

2. MFU (Most Frequently Used) :

Code :

#include <bits/stdc++.h>

using namespace std;

int getIndex(int arr[], int n, int key)

for (int i = 0; i < n; i++)

if (arr[i] == key)

return i;
UI20EC27 CAO LAB REPORT

return -1;

void printData(int arr1[], int n, int arr2[])

cout<<"CACHE : "<<" ";

for (int i = 0; i < n; i++)

cout << arr1[i] << " ";

cout << endl;

cout<<"Count : "<<" ";

for (int i = 0; i < n; i++)

cout << arr2[i] << " ";

cout << endl<<endl;

// Returns index of max element

int findMax(int arr[], int n)

int maxInd = 0;

for(int i=0;i<n;i++)

if(arr[i]>arr[maxInd])

maxInd = i;

return maxInd;

int main() {

int q_size,f_size,n,val;

int p = 0;

cout<<"Enter Frame Size : ";


UI20EC27 CAO LAB REPORT

cin>>f_size;

int cache[f_size];

int count[f_size] = {0};

cout<<"Enter Query Size : ";

cin>>q_size;

for(int i=0;i<q_size;i++)

cout<<"Enter Query

cin>>val;

int index = getIndex(cache, f_size,val);

// Element found in Cache

if(index != -1)

cout<<" *** CACHE HIT *** "<<endl;

count[index]++;

printData(cache, p, count);

// Element not found but cache has space

else if(index == -1 && p<f_size)

cout<<" *** CACHE MISS *** "<<endl;

cache[p] = val;

count[p]++;

p++;

printData(cache, p, count);

// Element not found & Cache Full ==> MFU

else

{
UI20EC27 CAO LAB REPORT

cout<<" *** CACHE MISS *** "<<endl<<" MFU"<<endl;

int maxIndex = findMax(count, f_size) ;

cout<<maxIndex<<endl;

cache[maxIndex] = val;

count[maxIndex] = 1;

printData(cache, p, count);

3. Optimal Replacement Policy :


UI20EC27 CAO LAB REPORT

Code :

#include <bits/stdc++.h>

using namespace std;

bool searchInCache(int key, vector<int> &arr)

for (int i = 0; i < arr.size(); i++)

if (arr[i] == key)

return true;

return false;

int Optimal_Index(int query[], vector<int> &arr, int q_size, int index)

int ans = -1, farthest = index;

for (int i = 0; i < arr.size(); i++)

int j;

for (j = index; j < q_size; j++)

if (arr[i] == query[j])

if (j > farthest)

farthest = j;

ans = i;

break;

if (j == q_size)
UI20EC27 CAO LAB REPORT

return i;

return (ans == -1) ? 0 : ans;

void OPT(int query[], int q_size, int frames)

vector<int> arr;

int hit = 0;

for (int i = 0; i < q_size; i++)

if (searchInCache(query[i], arr))

hit++;

continue;

if (arr.size() < frames)

arr.push_back(query[i]);

else

int j = Optimal_Index(query, arr, q_size, i + 1);

arr[j] = query[i];

cout << "Cache HIT = " << hit << endl;

cout << "Cache MISS = " << q_size - hit << endl;

int main()

int q_size;
UI20EC27 CAO LAB REPORT

cout << "Enter Query Size : ";

cin >> q_size;

int query[q_size];

cout << "Enter queries : ";

for (int i = 0; i < q_size; i++)

cin >> query[i];


UI20EC27 CAO LAB REPORT

int frames;

cout << "Enter frame size : ";

cin >> frames;

cout << endl;

OPT(query, q_size, frames);

return 0;

You might also like