⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bankersalgo.cpp

📁 Bankers Algorithm in deadlocks os
💻 CPP
字号:
#include<iostream>
#include<stdio.h>
using namespace std;
int avail[100],need[100][100],alloc[100][100],work[100],finish[100];
int n,m;
int maximum[100][100];
 bool leq(int x[100][100],int *y,int j)
 {
      int k=1,i;
      for(i=1;i<=m;i++)
       {
                         if(x[j][i]>y[i])
                              return false;
       }
       return true;
 }
 bool leq(int *y,int x[100][100],int j)
 {
      int k=1,i;
      for(i=1;i<=m;i++)
       {
                         if(y[i]>x[j][i])
                              return false;
       }
       return true;
 }
 bool leq(int *y,int *x)
 {
      int k=1,i;
      for(i=1;i<=m;i++)
       {
                         if(y[i]>x[i])
                              return false;
       }
       return true;
 }
      
      
  int safe_seq()
  {
       int i,j,k=1,seq[100],flag=1;
          for(i=1;i<=m;i++)
                work[i]=avail[i];
         for(i=1;i<=n;i++)
                 finish[i]=0;
                        
         while(1)
         {
                i=1;
                int f=0;
                while(i<=n && !(finish[i]==0 && leq(need,work,i)))
                    {
                         i++;
                        
                    }
                    
                if(i>n || k>n)
                   break;
                   seq[k]=i;
                   k++;
                for(j=1;j<=m;j++)
                   work[j]+=alloc[i][j];
                finish[i]=1;
         }
                
        //checking here
        for(i=1;i<=n;i++)
          if(finish[i]==0)
                {
                          flag=0;
                          break;
                }                
     
     if(flag==0)
     {
                cout<<endl<<"No safe seq possible"<<endl;
                return 0;
     }
     else
     {
            cout<<endl<<"Safe seq possible :"<<endl;
            for(i=1;i<=n;i++)
                cout<<seq[i]<<" ";
                cout<<endl;
                return 1;
     }
  }
     
int main()
{
    
    int i,j,p,req[100],t;
    char r='y';
    cout<<"No of processes"<<endl;
    cin>>n;
    cout<<"No of resource types"<<endl;
    cin>>m;
    //Input must give the max instances of a resource type a process uses
    //and the instances allocated to the process and those available for a process
    
    //give the allocted instances
    for(i=1;i<=n;i++)
         {
                     cout<<" allocation of resources for process "<<i<<endl;
                     for(j=1;j<=m;j++)
                           cin>>alloc[i][j];
                           
                    cout<<" max resources for process "<<i<<endl;
                     for(j=1;j<=m;j++)
                           cin>>maximum[i][j];
         }
         
         //available resources
         cout<<"Avail resources of each resource type "<<endl;
         for(i=1;i<=m;i++)
            cin>>avail[i];
            cout<<"Need Matrix"<<endl;
         for(i=1;i<=n;i++)
         {
                for(j=1;j<=m;j++)
                  { need[i][j]=maximum[i][j]-alloc[i][j];
                   cout<<need[i][j]<<" ";
                   }
                   cout<<endl;
                   
         }    
         
         safe_seq();  
         
         cout<<"Do you want to request for resources: enter y"<<endl;
         cin>>r;
         while(r=='y')
         {
                      cout<<"enter the requesting process"<<endl;
                      cin>>p;
                      cout<<"enter the resources requested"<<endl;
                      for(i=1;i<=m;i++)
                         cin>>req[i];
                      if(!leq(req,need,p))
                            cout<<"Error : Process cant request more than need"<<endl;
                      else if(!leq(req,avail))
                          {
                                                   cout<<"Process must wait,resources are not available"<<endl;
                          }
                                                   
                      else 
                      {
                              for(i=1;i<=m;i++)
                              {
                                               avail[i]=avail[i]-req[i];
                                               alloc[p][i]=alloc[p][i]+req[i];
                                               need[p][i]=need[p][i]-req[i];
                              }
                              t=safe_seq();
                              if(t==0)
                              {
                                 cout<<"Request leds to an unsafe state,hence process must wait "<<endl;
                                for(i=1;i<=m;i++)
                                  {
                                               avail[i]=avail[i]+req[i];
                                               alloc[p][i]=alloc[p][i]-req[i];
                                               need[p][i]=need[p][i]+req[i];
                                  } 
                                }
                                else
                                {
                                    cout<<"Resources allocated"<<endl;
                                }
                      }   
                      cout<<"Do you want to continue : enter y"<<endl;
                       cin>>r; 
         }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -