-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru.c
More file actions
63 lines (54 loc) · 1.48 KB
/
Copy pathlru.c
File metadata and controls
63 lines (54 loc) · 1.48 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
#include <stdio.h>
int main() {
int nop, nof, i , j, frames[100], rs[100], fault = 0, start = 0, hit = 0;
int recent[100];
printf("Enter the number of process : ");
scanf("%d", &nop);
printf("Enter the reference string\n");
for(int i = 1; i <= nop; i++) {
scanf("%d", &rs[i]);
}
printf("Enter the number of frames : ");
scanf("%d", &nof);
for(j = 1; j <= nof; j++) {
frames[j] = -1;
recent[j] = 0;
printf("Frames %d", j);
}
printf("\n");
for(i = 1; i <= nop; i++) {
for(j = 1; j <= nof; j++) {
if(frames[j] == rs[i]) {
hit++;
recent[j] = i;
break;
} else if(frames[j] == -1){
fault++;
frames[j] = rs[i];
recent[j] = i;
break;
}
}
if(j > nof) {
fault++;
int lru = 1;
for(j = 2; j <= nof; j++) {
if(recent[j] < recent[lru]) {
lru = j;
}
}
frames[lru] = rs[i];
recent[lru] = i;
}
for(j = 1; j <= nof; j++) {
if(frames[j] == -1) {
printf("%c\t", 'E');
} else {
printf("%d\t", frames[j]);
}
}
printf("\n");
}
printf("\nPage faults = %d\nHit ratio = %f\n", fault, (float)hit/nop);
return 0;
}