-
Notifications
You must be signed in to change notification settings - Fork 6
/
Least_recently_used_replacement.cpp
52 lines (51 loc) · 1.08 KB
/
Least_recently_used_replacement.cpp
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
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
int p[]={1,3,0,3,5,6,3};
int f=3;
int ans[3]={-1,-1,-1};
int x=0,ctr=0;
int lru=-1;
for(int k=0;k<7;k++){
int flag=0;
for(int i=0;i<f;i++){
if(p[k]==ans[i]){
flag=1;
lru=i;
}
}
if(x==3){
x=0;
}
if(flag==0){
cout<<endl<<p[k]<<" -- ";
if(x==lru){
x++;
if(x==3){
x=0;
}
}
ans[x]=p[k];
x++;
ctr++;
}
if(flag==1){
cout<<endl<<p[k]<<" -- ";
}
if(flag==1){
cout<<"No Page Fault";
}
if(flag==0){
for(int i=0;i<f;i++){
if(ans[i]==-1){
cout<<" ";
}
else{
cout<<ans[i]<<" ";
}
}
}
}
cout<<"\nTotal page faults: "<<ctr<<endl;
return 0;
}