Professional Documents
Culture Documents
CAO Lab 6
Cache Replacement Policies
Onkar Joshi
ECE A1
UI20EC27
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)
vector<int> cache;
int q_size,f_size,n,val;
cout<<"Enter Frame Size : ";
cin>>f_size;
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 & Cache Full
else
{
}
}
UI20EC27 CAO LAB REPORT
Code :
#include <bits/stdc++.h>
if (arr[i] == key)
return i;
UI20EC27 CAO LAB REPORT
return -1;
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;
cin>>f_size;
int cache[f_size];
cin>>q_size;
for(int i=0;i<q_size;i++)
cout<<"Enter Query
cin>>val;
if(index != -1)
count[index]++;
printData(cache, p, count);
cache[p] = val;
count[p]++;
p++;
printData(cache, p, count);
else
{
UI20EC27 CAO LAB REPORT
cout<<maxIndex<<endl;
cache[maxIndex] = val;
count[maxIndex] = 1;
printData(cache, p, count);
Code :
#include <bits/stdc++.h>
if (arr[i] == key)
return true;
return false;
int j;
if (arr[i] == query[j])
if (j > farthest)
farthest = j;
ans = i;
break;
if (j == q_size)
UI20EC27 CAO LAB REPORT
return i;
vector<int> arr;
int hit = 0;
if (searchInCache(query[i], arr))
hit++;
continue;
arr.push_back(query[i]);
else
arr[j] = query[i];
cout << "Cache MISS = " << q_size - hit << endl;
int main()
int q_size;
UI20EC27 CAO LAB REPORT
int query[q_size];
int frames;
return 0;