-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLRU.c
78 lines (73 loc) · 1.37 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
#include <stdio.h>
#include <stdlib.h>
int search(int a[], int t, int n)
{
int i=0;
while(i<n)
{
if(a[i]==t)
return 0;
i++;
}
return 1;
}
void printstack(int stack[], int n)
{
int i=0;
for(; i<n; i++)
printf("%d ",stack[i]);
printf("\n");
}
int findsmallest(int stamp[], int n)
{
int i=1;
int small=stamp[0];
int loc=0;
for(; i<n; i++)
if(stamp[i]<small)
{
small=stamp[i];
loc=i;
}
return loc;
}
int main()
{
printf("No of pages: ");
int pages;
scanf("%d", &pages);
printf("Lenght of string: ");
int n;
scanf("%d", &n);
int i;
int a[n];
int cnt=0;
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
int j=0;
int stack[4]={-1,-1,-1,-1};
int stamp[4]={0,0,0,0};
while(i<n)
{
if(search(stack,a[i],pages))
{
int index= findsmallest(stamp,pages);
printf("Replace %d with data %d\n", index,a[i]);
stack[index]=a[i];
stamp[index]++;
printstack(stack, pages);
}
else
{
printf("Page found:%d\n",a[i]);
int k=0;
for(; k<pages;k++)
if(a[i]==a[k])
break;
stamp[k]=stamp[k]+1;
}
i++;
}
return 0;
}