-
Notifications
You must be signed in to change notification settings - Fork 12
/
Optimal_PageReplacement.c
99 lines (93 loc) · 2.44 KB
/
Optimal_PageReplacement.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
87
88
89
90
91
92
93
94
95
96
97
98
99
//OPTIMAL PAGE REPLACEMENT
#include<stdio.h>
#include<stdlib.h>
//7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
//4 7 6 1 7 6 1 2 7 2
//7 0 1 2 0 3 0 4 2 3 0 3 2 3
int findOPTIMAL(int occur[],int fr[],int rs[],int f,int n,int r){
int flag;
for(int i=0;i<f;i++){
flag=0;
for(int j=r+1;j<n;j++){
if(fr[i]==rs[j]){
occur[i]=j;
flag=1;
break;
}
}
if(flag!=1 && fr[i]!=-1){
occur[i]=100;
}
}
int max=occur[0],pos=0,pos1,pos2;
for(int i=0;i<f;i++){
if(occur[i]>max){
max=occur[i];
pos=i;
}
else if(occur[i]==max){
pos1-1;pos2=-1;
for(int j=0;j<n;j++){
if(rs[j]==fr[i]){
pos1=j;
}
if(rs[j]==fr[pos]){
pos2=j;
}
if(pos1!=-1 && pos2!=-1){
break;
}
}
if(pos1<pos2){
pos=i;
}
}
}
return pos;
}
void main(){
int n,f,pf=0,hits=0;
printf("Enter the no: of pages in reference string : ");
scanf("%d",&n);
int rs[n];
printf("\nEnter the reference string : ");
for(int i=0;i<n;i++){
scanf("%d",&rs[i]);
}
printf("\nEnter the no: of frames : ");
scanf("%d",&f);
int fr[f],occur[f];
for(int i=0;i<f;i++){
fr[i]=-1;
occur[i]=1000;
}
printf("\nOPTIMAL PAGE REPLACEMENT....\n\n");
int pos,flag;
for(int i=0;i<n;i++){
flag=0;
for(int j=0;j<f;j++){
if(rs[i]==fr[j]){
flag=1;
hits++;
printf("\t\t--------------------------------------------------------------HIT\n\n");
break;
}
}
if(flag!=1){
pos=findOPTIMAL(occur,fr,rs,f,n,i);
fr[pos]=rs[i];
pf++;
for(int k=0;k<f;k++){
if(fr[k]==-1){
printf("\t\t");
}
else{
printf("\t\t%d",fr[k]);
}
}
printf("\t\tPAGEFAULT\n\n");
}
}
printf("\nThe no: of page faults = %d.",pf);
printf("\nThe no: of hits = %d.\n",hits);
}