-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLRU.cpp
98 lines (91 loc) · 2.56 KB
/
LRU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
LRU cache replacement algorithm
Implemented by:
Anuj Rai (2019AIM1003)
Mahip Soni (2019AIM1006)
for CS-604(Advanced Operating System) Group Project Work
*/
#include<bits/stdc++.h>
using namespace std;
unsigned int MissCount=0,HitCount=0;
/*
Function to implement LRU page referencing algorithm
*/
void LRUReferencePage(vector<unsigned int>& LRU_queue,unsigned int cacheSize, long pageNum)
{
unsigned int trav, l=LRU_queue.size(),pos;
int flag=0;
for (pos=0;pos<l;pos++)
{
if (LRU_queue[pos]== pageNum)
{
flag=1;
break;
}
}
if (!flag)
{
if(l==cacheSize)
{
LRU_queue.pop_back();
}
LRU_queue.insert(LRU_queue.begin(),pageNum);
MissCount++;
}
else
{
unsigned int temp=LRU_queue[pos];
LRU_queue.erase(LRU_queue.begin()+pos);
LRU_queue.insert(LRU_queue.begin(),temp);
HitCount++;
}
}
int main()
{ //input and output file names
char ipFileName[8][100]={"P12.lis",}, opFileName[100]="DemoLRU.txt";
int x,y;
unsigned int cacheSize;
//scanf("%s %s",ipFileName,opFileName);
//cache size input from the user
//cin>>cacheSize;
for(x=0;x<8;x++)
{
for(y=32;y<=4096;y*=2)
{ printf("%s %d\n",ipFileName[x],y);
cacheSize=y;
MissCount=0,HitCount=0;
//time variables, to calculate runtime
time_t start,stop;
FILE *fp=fopen(ipFileName[x], "r");
if(!fp)
{
printf("Error while opening the file: %s\n", ipFileName);
exit(EXIT_FAILURE);
}
unsigned int iStartingBlock = 0, iNumberOfBlocks = 0, iIgnore = 0, iRequestNumber = 0;
unsigned int i=0;
vector<unsigned int> LRU_queue(cacheSize); //LRU queue
time(&start);
//Reading traces from file
while (fscanf(fp, "%u %u %u %u", &iStartingBlock, &iNumberOfBlocks, &iIgnore, &iRequestNumber)!=-1)
{
for (i = iStartingBlock; i < (iStartingBlock + iNumberOfBlocks); i++)
{
LRUReferencePage(LRU_queue, cacheSize,i);
}
}
time(&stop);
fclose(fp);
fp=fopen(opFileName,"a");
fprintf(fp,"%s %d\n",ipFileName[x],y);
fprintf(fp,"%u %u\n",MissCount,HitCount);
//fprintf(fp,"iMissCount + iHitCount = %u\n", iMissCount + iHitCount);
//fprintf(fp,"Total Requests = %u\n", iTotalRequests);
fprintf(fp,"Hit Ratio = %5.4f %%\n", ((float)(HitCount * 100) / (HitCount + MissCount)));
fprintf(fp,"Rounded Hit Ratio = %5.2f %%\n", floor(((float)(HitCount * 100) / (HitCount + MissCount)) * 100 + 0.5) / 100);
fprintf(fp,"Finished in about %.0f seconds. \n", difftime(stop, start));
fclose(fp);
}
}
return 0;
}