-
Notifications
You must be signed in to change notification settings - Fork 8
/
FIFO-Page-Replacement.c
52 lines (48 loc) · 1.5 KB
/
FIFO-Page-Replacement.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
#include <stdio.h>
void fifo(int[], int[], int, int);
int main()
{
int i, pCount, fCount, pages[30], frames[20];
printf("Number of Frames : ");
scanf("%d", &fCount);
// create frames array will null value
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]);
}
// call the function
fifo(pages, frames, pCount, fCount);
return 0;
}
void fifo(int pages[], int frames[], int pCount, int fCount) {
printf("\nRef.String |\tFrames\n");
printf("-------------------------------\n");
int i, j, k, flag, faultCount = 0, queue = 0;
for (i = 0; i < pCount; ++i) {
printf(" %d\t|\t", pages[i]);
flag = 0;
for (j = 0; j < fCount; ++j) {
if (frames[j] == pages[i]) { // compare with string in str[]
flag = 1;
printf(" Hit");
break;
}
}
if (flag == 0) { // not present in frames
frames[queue] = pages[i];
faultCount++;
queue = (queue + 1) % fCount; // Queue position in circular way
// display
for (k = 0; k < fCount; ++k) {
printf("%d ", frames[k]);
}
}
printf("\n\n");
}
printf("Total Page Faults = %d\n", faultCount);
}