-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal2.c
More file actions
237 lines (193 loc) · 5.72 KB
/
final2.c
File metadata and controls
237 lines (193 loc) · 5.72 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Created by Tenzin monlam on 04/04/20.
#include<stdio.h>
#include<stdlib.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i){
if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
int i,j,n,no_frame,k,avail,count=0;
int p[20],frame[10];
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n); //input no. of page
printf("\n PAGE NUMBER :\n");
for(i=1;i<=n;i++)
{
p[i]=rand()%10; // random page /*scanf("%d",&a[i]); */ // manual
}
for(i=1;i<=n;i++)
{
printf("a[%d] = %d \n",i,p[i]);
}
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no_frame);//no. of frames
for(i=0;i<n;i++)
frame[i]= -1; //for empty space = -1
//FIRST IN FIRST OUT (FIFO) PAGE REPLACEMENT ALGO
printf("\n--FIRST IN FIRST OUT(FIFO)--\n");
j=0;
printf("ref string\t\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",p[i]);
avail=0;
for(k=0;k<no_frame;k++)
if(frame[k]==p[i])
{
avail=1;
break;
}
if (avail==0)
{
frame[j]=p[i];
j=(j+1)%no_frame;
count++;
for(k=0;k<no_frame;k++)
printf("%d\t",frame[k]);
}
else
{
for(k=0;k<no_frame;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d\n",count);
//LEAST RECENTLY USED (LRU) PAGE REPLACEMENT ALGO
int flag1,flag2,counter=0,faults=0,time[10],pos;
for(i=0;i<n;i++)
frame[i]= -1;
printf("\n\n--LEAST RECENTLY USED(LRU)--\n");
printf("ref string\t\t page frames\n");
for(i = 1; i <= n; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_frame; ++j)
{
if(frame[j] == p[i])
{
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_frame; ++j)
{
if(frame[j] == -1)
{
counter++;
faults++;
frame[j] = p[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
pos = findLRU(time, no_frame);
counter++;
faults++;
frame[pos] = p[i];
time[pos] = counter;
}
printf("\n");
printf("%d\t\t",p[i]);
for(j = 0; j < no_frame; ++j)
{
printf("%d\t", frame[j]);
}
}
printf("\n\n Page Faults is %d\n", faults);
//OPTIMAL PAGE REPLACEMENT ALGO
int temp[10],f1,f2,f3,max;
faults=0;
for(i=0;i<n;i++)
frame[i]= -1;
printf("\n\n--OPTIMAL PAGE REPLACEMENT --\n");
printf("ref string\t\t page frames\n");
for(i = 1; i <= n; ++i)
{
f1 = f2 = 0;
for(j = 0; j < no_frame; ++j)
{
if(frame[j] == p[i])
{
f1 = f2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_frame; ++j)
{
if(frame[j] == -1)
{
faults++;
frame[j] = p[i];
f2 = 1;
break;
}
}
}
if(f2 == 0)
{
f3 =0;
for(j = 0; j < no_frame; ++j)
{
temp[j] = -1;
for(k = i + 1; k < n; ++k)
{
if(frame[j] == p[k])
{
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_frame; ++j)
{
if(temp[j] == -1)
{
pos = j;
f3 = 1;
break;
}
}
if(f3 ==0)
{
max = temp[0];
pos = 0;
for(j = 1; j < no_frame; ++j)
{
if(temp[j] > max)
{
max = temp[j];
pos = j;
}
}
}
frame[pos] = p[i];
faults++;
}
printf("\n");
printf("%d\t\t", p[i]);
for(j = 0; j < no_frame; ++j)
{
printf("%d\t", frame[j]);
}
}
printf("\n\nTotal Page Faults = %d\n", faults);
return 0;
}