Sunday, March 23, 2014

FIFO Page Replacement Code (Memory Management) : C++

#include "stdafx.h"
#include <iostream>
/* Warning: Write Only Code : Don't try to understand */
using namespace std;

class Input
{
public:
Input()
{
pageStream = new int[100];
elementsInStream = 0;

cout<<"Number of page frames to be allocated to this process: ";
cin>>nFrames;
pageFrames = new int[nFrames];
for(int i =0;i<nFrames;++i)
{
pageFrames[i] = 0; //need to set explicity, otherwise garbage values prevail
}

getInput();
}

void getInput()
{

cout<<"\nEnter the number of page addresses in stream: ";
cin>>elementsInStream;
cout<<"\nInput Page Address Stream: ";
for(int i =0;i<elementsInStream;++i)
{
cin>>pageStream[i];
}
}

protected:
int *pageStream;
int nFrames;
int *pageFrames;
int elementsInStream;
};

class Allocator:public Input
{
public:
Allocator()
{
age = new int[nFrames];
for(int i= 0;i<nFrames;++i)
age[i]=0;

computation = new int * [elementsInStream];

for(int i = 0;i<elementsInStream;++i)
{
computation[i] = new int[nFrames];
for(int j = 0;j<nFrames;++j)
computation[i][j] = 0;
}

}

bool alreadyPresent(int pageNumber)
{
for(int i =0;i<nFrames;++i)
{
if(pageFrames[i]==pageNumber)
{
return true;
}
}
return false;
}

void allocate()
{

int pageNumber=-1;

int j = 0; //j variable for intialization of blocks (with value 0) in the start

for(int i = 0;i<elementsInStream;++i)
{
pageNumber = pageStream[i];

if(pageFrames[j]==0)
{
if(alreadyPresent(pageNumber))
{
for(int k=0;k<j;++k) //increment the age of previously allocated frames
{
age[k]++;
}

updateComputation(0, 0);
}
else
{
pageFrames[j] = pageNumber;
for(int k=0;k<=j;++k) //increment the age of all allocated frames till now
{
age[k]++;
}
updateComputation(j, pageNumber);
j++;
if(j==nFrames)
j=0; //no page will have number 0 so the check if(pageFrames[j]==0) will be false

}
}

else if(alreadyPresent(pageNumber))
{
for(int i =0;i<nFrames;++i)
age[i]++;

updateComputation(0, 0);
continue;
}

else
{
int toReplace = replaceLongest();
pageFrames[toReplace] = pageNumber;
for(int i =0;i<nFrames;++i)
{
if(i==toReplace)
{
age[toReplace] = 1;
continue;
}
age[i]++;
}

updateComputation(toReplace, pageNumber);
}
}
}

int replaceLongest()
{
int maxAge = -1;
int toReturn = -11; //program halts if incorrect
for(int i =0;i<nFrames;++i)
{
if(age[i]>maxAge)
{
maxAge = age[i];
toReturn = i;
}
}
return toReturn;
}

void updateComputation(int position, int value)
{
static int number = 0;

for(int i = 0;i<nFrames;++i)
{
if(number!=0)
computation[number][i] = computation[number-1][i];
}

if(value!=0) //we want the new column to just be a copy, we don't want any change
computation[number][position] = value;
number++;
}

void display()
{
cout<<endl;
for(int i = 0;i<nFrames;++i)
cout<<pageFrames[i]<<endl;
cout<<endl;
}

void displayComputation()
{
cout<<"\nPage Replacement : First in first out behaviour ->\n";
cout<<endl;
for(int k=0;k<nFrames;++k)
{
cout<<"Frame #"<<k<<" states: ";
for(int i = 0;i<elementsInStream;++i)
{
cout<<computation[i][k]<<" ";
}
cout<<endl<<endl;
}
}

void displayAge()
{
cout<<endl;
for(int i =0;i<nFrames;++i)
cout<<age[i]<<endl;
cout<<endl;
}

private:
int *age;
int **computation;
};

int main()
{
Allocator allocator;

allocator.allocate();
allocator.displayComputation();
return NULL;
}

No comments:

Post a Comment