Thursday, March 6, 2014

Implementation of Banker's algorithm in C++

It is a deadlock avoidance strategy utilized to check where whether the given state of the system having certain maximum request and availability of the resources is safe or if it can lead to a deadlock;

For the Banker's algorithm to work, it needs to know three things:

  • Maximum no. of instances a process is allowed to hold [MAX]
  • Resources each process is currently holding[ALLOCATED]
  • Resources currently available in the system [AVAILABLE]
Either MAX is given or REQUEST is given

if  REQUEST are given  then MAX=allocated+request;

From these [NEED] of each process is calculated.
need=max-allocated

Resources are allocated to a process  if :
  • need≤ available

#include<iostream.h>
#include<conio.h>
void main()
{
int instance[5],count,sequence[10],safe,s=0,j,completed;
int available[5],allocation[10][5],max[10][5];
int need[10][5],process,P[10],countofr,countofp,running[10];
clrscr();
cout<<"\n Enter the number of resources (<=5): ";
cin>> countofr;
for(int i=0;i<countofr;i++)
{  cout<<"\n enter the max instances of  resource R["<<i<<"] :";
   cin>>instance[i];
   available[i]=instance[i];
}
cout<<"\n Enter the number of processes (<=10): ";
cin>> countofp;
cout<<"\n Enter the allocation matrix \n     ";
 
for(i=0;i<countofp;i++)
   { cout<<"FOR THE PROCESS :P["<<i<<"]"<<endl;
     for(int j=0;j<countofr;j++)
    {  cout<<"allocation of resource R["<<j<<"] is : " ;
  cin>>allocation[i][j];
  available[j]-=allocation[i][j];
}
   }
cout<<"\nEnter the MAX matrix \n\n";

    for(i=0;i<countofp;i++)
       { cout<<"FOR THE PROCESS P["<<i<<"]"<<endl;
for(int j=0;j<countofr;j++)
 {   cout<<"max demand of resource R["<<j<<"] is : ";
     cin>>max[i][j];
 }
       }
clrscr();
cout<<"\n the given data are : \n";

cout<<endl<<"\nTotal resources in system : \n\n  ";
for(i=0;i<countofr;i++)
   cout<<" R["<<i<<"]  ";
   cout<<endl;
for(i=0;i<countofr;i++)
   cout<<"     "<<instance[i];

cout<<"\n\n ALLOCATION matrix \n\n\t";
for(j=0;j<countofr;j++)
   cout<<"R["<<j<<"]  ";
   cout<<endl;

for(i=0;i<countofp;i++)
{  cout<<"P["<<i<<"]  ";
   for(j=0;j<countofr;j++)
      cout<<"    "<<allocation[i][j];
   cout<<endl;
 }

 cout<<"\n\n MAX matrix \n\n\t";
for(j=0;j<countofr;j++)
   cout<<"R["<<j<<"]  ";
   cout<<endl;

for(i=0;i<countofp;i++)
{  cout<<"P["<<i<<"]  ";
   for(j=0;j<countofr;j++)
      cout<<"    "<<max[i][j];
   cout<<endl;
 }
    for(i=0;i<countofp;i++)
       { 
for(j=0;j<countofr;j++)
 {   
     need[i][j]=max[i][j]-allocation[i][j];
 }
}

 cout<<"\n\n NEED matrix \n\n\t";
for(j=0;j<countofr;j++)
   cout<<"R["<<j<<"]  ";
   cout<<endl;

for(i=0;i<countofp;i++)
{  cout<<"P["<<i<<"]  ";
   for(j=0;j<countofr;j++)
      cout<<"    "<<need[i][j];
   cout<<endl;
 }

 cout<<"\n NOW to  check whether above state is safe";
 cout<<"\n sequence in which above requests can be fulfilled";
 cout<<"\n press any key to continue";
 getch();

count=countofp;

for(i=0;i<countofp;i++)
    { running[i]=1;}

while(count)
    {   safe=0;
        for(i=0;i<countofp;i++)
  { if(running[i])
      {  completed=1;
 for(j=0;j<countofr;j++)
    {   if(need[i][j]> available[j])
  { completed=0;
    break;
  }
    }
if(completed)
                {
   running[i]=0;
                    count--;
   safe=1;
                    for(j=0;j<countofr;j++) 
                    {
                        available[j]+=allocation[i][j];
                    }
   sequence[s++]=i;
   cout<<"\n\n Running process P["<<i<<"]";
   cout<<endl<<"\n\nTotal resources now available:\n\n";
   for(i=0;i<countofr;i++)
   cout<<" R["<<i<<"]  ";
   cout<<endl;
   for(i=0;i<countofr;i++)
   cout<<"     "<<available[i];
                    break;
                }
            }

        }    
         if(!safe)
         break;
     }

if(safe)
 {
            cout<<"\nThe System is in safe state";
   cout<<"\nSafe sequence is :";
            for(i=0;i<countofp;i++)
            {
                cout<<"\t"<<"P["<<sequence[i]<<"]";
            }
 }
else
 {
   cout<<"\nThe System is in unsafe state";
 }
       getch();
}