forked from vaibhavstark/Hacktoberfest_2021-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRU_os
380 lines (341 loc) · 9.54 KB
/
LRU_os
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <stdio.h>
#include <stdlib.h>
#define total_frame 20
// CODE SUBMITTED BY ASHISH LALWANI
// ROLL NO:- 18UCS194
int c1 = 0;
int c2 = 0;
void local_strategy(int page_per_process,int process_no,int page_no,int memory[][10],int timer[][10],int free_frame_list[][10],int total_process)
{
int i,j;
int check = 0;
int check2 = 0;
printf("Free frame list\n");
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++)
{
printf("%d ",free_frame_list[i][j]);
}
printf("\n");
}
for(i=0;i<page_per_process;i++)
{
if(memory[process_no][i]==page_no)
{
printf("Hi1\n");
check = 1;
timer[process_no][i] = 0;
for(j=0;j<page_per_process;j++)
{
if(j!=i)
{
timer[process_no][j]++;
}
}
break;
}
}
if(check==0)
{
printf("Hi2\n");
for(j=0;j<page_per_process;j++)
{
if(free_frame_list[process_no][j]==0)
{
check2 = 1;
free_frame_list[process_no][j] = 1;
timer[process_no][j] = 0;
memory[process_no][j] = page_no;
printf("memory[][] %d\n",memory[process_no][j]);
break;
}
}
}
if(check2==0) // apply LRU
{
printf("Hi3\n");
int max = -1;
int index;
for(j=0;j<page_per_process;j++)
{
if(timer[process_no][j] > max)
{
index = j;
max = timer[process_no][j];
}
}
printf("Victim page for corressponding process is %d\n",memory[process_no][index]);
timer[process_no][index] = 0;
memory[process_no][index] = page_no;
c1++;
}
}
int main()
{
int i,j,a,b;
//int total_process = rand()%5 + 1;
int total_process = 5;
int page_requiremnet[total_process];
printf("Page requirement of each process\n");
for(i=0;i<total_process;i++)
{
page_requiremnet[i] = rand()%10 + 1;
printf("%d page number requirement is %d\n",i,page_requiremnet[i]);
}
int page_per_process = total_frame/total_process;
printf("Each process will get %d th frames\n",page_per_process);
int memory[total_process][page_per_process];
int memoryA[total_process][page_per_process];
int free_frame_list[total_process][page_per_process];
int free_frame_listA[total_process][page_per_process];
int timer[total_process][page_per_process];
int timerA[total_process][page_per_process];
int total_references,process_no,page_no;
int check,check2;
printf("Here I am using LRU Counting Implementation\n");
printf("Row for each process and column for frames allocated to that process\n");
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++) // initialize memory by -1
{
memory[i][j] = -1;
memoryA[i][j] = -1;
}
}
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++)
{
free_frame_list[i][j] = -1;
free_frame_listA[i][j] = -1;
}
}
printf("Free frame list\n");
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++)
{
printf("%d ",free_frame_list[i][j]);
}
printf("\n");
}
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++)
{
timer[i][j] = 0;
timerA[i][j] = 0;
}
}
printf("How many reference do you want?\n");
scanf("%d",&total_references);
while(total_references--)
{
printf("Please enter process number and page number for reference\n");
scanf("%d %d",&process_no,&page_no);
// local_strategy(page_per_process,process_no,page_no,memory,timer,free_frame_list,total_process);
///////////////////////////////////////////
/// LOCAL ALLOCATION/////
check = 0;
check2 = 0;
printf("Free frame list in case of GLOBAL ALLOCATION\n");
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++)
{
printf("%d ",free_frame_list[i][j]);
}
printf("\n");
}
printf("Memory list in case of LOCAL ALLOCATION\n");
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++)
{
printf("%d ",memory[i][j]);
}
printf("\n");
}
for(i=0;i<page_per_process;i++)
{
if(memory[process_no][i]==page_no)
{
// printf("Hi1\n");
check = 1;
timer[process_no][i] = 0;
for(j=0;j<page_per_process;j++)
{
if(j!=i)
{
timer[process_no][j]++;
}
}
break;
}
}
if(check==0)
{
// printf("Hi2\n");
for(j=0;j<page_per_process;j++)
{
if(free_frame_list[process_no][j]==-1)
{
check2 = 1;
free_frame_list[process_no][j] = 1;
timer[process_no][j] = 0;
for(i=0;i<page_per_process;i++)
{
if(j!=i)
{
timer[process_no][i]++;
}
}
memory[process_no][j] = page_no;
printf("memory[][] %d\n",memory[process_no][j]);
break;
}
}
}
if(check2==0 && check!=1) // apply LRU
{
// printf("Hi3\n");
int max = -1;
int index;
for(j=0;j<page_per_process;j++)
{
if(timer[process_no][j] > max)
{
index = j;
max = timer[process_no][j];
}
}
printf("Victim page for corressponding process is %d\n",memory[process_no][index]);
timer[process_no][index] = 0;
for(i=0;i<page_per_process;i++)
{
if(i!= index)
{
timer[process_no][i]++; // Increase counter for rest of elements
}
}
memory[process_no][index] = page_no;
c1++;
}
////////////////////////////////////////////
printf("C1 after each iteration %d\n",c1);
///////////////////////////////////////// Apply LRU Global
////// GLOBAL ALLOCATION /////
int checkA = 0;
int check2A = 0;
int index_x,index_y;
printf("Free frame list in case GLOBAL ALLOCATION\n");
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++)
{
printf("%d ",free_frame_listA[i][j]);
}
printf("\n");
}
printf("Memory list in case GLOBAL ALLOCATION\n");
for(i=0;i<total_process;i++)
{
for(j=0;j<page_per_process;j++)
{
printf("%d ",memoryA[i][j]);
}
printf("\n");
}
int flag = 0;
for(a=0;a<total_process;a++){
for(i=a;i<page_per_process;i++)
{
if(memoryA[a][i]==page_no)
{
// printf("Hi1\n");
check = 1;
timerA[a][i] = 0;
for(b=0;b<total_process;b++){
for(j=b;j<page_per_process;j++)
{
if(j!=i && b!=a)
{
timerA[b][j]++;
}
}
}
flag = 1;
break;
}
}
if(flag==1)
break;
}
flag = 0;
if(checkA==0)
{
// printf("Hi2\n");
for(a=0;a<total_process;a++){
for(j=a;j<page_per_process;j++)
{
if(free_frame_listA[a][j]==-1)
{
check2A = 1;
free_frame_listA[a][j] = 1;
timerA[a][j] = 0;
for(b=0;b<total_process;b++){
for(i=b;i<page_per_process;i++)
{
if(i!=j && b!=a)
{
timerA[b][i]++;
}
}
}
memoryA[a][j] = page_no;
// printf("memory[][] %d\n",memoryA[a][j]);
flag = 1;
break;
}
}
if(flag==1)
break;
}
}
if(check2A==0 && checkA!=1) // apply LRU
{
// printf("Hi3\n");
int max = -1;
int index;
for(a=0;a<total_process;a++){
for(j=0;j<page_per_process;j++)
{
if(timerA[a][j] > max)
{
index_x = a;
index_y = j;
max = timerA[a][j];
}
}
}
printf("Victim page for corressponding process is %d\n",memoryA[index_x][index_y]);
timerA[index_x][index_y] = 0;
for(b=0;b<total_process;b++){
for(i=b;i<page_per_process;i++)
{
if(i!=index_y && b!=index_x)
{
timerA[b][i]++;
}
}
}
memoryA[index_x][index_y] = page_no;
c2++;
}
//////////////////////////////////////////////
}
printf("Efficiency in terms of total number of LRU Replacement in local or global allocations\n");
printf("Ratio of global(LRU) vs local(LRU) %d/%d",c2,c1);
return 0;
}