-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path2_LRU.c
114 lines (105 loc) · 3.21 KB
/
2_LRU.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
void LRU(int[], int[], int[], int, int);
int findLRU(int[], int);
int main() {
int i, pCount, fCount, pages[30], frames[20], time[20];
printf("Number of Frames : ");
scanf("%d", &fCount);
// create frames array will null values
for (i = 0; i < fCount; ++i) {
frames[i] = -1;
}
printf("Number of Pages : ");
scanf("%d", &pCount);
printf("Enter the reference string\n");
for (i = 0; i < pCount; ++i) {
scanf("%d", &pages[i]);
}
LRU(pages, frames, time, fCount, pCount);
return 0;
}
void LRU(int pages[], int frames[], int time[], int fCount, int pCount) {
printf("\nRef.String |\tFrames\n");
printf("-------------------------------\n");
int i, j, k, pos, flag, faultCount, counter, queue;
counter = 0, queue = 0, faultCount = 0;
for (i = 0; i < pCount; ++i) {
flag = 0;
printf(" %d\t|\t", pages[i]);
// check if already present
for (j = 0; j < fCount; ++j) {
if (frames[j] == pages[i]) {
flag = 1; // this means that the character is already there in one of the frames.
counter++;
time[j] = counter; // update occurance
printf(" Hit\n\n");
break;
}
}
// push if there is free space
if ((flag == 0) && (queue < fCount)) { // empty frames[] spaces
faultCount++;
counter++;
frames[queue] = pages[i];
time[queue] = counter;
queue++; // queue is to keep the count of number of frames which have been filled.
}
// replacement as frames[] is full
else if ((flag == 0) && (queue == fCount)) { // the frames are full now
faultCount++;
counter++;
pos = findLRU(time, fCount);
frames[pos] = pages[i];
time[pos] = counter;
}
// printing
if (flag == 0) {
for (k = 0; k < fCount; ++k) {
printf("%d ", frames[k]);
}
printf("\n\n");
}
}
printf("Total Page Faults = %d\n\n", faultCount);
}
int findLRU(int time[], int fCount) {
int k, min, pos;
pos = 0;
min = time[0];
for (k = 1; k < fCount; ++k) {
if (time[k] < min) {
min = time[k];
pos = k;
}
}
return pos;
}
/* OUTPUT
Number of Frames : 3
Number of Pages : 20
Enter the reference string
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Ref.String | Frames
-------------------------------
7 | 7 -1 -1
0 | 7 0 -1
1 | 7 0 1
2 | 2 0 1
0 | Hit
3 | 2 0 3
0 | Hit
4 | 4 0 3
2 | 4 0 2
3 | 4 3 2
0 | 0 3 2
3 | Hit
2 | Hit
1 | 1 3 2
2 | Hit
0 | 1 0 2
1 | Hit
7 | 1 0 7
0 | Hit
1 | Hit
Total Page Faults = 12
*/