-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondChance.cpp
More file actions
97 lines (82 loc) · 1.72 KB
/
Copy pathSecondChance.cpp
File metadata and controls
97 lines (82 loc) · 1.72 KB
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
#include<bits/stdc++.h>
using namespace std;
int present(int a[],int nf,int page,int rf_bit[])
{
for(int i=0;i<nf;i++)
{
if(a[i]==page)
{
int temp = a[i];
for(int k=i;k<nf-1;k++){
a[k]=a[k+1];
}
a[nf-1]= temp;
rf_bit[i]=1;
return 1;
}
}
return 0;
}
void printtable(int table_frame[], int nf)
{
for(int i=0; i<nf; i++)
{
if(table_frame[i] == -1)
printf("-- ");
else
printf("%2d ", table_frame[i]);
}
printf("||");
}
int main()
{
int nf;
cout<<"enter number of frames"<<endl;
cin>>nf;
int a[nf],rf_bit[nf];
for(int i=0;i<nf;i++)
{
a[i]=-1;
rf_bit[i]=0;
}
int np;
cout<<"enter number of pages"<<endl;
cin>>np;
int b[np];
cout<<"enter page requests"<<endl;
for(int i=0;i<np;i++)
{
cin>>b[i];
}
int pos=0,count1=0;
for(int i=0;i<np;i++)
{
printf("page table after request from %2d || ",b[i]);
if(!present(a,nf,b[i],rf_bit))
{
//int pos=findpos(a,nf,b,i,np);
while(rf_bit[pos]==1)
{
rf_bit[pos]=0;
pos=(pos+1)%nf;
}
if(pos>=nf){
for(int j=0;j<nf-1;j++){
a[j]=a[j+1];
}
a[nf-1]=b[i];
}
else{
a[pos]=b[i];
}
printtable(a,nf);
printf("page fault\n");
count1++;
pos++;
continue;
}
printtable(a,nf);
printf("\n");
}
printf("\nNumber of page faults : %d\n\n", count1);
}