3
1

Made some minor changes to B7 (page replacement in C++) code.

- Changed variable names
- Indentation and stuff
- Added description
This commit is contained in:
K 2024-11-05 16:42:49 +05:30
parent 3e21e023a1
commit 9296a4bd14
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

View File

@ -1,136 +1,135 @@
#include<iostream> // Assignment-B7 - Page Replacement in C++ (LRU + Optimal)
// BEGINNING OF CODE
#include <cmath>
#include <complex>
#include <iostream>
using namespace std; using namespace std;
int refstring[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1}; int refString[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1};
int win, hit = 0, miss = 0, res[4], lastUsed[40]; int window = 0, hit = 0, miss = 0, currentPages[4], lastUsed[40];
int len = sizeof(refString) / sizeof(refString[0]);
int findLRU() { int findLRU() {
int minIndex = 0; int min_index = 0;
for(int i = 1; i < win; i++){ for (int i=1; i<window; i++) {
if(lastUsed[i] < lastUsed[minIndex]){ if (lastUsed[i] < lastUsed[min_index]) {
minIndex = i; min_index = i;
}
} }
return minIndex; }
return min_index;
} }
void lru(int rf[], int len) void LRU() {
{ for (int i=0; i<window; i++) {
for(int i = 0; i < win; i++) { currentPages[i] = -1;
res[i] = -1; lastUsed[i] = -1;
lastUsed[i] = -1; }
}
for(int j = 0; j < len; j++) { for (int i=0; i<len; i++) {
bool hitFlag = false; bool hitFlag = false;
for(int k = 0; k < win; k++){ for (int j=0; j<window; j++) {
if (res[k] == rf[j]){ if (currentPages[j] == refString[i]) {
hit++; hit++;
lastUsed[k] = j; hitFlag = true;
hitFlag = true; lastUsed[j] = i;
break; break;
} }
}
if (!hitFlag) {
miss++;
bool emptyFound = false;
for(int k = 0; k < win; k++){
if (res[k] == -1){
res[k] = rf[j];
lastUsed[k] = j;
emptyFound = true;
break;
}
}
if(!emptyFound){
int lruIndex = findLRU();
res[lruIndex] = rf[j];
lastUsed[lruIndex] = j;
}
}
} }
if (hitFlag == false) {
cout<<"\nNo of hits (LRU): "<<hit; miss++;
cout<<"\nNo of misses (LRU): "<<miss; bool emptyFound = false;
for (int j=0; j<window; j++) {
if (currentPages[j] == -1) {
emptyFound = true;
currentPages[j] = refString[i];
lastUsed[j] = i;
break;
}
}
if (emptyFound == false) {
int lru_index = findLRU();
currentPages[lru_index] = refString[i];
lastUsed[lru_index] = i;
}
}
}
cout<<endl<<"----- LRU ------";
cout<<endl<<"Number of hits: "<<hit;
cout<<endl<<"Number of misses: "<<miss;
} }
int findOptimal(int rf[], int len, int currentIndex) int findOptimal(int current_index) {
{ int max_index = -1, farthest = current_index;
int maxIndex = -1, farthest = currentIndex; for (int i=0; i<window; i++) {
for(int i = 0; i < win; i++){ int j;
int j; for (j=current_index+1; j<len; j++) {
for(j = currentIndex + 1; j < len; j++){ if (currentPages[i] == refString[j]) {
if(res[i] == rf[j]){ if (j > farthest) {
if(j > farthest){ farthest = j;
farthest = j; max_index = i;
maxIndex = i;
}
break;
}
} }
if(j == len) break;
return i; }
} }
return maxIndex; if (j == len) {
return i;
}
}
return max_index;
} }
void optimal(int rf[], int len){ void Optimal() {
hit = 0; hit = 0;
miss = 0; miss = 0;
for(int i = 0; i < win; i++){ for (int i=0; i<window; i++) {
res[i] = -1; currentPages[i] = -1;
}
for (int i=0; i<len; i++) {
bool hitFlag = false;
for (int j=0; j<window; j++) {
if (currentPages[j] == refString[i]) {
hit++;
hitFlag = true;
break;
}
} }
if (hitFlag == false) {
for(int j = 0; j < len; j++){ miss++;
bool hitFlag = false; bool emptyFound = false;
for(int k = 0; k < win; k++){ for (int j=0; j<window; j++) {
if (res[k] == rf[j]){ if (currentPages[j] == -1) {
hit++; emptyFound = true;
hitFlag = true; currentPages[j] = refString[i];
break; break;
}
}
if (!hitFlag){
miss++;
bool emptyFound = false;
for(int k = 0; k < win; k++){
if (res[k] == -1){
res[k] = rf[j];
emptyFound = true;
break;
}
}
if(!emptyFound){
int optimalIndex = findOptimal(rf, len, j);
res[optimalIndex] = rf[j];
}
} }
}
if (emptyFound == false) {
int optimal_index = findOptimal(i);
currentPages[optimal_index] = refString[i];
}
} }
}
cout<<"\nNo of hits (Optimal): "<<hit; cout<<endl<<"----- OPTIMAL -----";
cout<<"\nNo of misses (Optimal): "<<miss; cout<<endl<<"Number of hits: "<<hit;
cout<<endl<<"Number of misses: "<<miss;
} }
void display() void display() {
{ cout<<endl<<"Current pages are:\t";
cout<<"\nPages in window are: \n"; for (int i=0; i<window; i++) {
for(int i = 0; i < win; i++){ cout<<currentPages[i]<<" ";
cout<<res[i]<<"\n"; }
} cout<<endl;
} }
int main() int main() {
{ cout<<endl<<"Enter window size:\t";
cout<<"\nEnter window size: "; cin>>window;
cin>>win; LRU();
int len = sizeof(refstring)/sizeof(refstring[0]); display();
Optimal();
cout<<"\nBy LRU algorithm: "; display();
lru(refstring, len);
display();
cout<<"\n\nBy Optimal Algorithm: ";
optimal(refstring, len);
display();
} }
// END OF CODE