-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo.c
95 lines (83 loc) · 1.48 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
95
#include <stdio.h>
int hasspace(int m, int f[m])
{
int i;
for(i=0; i<m; i++)
{
if(f[i] == -1)
return 1;
}
return 0;
}
int available(int m, int f[m], int x)
{
int i;
for(i=0; i<m; i++)
if(f[i] == x)
return 1;
return 0;
}
void printF(int m, int f[m])
{
int i;
for(i=0; i<m; i++)
printf("\t%d",f[i]);
}
int iterate(int n, int s[n], int m, int f[m])
{
printf("\n\n\t\tPage\t Frames\n\t\t---------------------------\n\n");
int i, k = -1, pf = 0;
for(i=0; i<n; i++)
{
printf("\t");
if(available(m,f,s[i]) != 1)
{
pf++;
k++;
if(k >= m)
k = 0;
f[k] = s[i];
if(hasspace(m,f) != 1)
{
printf("!--");
}
}
printf("\t[%d]",s[i]);
printF(m,f);
printf("\n");
if((i+1)%4 == 0 )
printf("\n\n");
}
return pf;
}
void setdefault(int m, int f[m])
{
int i;
for(i=0; i<m; i++)
f[i] = -1;
}
void main()
{
int n, m = 3, i;
printf("\n\t\t\tFIFO\n");
printf("\n Enter the length of the reference string: ");
scanf("%d",&n);
int s[n];
printf("\n Enter the reference string: ");
//7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
for(i=0; i<n; i++)
{
scanf("%d",&s[i]);
if(s[i] < 0)
{
printf("\n Reference string cannot have negative entries.\n");
return;
}
}
printf("\n Enter the no. of frames: ");
scanf("%d",&m);
int f[m];
setdefault(m,f);
int pf = iterate(n,s,m,f);
printf("\n\tThe Total number of page faults: %d\n\n",pf);
}