-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.cpp
234 lines (199 loc) · 6.35 KB
/
page.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <algorithm>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <queue>
using namespace std;
void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters) {
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) {
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
int fifo(string reference_string, int nframes) {
vector<int> v;
int page_fault=0;
vector<string> tokens;
Tokenize(reference_string,tokens," ");
vector<int> vint;
int i;
for (i = 0; i < tokens.size(); i++) {
vint.push_back(atoi(tokens[i].c_str()));
}
for(i = 0; i < vint.size(); i++) {
if(find(v.begin(), v.end(),vint[i]) != v.end()) {
/* Page already present */
} else {
/* v Page fault */
if(v.size()==nframes) {
v.erase(v.begin());
}
v.push_back(vint[i]);
page_fault++;
}
}
return page_fault;
}
int lru(string reference_string, int nframes) {
vector<int> v;
int page_fault=0;
vector<string> tokens;
Tokenize(reference_string,tokens," ");
vector<int> vint;
int i;
for (i = 0; i < tokens.size(); i++) {
vint.push_back(atoi(tokens[i].c_str()));
}
for(i = 0; i < vint.size(); i++) {
std::vector<int>::iterator it;
it = find(v.begin(), v.end(),vint[i]);
if(it != v.end()) {
/* Page already present */
v.erase(it);
} else {
/* v Page fault */
if(v.size() == nframes) {
v.erase(v.begin());
}
page_fault++;
}
v.push_back(vint[i]);
}
return page_fault;
}
struct lfu_struct {
lfu_struct (int page_no): page_no (page_no){frequency = 1;};
int page_no;
int frequency;
};
int lfu (string reference_string, int nframes) {
vector<int> v;
int page_fault=0;
vector<string> tokens;
Tokenize(reference_string,tokens," ");
vector<int> vint;
vector <lfu_struct> loaded_pages;
int i;
for (i = 0; i < tokens.size(); i++) {
vint.push_back(atoi(tokens[i].c_str()));
}
for(i = 0; i < vint.size(); i++) {
std::vector<lfu_struct>::iterator it;
int found = 0, min_freq, min_freq_index;
if(loaded_pages.size() > 0)
min_freq = loaded_pages[0].frequency;
for(it = loaded_pages.begin(); it < loaded_pages.end(); it++) {
if(it -> frequency <= min_freq) {
min_freq = it->frequency;
min_freq_index = it-loaded_pages.begin();
}
if(it -> page_no == vint[i]) {
// already present
found = 1;
it->frequency++;
}
}
if (found == 0) {
// Page fault
page_fault++;
if(loaded_pages.size() == nframes) {
vector<lfu_struct>::iterator nth = loaded_pages.begin() + min_freq_index;
loaded_pages.erase(nth);
}
loaded_pages.push_back(*new lfu_struct(vint[i]));
}
}
return page_fault;
}
int second_chance (string reference_string, int nframes) {
vector<int> v;
vector<bool> reference_bit;
int page_fault=0;
vector<string> tokens;
Tokenize(reference_string,tokens," ");
vector<int> vint;
int i;
for (i = 0; i < tokens.size(); i++) {
vint.push_back(atoi(tokens[i].c_str()));
}
for(i = 0; i < vint.size(); i++) {
std::vector<int>::iterator it;
it = find(v.begin(), v.end(),vint[i]);
if(it != v.end()) {
/* v Page already present */
reference_bit[it-v.begin()] =true;
} else {
/* Page fault */
if(v.size() == nframes) {
for(int i = 0; i < v.size(); i++) {
// Give second chance
if (reference_bit[i] == true)
reference_bit[i] = false;
else {
// Remove page
v.erase(v.begin() + i);
reference_bit.erase(reference_bit.begin()+i);
break;
}
}
if(it==v.end()) { // all pages given second chance, so remove the first page
v.erase(v.begin());
reference_bit.erase(reference_bit.begin());
}
}
v.push_back(vint[i]);
reference_bit.push_back(true);
page_fault++;
}
}
return page_fault;
}
string generate_reference_string(int n, int d) {
stringstream os;
int i, j, k;
for (i=0; i<n; i++ ) {
for(j=0; j<n; j++ ) {
for(k=0; k<n; k++) {
os << 101 + (i * n + k) / d << " " << 501 + (k * n + j) / d << " ";
}
os << 1001 + (i * n + j) / d << " ";
}
}
return os.str();
}
int main() {
int n, d;
cout << "n = ";
cin >> n;
cout << "d = ";
cin >> d;
string reference_string = generate_reference_string(n,d);
int no_of_frames, fault_count;
int i;
cout << endl;
cout << "frames\tfifo\tlru\tlfu\tsecond chance" << endl;
for(i = 1; i < 3 * n * n / d + d; i++) {
no_of_frames = i;
cout << i << "\t";
fault_count = fifo(reference_string, no_of_frames);
cout << fault_count << "\t";
fault_count = lru(reference_string, no_of_frames);
cout << fault_count << "\t";
fault_count = lfu(reference_string, no_of_frames);
cout << fault_count << "\t";
fault_count = second_chance(reference_string, no_of_frames);
cout << fault_count << "\t";
cout << endl;
}
}