Saturday, April 5, 2014

Next Fit Memory Management : C++

// NextFitMemMan.cpp

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

using namespace std;

void main()
{
//INFORMATION ABOUT THE PROCESSES
cout<<"Enter the number of processes: ";
int nProc = 0;
cin>>nProc;

cout<<"Enter the size of each process: ";
int data[10][3]; //we assume at most 10 processes

for(int i = 0;i<nProc;++i)
{
cin>>data[i][0]; //size of the process
data[i][1] = 0;  //0 means unallocated
data[i][2] = i;  //id of the process
}

//INFORMATION ABOUT THE MEMORY BLOCKS
cout<<"Enter the number of memory blocks: ";
int nBlocks = 0;
cin>>nBlocks;

cout<<"Enter the size of each block: ";
int dataBlocks[10][2];

for(int i =0;i<nProc;++i)
{
cin>>dataBlocks[i][0];
dataBlocks[i][1] = 0; //0 means unallocated
}

//ALLOCATION TO BE DONE HERE
int j = 0;
for(int i = 0;i<nProc;++i)
{
for(;j<nBlocks;++j)
{
if(dataBlocks[j][1]==0&&data[i][0]<=dataBlocks[j][0])
{
data[i][1] = 1; //allocated
dataBlocks[j][1] = 1; //allocated
cout<<"Process id "<<i<<" allocated to memory block: "<<j<<endl;
break;
}
}
}

for(int i = 0;i<nBlocks;++i)
{
if(data[i][1]==0)
{
cout<<"Process id "<<i<<" unallocated\n";
}
}
}

No comments:

Post a Comment