-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-32
More file actions
60 lines (50 loc) · 1.4 KB
/
problem-32
File metadata and controls
60 lines (50 loc) · 1.4 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
#include <stdio.h>
void lruPageReplacement(int pages[], int n, int capacity) {
int frames[capacity];
int counter[capacity];
for (int i = 0; i < capacity; i++) {
frames[i] = -1;
counter[i] = 0;
}
int time = 0, pageFaults = 0;
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < capacity; j++) {
if (frames[j] == pages[i]) {
found = 1;
counter[j] = time;
time++;
break;
}
}
if (!found) {
int lruIndex = 0;
for (int j = 1; j < capacity; j++) {
if (counter[j] < counter[lruIndex]) {
lruIndex = j;
}
}
frames[lruIndex] = pages[i];
counter[lruIndex] = time;
time++;
pageFaults++;
}
printf("Page: %d -> Frames: ", pages[i]);
for (int j = 0; j < capacity; j++) {
if (frames[j] != -1) {
printf("%d ", frames[j]);
} else {
printf("- ");
}
}
printf("\n");
}
printf("Total Page Faults: %d\n", pageFaults);
}
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3;
lruPageReplacement(pages, n, capacity);
return 0;
}