-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRU.c
86 lines (69 loc) · 1.13 KB
/
LRU.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
#include <stdio.h>
int main()
{
int pages[20], frames[20], np, nf, i, j, counter = 0, time[20] = {0}, faults = 0, q ,k;
int flag1, flag2, pos, mini;
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])
{
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j=0; j<nf; j++)
{
if(frames[j] == -1)
{
counter++;
faults++;
time[j] = counter;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
mini = time[0];
pos = 0;
for(k=1;k<nf;k++)
{
if(mini > time[k]){
mini = time[k];
pos = k;
}
}
counter++;
faults++;
time[pos] = counter;
frames[pos] = pages[i];
}
for(q=0;q<nf;q++)
{
printf("%d ", frames[q]);
}
printf("\n");
}
printf("Faults = %d\n", faults);
printf("\n");
return 0;
}