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