Skip to content

Commit 274855b

Browse files
authored
Merge pull request #73 from Satvik-Singh192/feat/page_simulator
feat: implemented page replacement algorithms
2 parents a282612 + edfc0ee commit 274855b

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <iomanip>
4+
using namespace std;
5+
void printFrames(const vector<int>& frames) {
6+
cout << "Frames: ";
7+
for (int f : frames) {
8+
if (f == -1) cout << "- ";
9+
else cout << f << " ";
10+
}
11+
cout << endl;
12+
}
13+
void simulateFIFO(int framesCount, const vector<int>& pages) {
14+
vector<int> frames(framesCount, -1);
15+
int pointer = 0, hits = 0, misses = 0;
16+
17+
cout << "\n===== FIFO Page Replacement =====\n";
18+
19+
for (int p : pages) {
20+
bool hit = false;
21+
for (int f : frames) {
22+
if (f == p) { hit = true; break; }
23+
}
24+
25+
if (hit) {
26+
hits++;
27+
cout << "Page " << p << ": HIT | ";
28+
} else {
29+
misses++;
30+
frames[pointer] = p;
31+
pointer = (pointer + 1) % framesCount;
32+
cout << "Page " << p << ": MISS | ";
33+
}
34+
35+
printFrames(frames);
36+
}
37+
38+
cout << "\nTotal Hits : " << hits;
39+
cout << "\nTotal Misses: " << misses;
40+
cout << "\nHit Ratio : " << fixed << setprecision(2)
41+
<< (double)hits / pages.size() << endl;
42+
}
43+
void simulateLRU(int framesCount, const vector<int>& pages) {
44+
vector<int> frames(framesCount, -1);
45+
vector<int> lastUsed(framesCount, -1);
46+
int time = 0, hits = 0, misses = 0;
47+
48+
cout << "\n===== LRU Page Replacement =====\n";
49+
50+
for (int p : pages) {
51+
time++;
52+
bool hit = false;
53+
for (int i = 0; i < framesCount; i++) {
54+
if (frames[i] == p) {
55+
hit = true;
56+
lastUsed[i] = time;
57+
break;
58+
}
59+
}
60+
61+
if (hit) {
62+
hits++;
63+
cout << "Page " << p << ": HIT | ";
64+
} else {
65+
misses++;
66+
67+
int replaceIndex = -1;
68+
for (int i = 0; i < framesCount; i++) {
69+
if (frames[i] == -1) {
70+
replaceIndex = i;
71+
break;
72+
}
73+
}
74+
if (replaceIndex == -1) {
75+
int minTime = lastUsed[0];
76+
replaceIndex = 0;
77+
78+
for (int i = 1; i < framesCount; i++) {
79+
if (lastUsed[i] < minTime) {
80+
minTime = lastUsed[i];
81+
replaceIndex = i;
82+
}
83+
}
84+
}
85+
86+
frames[replaceIndex] = p;
87+
lastUsed[replaceIndex] = time;
88+
cout << "Page " << p << ": MISS | ";
89+
}
90+
91+
printFrames(frames);
92+
}
93+
94+
cout << "\nTotal Hits : " << hits;
95+
cout << "\nTotal Misses: " << misses;
96+
cout << "\nHit Ratio : " << fixed << setprecision(2)
97+
<< (double)hits / pages.size() << endl;
98+
}
99+
void simulateOptimal(int framesCount, const vector<int>& pages) {
100+
vector<int> frames(framesCount, -1);
101+
int hits = 0, misses = 0;
102+
103+
cout << "\n===== Optimal Page Replacement =====\n";
104+
105+
for (int i = 0; i < (int)pages.size(); i++) {
106+
int p = pages[i];
107+
bool hit = false;
108+
for (int f : frames) {
109+
if (f == p) { hit = true; break; }
110+
}
111+
112+
if (hit) {
113+
hits++;
114+
cout << "Page " << p << ": HIT | ";
115+
} else {
116+
misses++;
117+
int replaceIdx = -1;
118+
for (int j = 0; j < framesCount; j++) {
119+
if (frames[j] == -1) {
120+
replaceIdx = j;
121+
break;
122+
}
123+
}
124+
if (replaceIdx == -1) {
125+
int farthest = -1;
126+
for (int j = 0; j < framesCount; j++) {
127+
int nextUse = -1;
128+
for (int k = i + 1; k < (int)pages.size(); k++) {
129+
if (pages[k] == frames[j]) {
130+
nextUse = k;
131+
break;
132+
}
133+
}
134+
if (nextUse == -1) {
135+
replaceIdx = j;
136+
break;
137+
}
138+
139+
if (nextUse > farthest) {
140+
farthest = nextUse;
141+
replaceIdx = j;
142+
}
143+
}
144+
}
145+
146+
frames[replaceIdx] = p;
147+
cout << "Page " << p << ": MISS | ";
148+
}
149+
150+
printFrames(frames);
151+
}
152+
153+
cout << "\nTotal Hits : " << hits;
154+
cout << "\nTotal Misses: " << misses;
155+
cout << "\nHit Ratio : " << fixed << setprecision(2)
156+
<< (double)hits / pages.size() << endl;
157+
}
158+
159+
int main() {
160+
int framesCount, n, choice;
161+
cout << "Enter number of frames: ";
162+
cin >> framesCount;
163+
164+
cout << "Enter number of page requests: ";
165+
cin >> n;
166+
167+
vector<int> pages(n);
168+
cout << "Enter the page reference string:\n";
169+
for (int i = 0; i < n; i++) cin >> pages[i];
170+
171+
cout << "\nChoose Algorithm:\n";
172+
cout << "1. FIFO\n2. LRU\n3. Optimal\n";
173+
cout << "Enter choice: ";
174+
cin >> choice;
175+
176+
switch (choice) {
177+
case 1: simulateFIFO(framesCount, pages); break;
178+
case 2: simulateLRU(framesCount, pages); break;
179+
case 3: simulateOptimal(framesCount, pages); break;
180+
default: cout << "Invalid choice!\n";
181+
}
182+
183+
return 0;
184+
}
89.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)