-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulators.cpp
More file actions
178 lines (145 loc) · 4.54 KB
/
Copy pathsimulators.cpp
File metadata and controls
178 lines (145 loc) · 4.54 KB
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
#include "simulators.h"
#include <algorithm>
#include <limits>
// Helper: create empty frames vector
static std::vector<int> make_empty(int n) {
return std::vector<int>(n, -1);
}
//
// ---------------------------- FIFO ----------------------------
//
ResultSummary FIFO_Simulator::run() {
ResultSummary result;
std::vector<int> frames = make_empty(framesCount);
int pointer = 0;
for (int page : reference) {
SimulationStep step;
step.reference = page;
// Check HIT
bool hit = false;
for (int f : frames) {
if (f == page) { hit = true; break; }
}
step.hit = hit;
if (hit) {
result.page_hits++;
} else {
result.page_faults++;
// Replace using FIFO pointer
frames[pointer] = page;
pointer = (pointer + 1) % framesCount;
}
step.frames = frames;
result.steps.push_back(step);
}
return result;
}
//
// ----------------------------- LRU -----------------------------
//
ResultSummary LRU_Simulator::run() {
ResultSummary result;
std::vector<int> frames = make_empty(framesCount);
std::vector<int> lastUse(framesCount, -1);
for (int i = 0; i < (int)reference.size(); ++i) {
int page = reference[i];
SimulationStep step;
step.reference = page;
bool hit = false;
// Check HIT
for (int j = 0; j < framesCount; j++) {
if (frames[j] == page) {
hit = true;
lastUse[j] = i;
break;
}
}
step.hit = hit;
if (hit) {
result.page_hits++;
} else {
result.page_faults++;
// Find empty frame
int index = -1;
for (int j = 0; j < framesCount; j++) {
if (frames[j] == -1) {
index = j;
break;
}
}
// If full → replace least recently used
if (index == -1) {
int lruIndex = 0;
int minVal = lastUse[0];
for (int j = 1; j < framesCount; j++) {
if (lastUse[j] < minVal) {
minVal = lastUse[j];
lruIndex = j;
}
}
index = lruIndex;
}
frames[index] = page;
lastUse[index] = i;
}
step.frames = frames;
result.steps.push_back(step);
}
return result;
}
//
// --------------------------- OPTIMAL ---------------------------
//
ResultSummary Optimal_Simulator::run() {
ResultSummary result;
std::vector<int> frames = make_empty(framesCount);
for (int i = 0; i < (int)reference.size(); ++i) {
int page = reference[i];
SimulationStep step;
step.reference = page;
bool hit = false;
for (int f : frames) {
if (f == page) { hit = true; break; }
}
step.hit = hit;
if (hit) {
result.page_hits++;
} else {
result.page_faults++;
// Find an empty frame
int index = -1;
for (int j = 0; j < framesCount; j++) {
if (frames[j] == -1) {
index = j;
break;
}
}
// If full → find optimal victim
if (index == -1) {
int farthest = -1;
int farIndex = -1;
for (int j = 0; j < framesCount; j++) {
int currentPage = frames[j];
int nextUse = std::numeric_limits<int>::max();
// Find future use
for (int k = i + 1; k < (int)reference.size(); k++) {
if (reference[k] == currentPage) {
nextUse = k;
break;
}
}
// Choose page used farthest in future
if (nextUse > farthest) {
farthest = nextUse;
farIndex = j;
}
}
index = farIndex;
}
frames[index] = page;
}
step.frames = frames;
result.steps.push_back(step);
}
return result;
}