FaisalAhemdBijoy

banker's algo

Sep 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int allocation[10][10],available[10],maxmat[10][10],need[10][10],work[10],finish[10],process[10] ;
  5. /*
  6. 5
  7. 4
  8. 0 0 1 2
  9. 1 0 0 0
  10. 1 3 5 4
  11. 0 5 3 2
  12. 0 0 1 4
  13. 0 0 1 2
  14. 1 7 5 0
  15. 2 3 5 6
  16. 0 6 5 2
  17. 0 6 5 6
  18. 1 5 2 0
  19. */
  20. int main()
  21. {
  22.     int n,m,i,j;
  23.     cout<<"Banker's algorithm"<<endl;
  24.     cout<<"Enter The process number"<<endl;
  25.     cin>>n;
  26.     cout<<"Enter The resource number"<<endl;
  27.     cin>>m;
  28.  
  29.     //Allocation matrix
  30.     cout<<"Enter the allocation matrix"<<endl;
  31.     for (i=0; i<n; i++)
  32.     {
  33.         for(j=0; j<m; j++)
  34.         {
  35.             cin>>allocation[i][j];
  36.  
  37.         }
  38.     }
  39.     cout<<"Allocation matrix"<<endl;
  40.     for (i=0; i<n; i++)
  41.     {
  42.         for (j=0; j<m; j++)
  43.         {
  44.             cout<<allocation[i][j]<<" ";
  45.         }
  46.         cout<<endl;
  47.  
  48.     }
  49.  
  50.     //Max matrix
  51.     cout<<"Enter the Max matrix"<<endl;
  52.     for (i=0; i<n; i++)
  53.     {
  54.         for(j=0; j<m; j++)
  55.         {
  56.             cin>>maxmat[i][j];
  57.  
  58.         }
  59.     }
  60.     cout<<"Max  matrix"<<endl;
  61.     for (i=0; i<n; i++)
  62.     {
  63.         for (j=0; j<m; j++)
  64.         {
  65.             cout<<maxmat[i][j]<<" ";
  66.         }
  67.         cout<<endl;
  68.  
  69.     }
  70.     //Need Matrix calculation
  71.     for(i=0; i<n; i++)
  72.     {
  73.         for(j=0; j<m; j++)
  74.         {
  75.             //Need=Max-allocation
  76.             need[i][j]= maxmat[i][j]-allocation[i][j];
  77.         }
  78.     }
  79.     cout<<"Need  matrix"<<endl;
  80.     for (i=0; i<n; i++)
  81.     {
  82.         for (j=0; j<m; j++)
  83.         {
  84.             cout<<need[i][j]<<" ";
  85.         }
  86.         cout<<endl;
  87.  
  88.     }
  89.     //Available Matrix
  90.     cout<<"Enter the available matrix "<<endl;
  91.     for (i=0; i<m; i++)
  92.     {
  93.         cin>>available[i];
  94.  
  95.     }
  96.     cout<<"Available matrix "<<endl;
  97.     for(i=0; i<m; i++)
  98.     {
  99.         cout<<available[i]<<endl;
  100.  
  101.     }
  102.     //work matrix calculation
  103.     for(i=0; i<m; i++)
  104.     {
  105.         work[i]=available[i];
  106.     }
  107.     int k=0;
  108.     int idx=0;
  109.     int sum=0;
  110.     for(i=0;sum<n; i++)
  111.     {
  112.         i%=n;
  113.         if(finish[i]==0)
  114.         {
  115.             bool nibo=1;
  116.             for(j=0; j<m; j++)
  117.             {
  118.  
  119.                 if(need[i][j]>work[j])
  120.                 {
  121.                     nibo=0;
  122.                     break;
  123.                 }
  124.             }
  125.             if(nibo){
  126.                 sum++;
  127.                 process[idx]=i;
  128.                 idx++;
  129.                 for(j=0;j<m;j++){
  130.                     work[j]+=allocation[i][j];
  131.                 }
  132.                 finish[i]=1;
  133.             }
  134.         }
  135.     }
  136.     cout<<"Final work matrix"<<endl;
  137.     for (i=0; i<m; i++)
  138.     {
  139.         cout<<work[i]<<" ";
  140.     }
  141.     cout<<endl;
  142.  
  143.     cout<<"Sequence process matrix "<<endl;
  144.     for(i=0; i<n; i++)
  145.     {
  146.         cout<<process[i]<<endl;
  147.     }
  148. }
Add Comment
Please, Sign In to add comment