-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRU_implementation.cpp
95 lines (86 loc) · 2.65 KB
/
LRU_implementation.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
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
signed main(signed argc, char **argv){
string PageTableString=argv[1];
string out="22116015_"+PageTableString+'_'+argv[2]+"_"+argv[3]+"_"+argv[4];
ofstream MyFile(out);
ifstream inputs(PageTableString);
string vpns;
getline(inputs,vpns);
getline(inputs,vpns);
getline(inputs,vpns);
map<int,int> PageTable;
while(getline(inputs,vpns)){
string pfns;
int string_size=vpns.size();
int index;
for(int i=0;i<string_size;i++){
if(vpns.at(i)==' '){
index=i;
break;
}
}
for(int i=index+1;i<string_size;i++){
if(vpns.at(i)!=' '){
pfns=vpns.substr(i,string_size-i);
vpns=vpns.substr(0,index);
break;
}
}
PageTable.insert({stoll(vpns),stoll(pfns)});
}
string tlbaccessstring=argv[2];
ifstream input2(tlbaccessstring);
string VAstring;
getline(input2,VAstring);
int offset_bits=stoll(VAstring);
vector<pair<int,int>> outvec;
vector<pair<int,int>> TLBvec;
int TLBmax=stoll(argv[4]);
int miss=0,hits=0;
bool flag;
int accesses=0;
int pfn;
while(getline(input2,VAstring)){
int virtadd=stoll(VAstring);
int vpn=virtadd>>offset_bits;
flag=false;
for(int i=0;i<TLBvec.size();i++){
if(TLBvec[i].first==vpn){
pfn=TLBvec[i].second;
int phyadd=((pfn)<<offset_bits);
phyadd+=virtadd-(vpn<<offset_bits);
TLBvec.erase(TLBvec.begin()+i);
TLBvec.push_back({vpn,pfn});
flag=true;
outvec.push_back({1,phyadd});
}
}
if(flag==false){
miss++;
int vpn=virtadd>>offset_bits;
if(TLBvec.size()==TLBmax){
TLBvec.erase(TLBvec.begin());
}
int pfn=PageTable[vpn];
int phyadd=((pfn)<<offset_bits);
phyadd+=virtadd-(vpn<<offset_bits);
TLBvec.push_back({vpn,pfn});
outvec.push_back({0,phyadd});
}else hits++;
accesses++;
}
MyFile<<"TOTAL_ACCESSES = "<<accesses<<"\n";
MyFile<<"TOTAL_MISSES = "<<miss<<"\n";
MyFile<<"TOTAL_HITS = "<<hits<<"\n";
for(auto it:outvec){
if(it.first==1){
MyFile<<it.second<<" HIT\n";
}else{
MyFile<<it.second<<" MISS\n";
}
}
return 0;
}