-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRU algorithm
More file actions
42 lines (42 loc) · 1008 Bytes
/
Copy pathLRU algorithm
File metadata and controls
42 lines (42 loc) · 1008 Bytes
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
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
int n, f, page, faults = 0;
cout << "Enter number of frames: ";
cin >> f;
cout << "Enter number of pages: ";
cin >> n;
vector<int> pages(n);
cout << "Enter reference string: ";
for (int i = 0; i < n; i++) cin >> pages[i];
vector<int> frame;
unordered_map<int,int> lastUsed;
for (int i = 0; i < n; i++) {
page = pages[i];
bool found = false;
for (int x : frame) if (x == page) found = true;
if (!found) {
if (frame.size() < f) frame.push_back(page);
else {
int lru_index = 0;
int min_last = i;
for (int j = 0; j < frame.size(); j++) {
if (lastUsed[frame[j]] < min_last) {
min_last = lastUsed[frame[j]];
lru_index = j;
}
}
frame[lru_index] = page;
}
faults++;
}
lastUsed[page] = i;
cout << "Frames: ";
for (int x : frame) cout << x << " ";
cout << endl;
}
cout << "Total Page Faults: " << faults << endl;
return 0;
}