-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIFO.c
94 lines (74 loc) · 1.18 KB
/
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
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
int main()
{
int pages[20], frames[20], np, nf, i, j, faults = 0, q ,k, temp[20] = {0};
int flag1, flag2, flag3, pos, min;
printf("Enter the no. of frames -\n");
scanf("%d", &nf);
printf("Enter the no. of pages -\n");
scanf("%d", &np);
printf("Enter the pages -\n");
for(i=0;i<np;i++)
scanf("%d", &pages[i]);
for(i=0;i<20;i++)
frames[i] = -1;
for(i=0; i<np; i++)
{
flag1 = 0;
flag2 = 0;
for(j=0; j<nf; j++)
{
if(frames[j] == pages[i])
{
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j=0; j<nf; j++){
if(frames[j] == -1)
{
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}//flag1
if(flag2 == 0)
{
for(j=0;j<nf;j++)
{
for(k=i-1; k >= 0; k--)
{
if(frames[j] == pages[k])
{
temp[j] = k;
break;
}
}
}
min = temp[0];
pos = 0;
for(q = 1; q<nf; q++)
{
if(min > temp[q])
{
min = temp[q];
pos = q;
}
}
faults++;
frames[pos] = pages[i];
}//flag2
for(k=0; k<nf; k++)
{
printf("%d ", frames[k]);
}
printf("\n");
}//main loop
printf("Faults = %d\n", faults);
printf("\n");
return 0;
}