Sunday, June 12, 2011

BANKER'S ALGORITHM TO DETECT DEADLOCKS


#include<iostream.h>
#include<conio.h>
#define MAX 5
class bankers
{
 private:
 int al[MAX][MAX],m[MAX][MAX],n[MAX][MAX],avail[MAX];
 int nop,nor,k,result[MAX],pnum,work[MAX],finish[MAX];
 public:
 bankers();
 void input();
 void method();
 int search(int);
 void display();
};



bankers::bankers()
{
 k=0;
 for(int i=0;i<MAX;i++)
 {
 for(int j=0;j<MAX;j++)
 {
  al[i][j]=0;
  m[i][j]=0;
  n[i][j]=0;
 }
 avail[i]=0;
 result[i]=0;
 finish[i]=0;
 }
}


void bankers::input()
{
 int i,j;
 cout<<"Enter the number of processes:";
 cin>>nop;
 cout<<"Enter the number of resources:";
 cin>>nor;
 cout<<"Enter the allocated resources for each process: "<<endl;
 for(i=0;i<nop;i++)
 {
  cout<<"\nProcess "<<i;
  for(j=0;j<nor;j++)
  {
   cout<<"\nResource "<<j<<":";
   cin>>al[i][j];
  }
 }
 cout<<"Enter the maximum resources that are needed for each process: "<<endl;
 for(i=0;i<nop;i++)
 {
  cout<<"\nProcess "<<i;
  for(j=0;j<nor;j++)
  {
   cout<<"\nResouce "<<j<<":";
   cin>>m[i][j];
   n[i][j]=m[i][j]-al[i][j];
  }
 }
 cout<<"Enter the currently available resources in the system: ";
 for(j=0;j<nor;j++)
 {
  cout<<"Resource "<<j<<":";
  cin>>avail[j];
  work[j]=-1;
 }
 for(i=0;i<nop;i++)
 finish[i]=0;
}


void bankers::method()
{
 int i=0,j,flag;
 while(1)
 {
  if(finish[i]==0)
  {
   pnum =search(i);
   if(pnum!=-1)
   {
    result[k++]=i;
    finish[i]=1;
    for(j=0;j<nor;j++)
    {
     avail[j]=avail[j]+al[i][j];
    }
   }
  }
  i++;
  if(i==nop)
  {
   flag=0;
   for(j=0;j<nor;j++)
   if(avail[j]!=work[j])
   flag=1;
   for(j=0;j<nor;j++)
   work[j]=avail[j];
   if(flag==0)
   break;
   else
   i=0;
  }
 }
}


int bankers::search(int i)
{
 int j;
 for(j=0;j<nor;j++)
 if(n[i][j]>avail[j])
 return -1;
 return 0;
}


void bankers::display()
{
 int i,j;
 cout<<endl<<"OUTPUT:";
 cout<<endl<<"========";
 cout<<endl<<"PROCESS\t     ALLOTED\t     \tMAXIMUM\t     \tNEED";
 for(i=0;i<nop;i++)
 {
  cout<<"\nP"<<i<<"\t     ";
  for(j=0;j<nor;j++)
   cout<<al[i][j]<<"  ";
  cout<<"\t     ";
  for (j=0;j<nor;j++)
  {
   cout<<m[i][j]<<"  ";
  }
  cout<<"\t     ";
  for(j=0;j<nor;j++ )
  {
   cout<<n[i][j]<<"  ";
  }
 }
 cout<<"\nThe sequence of the safe processes are: \n";
 for(i=0;i<k;i++)
 {
  int temp = result[i]+1 ;
  cout<<"P"<<temp<<" ";
 }
 int flg=0;
 /*cout<<"\nThe sequence of unsafe processes are: \n";
 int flg=0;
 for (i=0;i<nop;i++)
 {
  if(finish[i]==0)
  {
   flg=1;
  }
  cout<<"P"<<i<<" ";
 } */
 cout<<endl<<"RESULT:";
 cout<<endl<<"=======";
 if(flg==1)
 cout<<endl<<"The system is not in safe state and deadlock may occur!!";
 else
 cout<<endl<<"The system is in safe state and deadlock will not occur!!";
}


int main()
{
 clrscr();
 cout<<" DEADLOCK BANKER'S ALGORITHM "<<endl;
 bankers B;
 B.input ( );
 B.method ( );
 B.display ( );
 getch ( );
 return 0;
}

No comments:

Post a Comment