-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimal_page.c
63 lines (62 loc) · 1.79 KB
/
optimal_page.c
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
#include <stdio.h>
#include <limits.h>
int main() {
int incomingStream[] = {4, 1, 2, 4, 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream) / sizeof(incomingStream[0]);
printf("Incoming\tFrame 1\tFrame 2\tFrame 3\n");
int temp[frames];
for (m = 0; m < frames; m++) {
temp[m] = -1;
}
for (m = 0; m < pages; m++) {
s = 0;
for (n = 0; n < frames; n++) {
if (incomingStream[m] == temp[n]) {
s++;
}
}
if (s == 0) {
int index = -1;
for (n = 0; n < frames; n++) {
if (temp[n] == -1) {
index = n;
break;
}
}
if (index == -1) {
int farthest = -1;
for (n = 0; n < frames; n++) {
int j;
for (j = m + 1; j < pages; j++) {
if (temp[n] == incomingStream[j]) {
if (j > farthest) {
farthest = j;
index = n;
}
break;
}
}
if (j == pages && index == -1) {
index = n;
}
}
}
temp[index] = incomingStream[m];
pageFaults++;
}
printf("%d\t\t", incomingStream[m]);
for (n = 0; n < frames; n++) {
if (temp[n] != -1) {
printf("%d\t\t", temp[n]);
} else {
printf("-\t\t");
}
}
printf("\n");
}
printf("Total Page Faults:\t%d\n", pageFaults);
return 0;
}