-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1_FIFO.c
83 lines (77 loc) · 2.23 KB
/
1_FIFO.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
#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);
}
/* 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 3 1
0 | 2 3 0
4 | 4 3 0
2 | 4 2 0
3 | 4 2 3
0 | 0 2 3
3 | Hit
2 | Hit
1 | 0 1 3
2 | 0 1 2
0 | Hit
1 | Hit
7 | 7 1 2
0 | 7 0 2
1 | 7 0 1
Total Page Faults = 15
*/