Velocity Reviews > C++ > Banker's Algorithm

# Banker's Algorithm

c++
Guest
Posts: n/a

 11-14-2004
I wrote this code that should tell to the user who are the best
processes to release a unit of a resource using the banker's algorithm

#include<iostream>
#include<conio>
#include<stdio>
#include<stdlib>

void main()
{

const int max = 10; //max multiplicity
int i, j, z; //counters
int P, L, N; //P=
processes, L= FREE, N= maximum multiplity
int A[max], R[max], M[max]; //A= allocates, R=
request, M= maximum requests
int RS[max]; //RS= folowing request
int LS[max]; //free resurces after the i-
allocation
int CP[max]={0}; //CP= processes classification
int MAX[max];
int lim; //max limit

do
{
cout<<"\HOW MANY PROCESSES DO YOU WANT? ";
cin>>P;
}while(P>max);

cout<<"\nINSERT THE MULTEPLICITY OF THE RESOURCE: ";
cin>>N;

for(i=0;i<P;i++)
{
cout<<"\nHOW MANY UNIT HAS THE P"<<i<<" PROCESS? ";
cin>>A[i];
cout<<"\nHOW MANY UNIT REQUEST THE P"<<i<<" PROCESS? ";
cin>>R[i];
cout<<"\nWHAT IS THE MAX NUMBER OF UNITS OF THE P"<<i<<"
PROCESS? ";
cin>>M[i];
cout<<"\n\n";
}

L=N;
for(i=0; i<P; i++)
L-=A[i]; //nuber of units still idle

for(i=0; i<P; i++)
{
if(R[i]<=L) //is secure?
{
RS[i]=M[i]-A[i]-R[i]; //next request
LS[i]=L-R[i]; //idle resuorces after the allocation

}
else //isn't secure
{
LS[i]=-1;
}
}

for(i=0; i<P; i++)
MAX[i]=-1; //or 0???

z=0;
for(i=0; i<P; i++)
{
if(i==0)
{
for(j=0; j<P; j++)
{
if(LS[j]>=MAX[z])
{
MAX[z]=LS[j];
CP[z]=j;

lim=MAX[z];
}
}
}
else
{
for(j=0; j<P; j++)
{
if((LS[j]>=MAX[z])&&(LS[j]<lim))
{
MAX[z]=LS[j];
CP[z]=j;

lim=MAX[z];
}
}
}

z++;
}

for(i=0; i<P; i++)
{
if(LS[i]!=-1)
cout<<"\nTHE PROCESS NUMBER "<<i<<" IS THE "<<CP[i]<<"st
CHOICE\n";
else
cout<<"THE PROCESS NUMBER "<<i<<" IS NOT SECURE";
}

getch();
}

------------------

The program doesn't do what it shuld do: it doesn't putthe processes in
the right order.

I don't know how to wirte the piece of code that tell to the user that
the best choise is the precess that has the bigger value of LS[]

Could anyone tell me what I should write?

--
Questa è una firma automatica di MesNews.
Sito: http://www.mesnews.net