-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpageReplacement.c
147 lines (141 loc) · 3.2 KB
/
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<stdio.h>
int in(int size, int * array, int value){
int i;
for(i=0;i<size;i++){
if(array[i] == value)
return 1;
}
return 0;
}
int closestIndex(int size, int currentLocation, int * array, int value, int direction){
int i=currentLocation;
if(direction==-1){
for(i--;i>=0;i--){
if(array[i] == value)
return i;
}
return i;
}
else{
for(i++;i<size;i++){
if(array[i]==value){
return i;
}
}
return i;
}
}
int extreme(int size, int * array, int type){
int extreme = 0;
if(type==1){//max
int i=0;
int max=array[0];
for(i++;i<size;i++){
if(array[i] > max){
max = array[i];
extreme = i;
}
}
return extreme;
}
else{
int i=0;
int min=array[0];
for(i++;i<size;i++){
if(array[i] < min){
min = array[i];
extreme = i;
}
}
return extreme;
}
}
void printArray(int size, int * array){
int i=0;
for(;i<size;i++){
printf("%d ", array[i]);
}
printf("\n");
}
int main(){
int n;
printf("Enter number of page requests: ");
scanf("%d", &n);
int m;
printf("Enter number of pages: ");
scanf("%d", &m);
int currPages[m];
int pageRequests[n];
printf("Enter the page requests separated by space: \n");
int i;
for(i=0;i<n;i++) scanf("%d", &pageRequests[i]);
int faults=0;
int index=-1;
for(i=0;i<m;i++) currPages[i] = -1;
for(i=0;i<n;i++){
if(in(m, currPages, pageRequests[i])) continue;
else{
currPages[(++index)%m] = pageRequests[i];
faults++;
}
}
printf("FIFO:\n");
printf("\tPage faults : %d\n", faults);
printf("\t Hits : %d\n", n - faults);
printf("\t Hit Ratio : %.2f%%\n", (n-faults)*100.0/n);
for(i=0;i<m;i++) currPages[i] = -1;
int nextIndex[m];
int filledBlocks = 0;
faults = 0;
index = -1;
for(i=0;i<n;i++){
if(in(filledBlocks, currPages, pageRequests[i])) continue;
else{
if(filledBlocks == m){
int j=0;
for(;j<m;j++){
nextIndex[j] = closestIndex(n, i, pageRequests , currPages[j], 1);
}
int farest = extreme(m, nextIndex, 1);
currPages[farest] = pageRequests[i];
}
else{
currPages[(++index)%m] = pageRequests[i];
filledBlocks++;
}
faults++;
}
}
printf("\nOptimal: \n");
printf("\tPage faults : %d\n", faults);
printf("\t Hits : %d\n", n - faults);
printf("\t Hit Ratio : %.2f%%\n", (n-faults)*100.0/n);
faults = 0;
int prevIndex[m];
filledBlocks = 0;
index = -1;
for(i=0;i<m;i++) currPages[i] = -1;
for(i=0;i<n;i++){
if(in(m, currPages, pageRequests[i])) continue;
else{
if(filledBlocks == m){
int j=0;
for(;j<m;j++){
prevIndex[j] = closestIndex(n, i, pageRequests, currPages[j], -1);
}
int farest = extreme(m, prevIndex, -1);
currPages[farest] = pageRequests[i];
}
else{
currPages[(++index)%m] = pageRequests[i];
filledBlocks++;
}
faults++;
}
}
printf("\nLRU: \n");
printf("\tPage faults : %d\n", faults);
printf("\t Hits : %d\n", n - faults);
printf("\t Hit Ratio : %.2f%%\n", (n-faults)*100.0/n);
printf("\n");
}