forked from kabirthakkar/Hkfst2k21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LruPageReplacement.c
86 lines (81 loc) · 1.93 KB
/
LruPageReplacement.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 n,nf,in[100],p[50];
int hit=0,i,j,k;
int pgfaultcnt=0;
void getData(){
printf("\nEnter length of page reference sequence:");
scanf("%d",&n);
printf("\nEnter the page reference sequence:");
for(i=0; i<n; i++)
scanf("%d",&in[i]);
printf("\nEnter no of frames:");
scanf("%d",&nf);
}
void initialize(){
pgfaultcnt=0;
for(i=0; i<nf; i++)
p[i]=9999;}
int isHit(int data){
hit=0;
for(j=0; j<nf; j++){
if(p[j]==data){
hit=1;
break;
}
}
return hit;
}
void dispPages(){
for (k=0; k<nf; k++){
if(p[k]!=9999)
printf(" %d ",p[k]);
}
}
void dispPgFaultCnt(){
printf("\nTotal no of page faults:%d",pgfaultcnt);}
void lru(){
initialize();
printf("LRU PAGE REPLACEMENT ALGORITHM - SH");
int least[50];
for(i=0; i<n; i++){
printf("\n %d :",in[i]);
if(isHit(in[i])==0){
for(j=0; j<nf; j++){
int pg=p[j];
int found=0;
for(k=i-1; k>=0; k--){
if(pg==in[k]){
least[j]=k;
found=1;
break;
}
else
found=0;
}
if(!found)
least[j]=-9999;
}
int min=9999;
int repindex;
for(j=0; j<nf; j++){
if(least[j]<min){
min=least[j];
repindex=j;
}
}
p[repindex]=in[i];
pgfaultcnt++;
printf("Oops!! It's a MISS ");
dispPages();
}
else
printf("Yay! It's a HIT ");
}
dispPgFaultCnt();
}
int main(){
getData();
lru();
getData();
lru();
}