Sunday, April 6, 2014

FIFO Page Replacement : C++ (v2)

// FIFO Page Replacementv2.cpp

#include "stdafx.h"
#include <iostream>

using namespace std;

void main()
{
cout<<"Enter the number of page frames: ";
int nFrames = 0;
cin>>nFrames;

int frame[10];
for(int i =0;i<nFrames;++i)
{
frame[i]=-1;
}

cout<<"Enter the number of elements in page address stream: ";
int nStream=0;
cin>>nStream;

cout<<"Input the page address stream: ";
int stream[50];

for(int i = 0;i<nStream; ++i)
{
cin>>stream[i];
}

cout<<endl;
cout<<"Page address stream\t\tPage Frames\n";
cout<<"        ";
for(int i =0;i<nFrames;++i)
{
cout<<i<<"\t";
}
cout<<endl<<endl;

//Allocation to be done here
int point = 0;
int unallocated=-1;
for(int i =0;i<nStream;++i)
{
if(frame[point]==-1)
{
for(int j=0;j<nFrames;++j)
{
if(frame[j]==stream[i])
{

cout<<stream[i]<<"        ";
for(int k=0;k<nFrames;++k)
{
cout<<frame[k]<<"\t";
}
cout<<endl;

break;
}
else if(frame[j]!=stream[i])
{
frame[point]=stream[i];
cout<<stream[i]<<"        ";
for(int k=0;k<nFrames;++k)
{
cout<<frame[k]<<"\t";
}
cout<<endl;
point = (point+1)%nFrames;
break;
}
}
}
else
{
int flag=1;
for(int j =0;j<nFrames;++j)
{
if(frame[j]==stream[i])
{
cout<<stream[i]<<"        ";
for(int k=0;k<nFrames;++k)
{
cout<<frame[k]<<"\t";
}
cout<<endl;
flag = 0;
break;
}
}
if(flag)
{
frame[point]=stream[i];
cout<<stream[i]<<"        ";
for(int k=0;k<nFrames;++k)
{
cout<<frame[k]<<"\t";
}
cout<<endl;
point=(point+1)%nFrames;
}
}
}


for(int k=0;k<nFrames;++k)
{
cout<<"Frame #"<<k<<" contains "<<frame[k]<<endl;
}
}

No comments:

Post a Comment