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