# Banker's Algorithm

Discussion in 'C++' started by c++, Nov 14, 2004.

1. ### c++Guest

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;
cout<<"\nHOW MANY UNIT REQUEST THE P"<<i<<" PROCESS? ";
cin>>R;
cout<<"\nWHAT IS THE MAX NUMBER OF UNITS OF THE P"<<i<<"
PROCESS? ";
cin>>M;
cout<<"\n\n";
}

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

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

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

for(i=0; i<P; i++)
MAX=-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!=-1)
cout<<"\nTHE PROCESS NUMBER "<<i<<" IS THE "<<CP<<"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

c++, Nov 14, 2004