-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimal.c
141 lines (126 loc) · 2.22 KB
/
optimal.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#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]);
}
void print(int m, int f[m])
{
int i;
for(i=0; i<m; i++)
{
printf(" %d",f[i]);
}
}
int checkdemand(int pos, int ele, int n, int s[n], int m, int f[m], int demand[m])
{
int i, j, max = -1, index = -1;
for(i=0; i<m; i++)
{
demand[i] = 0;
for(j=0; j<n; j++)
{
if(j > pos)
{
if(f[i] == s[j])
break;
else
demand[i]++;
}
}
if(i == 0)
{
max = demand[i];
index = 0;
}
else if(demand[i] > max)
{
max = demand[i];
index = i;
}
}
printf("\t -Page Fault- demand of (");
print(m,f);
printf(" ) is after (");
print(m,demand);
printf(" ) requests =>\treplace %d",f[index]);
return index;
}
void setdefault(int m, int f[m])
{
int i;
for(i=0; i<m; i++)
f[i] = -1;
}
int iterate(int n, int s[n], int m, int f[m])
{
printf("\n\n\tPage\t Frames\n\t---------------------------");
int i, k = -1, index = -1, pf = 0;
int demand[m];
for(i=0; i<n; i++)
{
if(available(m,f,s[i]) == 0)
{
pf++;
if(hasspace(m,f) == 1)
{
k++;
f[k] = s[i];
}
else
{
index = checkdemand(i,s[i],n,s,m,f,demand);
f[index] = s[i];
}
}
printf("\n\t[%d]",s[i]);
printF(m,f);
if((i+1)%4 == 0 )
printf("\n\n");
}
return pf;
}
void main()
{
int n, m = 3, i;
printf("\n\t\t\tOptimal\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);
}