【OS课程设计四】银行家算法

一、设计目的

1、了解多道系统中多个进程并发执行时的资源分配情况
2、掌握死锁产生的原因、必要条件和处理死锁的基本方法
3、掌握银行家算法,了解资源在进程并发执行中的分配策略
二、设计要求
设计一个n个并发进程共享m个资源的银行家算法的模拟实现。要求具有:
(1) 简单的交互界面
(2) 能显示当前系统资源的剩余情况和占用情况
(3) 能输入每个进程的最大资源要求
模拟利用银行家算法为进程的若干次资源请求分配资源
(4) 输入本次资源要求;
(5) 按银行家算法为进程分配资源,本次分配是否成功要显示出来(要能处理各种情况:可以满足这次请求、由于资源不够不能满足这次请求、由于可能产生不安全不能满足这次请求、请求不合理拒绝请求等)
(6) 作业撤销时要回收资源

Banker_Algorithm.h

Code:
  1. #ifndef _BANKER_ALGORITHM_H_   
  2. #define _BANKER_ALGORITHM_H_   
  3.        
  4.     class Banker_Algorithm   
  5.     {   
  6.     public:   
  7.         Banker_Algorithm(unsigned int pn,unsigned int rn);   
  8.         ~Banker_Algorithm();   
  9.         void init_Need();   
  10.         void request_Resource(unsigned int proc_no,unsigned int request_res[]);   
  11.         void callback(unsigned int proc_no);   
  12.         bool is_Safe();   
  13.         void disp_Available();   
  14.         void disp_Allocation();   
  15.            
  16.         unsigned int **get_Max()   
  17.         {   
  18.             return max;   
  19.         }   
  20.            
  21.         unsigned int *get_Available()   
  22.         {   
  23.             return available;   
  24.         }   
  25.            
  26.     private:   
  27.         Banker_Algorithm(){}   
  28.         unsigned int proc_num;   
  29.         unsigned int resource_num;   
  30.         unsigned int **max;   
  31.         unsigned int **allocation;   
  32.         unsigned int **need;   
  33.         unsigned int *available;   
  34.     };   
  35.   
  36. #endif   

Banker_Algorithm.cpp

Code:
  1. #include "Banker_Algorithm.h"   
  2. #include <iostream>   
  3.   
  4. using std::cerr;   
  5. using std::cout;   
  6. using std::endl;   
  7.   
  8. Banker_Algorithm::Banker_Algorithm(unsigned int pn,unsigned int rn):proc_num(pn),resource_num(rn)   
  9. {   
  10.     unsigned int i,j;   
  11.     max = new unsigned int*[pn];   
  12.     allocation = new unsigned int*[pn];   
  13.     need = new unsigned int*[pn];   
  14.     available = new unsigned int[rn];   
  15.        
  16.     for(i = 0;i < proc_num;i++)   
  17.     {   
  18.         max[i] = new unsigned int[resource_num];   
  19.         allocation[i] = new unsigned int[resource_num];   
  20.         need[i] = new unsigned int[resource_num];   
  21.            
  22.         for(j = 0;j < resource_num;j++)   
  23.         {   
  24.             allocation[i][j] = 0;   
  25.         }   
  26.     }   
  27. }   
  28.   
  29. Banker_Algorithm::~Banker_Algorithm()   
  30. {   
  31.     unsigned i;   
  32.        
  33.     delete []available;   
  34.        
  35.     for(i = 0;i < proc_num;i++)   
  36.     {   
  37.         delete []need[i];   
  38.         delete []allocation[i];   
  39.         delete []max[i];   
  40.     }   
  41.        
  42.     delete need;   
  43.     delete allocation;   
  44.     delete max;   
  45. }   
  46.   
  47. void Banker_Algorithm::init_Need()   
  48. {   
  49.     unsigned int i,j;   
  50.     for(i = 0;i < proc_num;i++)   
  51.     {   
  52.         for(j = 0;j < resource_num;j++)   
  53.         {   
  54.             need[i][j] = max[i][j];    
  55.         }   
  56.     }   
  57. }   
  58.   
  59. void Banker_Algorithm::request_Resource(unsigned int proc_no,unsigned int request_res[])   
  60. {   
  61.     unsigned int i;   
  62.        
  63.     if(proc_no > proc_num)   
  64.     {   
  65.         cerr<<"WRONG PROCESS NO,REQUEST FAILED"<<endl;   
  66.         return;   
  67.     }   
  68.        
  69.     for(i = 0;i < resource_num;i++)   
  70.     {   
  71.         if(request_res[i] > need[proc_no - 1][i])   
  72.         {   
  73.             cerr<<"THE NO "<<i + 1<<" REQUEST RESOURCE NUMBER IS BEYOND THE NEED OF THIS PROCESS"<<endl;   
  74.             cerr<<"REQUEST FAILED"<<endl;   
  75.             return;   
  76.         }   
  77.        
  78.         if(request_res[i] > available[i])   
  79.         {   
  80.             cerr<<"THE NO "<<i + 1<<" REQUEST RESOURCE NUMBER IS BEYOND THE AVAILABLE RESOURCE"<<endl;   
  81.             cerr<<"REQUEST FAILED"<<endl;   
  82.             return;   
  83.         }   
  84.     }   
  85.        
  86.     for(i = 0;i < resource_num;i++)   
  87.     {   
  88.         available[i] -= request_res[i];   
  89.         allocation[proc_no - 1][i] += request_res[i];   
  90.         need[proc_no][i] -= request_res[i];   
  91.     }   
  92.        
  93.     if(is_Safe())   
  94.     {   
  95.         cout<<"ALLOCATION SUCCESS"<<endl;   
  96.     }   
  97.     else  
  98.     {   
  99.         for(i = 0;i < resource_num;i++)   
  100.         {   
  101.             available[i] += request_res[i];   
  102.             allocation[proc_no - 1][i] -= request_res[i];   
  103.             need[proc_no][i] += request_res[i];   
  104.         }   
  105.         cerr<<"NOT SAFE!ALLOCATION FAILED"<<endl;   
  106.     }   
  107. }   
  108.   
  109. void Banker_Algorithm::callback(unsigned int proc_no)   
  110. {   
  111.     unsigned int i;   
  112.        
  113.     if(proc_no > proc_num)   
  114.     {   
  115.         cerr<<"WRONG PROCESS NO,REQUEST FAILED"<<endl;   
  116.         return;   
  117.     }   
  118.        
  119.     for(i = 0;i < resource_num;i++)   
  120.     {   
  121.         available[i] += allocation[proc_no - 1][i];   
  122.         allocation[proc_no - 1][i] = 0;   
  123.         need[proc_no - 1][i] = max[proc_no][i];   
  124.     }   
  125.        
  126.     cout<<"CALLBACK SUCCESS"<<endl;   
  127. }   
  128.   
  129. bool Banker_Algorithm::is_Safe()   
  130. {   
  131.     int i;   
  132.     unsigned int work[resource_num],safe_seq[proc_num],j,safe_seq_index = 0;   
  133.     bool finish[proc_num],is_safe,is_fit;   
  134.        
  135.     for(i = 0;static_cast<unsigned int>(i) < proc_num;i++)   
  136.     {   
  137.         finish[i] = false;   
  138.     }   
  139.        
  140.     for(i = 0;static_cast<unsigned int>(i) < resource_num;i++)   
  141.     {   
  142.         work[i] = available[i];   
  143.     }   
  144.        
  145.     i = 0;   
  146.     for(;static_cast<unsigned int>(i) < proc_num;i++)   
  147.     {   
  148.         is_fit = true;   
  149.         if(!finish[i])   
  150.         {   
  151.             for(j = 0;j < resource_num;j++)   
  152.             {   
  153.                 if(need[i][j] > work[j])   
  154.                 {   
  155.                     is_fit = false;   
  156.                     break;   
  157.                 }   
  158.             }   
  159.                
  160.             if(is_fit)   
  161.             {   
  162.                 for(j = 0;j < resource_num;j++)   
  163.                 {   
  164.                     work[j] += allocation[i][j];   
  165.                 }   
  166.                 finish[i] = true;   
  167.                 safe_seq[safe_seq_index++] = i + 1;   
  168.                 i = -1;   
  169.             }   
  170.         }   
  171.         else  
  172.         {   
  173.             is_safe = true;   
  174.             for(j = 0;j < proc_num;j++)   
  175.             {   
  176.                 if(!finish[j])   
  177.                 {   
  178.                     is_safe = false;   
  179.                     break;   
  180.                 }   
  181.             }   
  182.                
  183.             if(is_safe)   
  184.             {   
  185.                 cout<<"SAFE SEQUENCE:";   
  186.                 for(j = 0;j < proc_num;j++)   
  187.                 {   
  188.                     cout<<safe_seq[j]<<" ";   
  189.                 }   
  190.                 cout<<endl;   
  191.                 return true;   
  192.             }   
  193.         }   
  194.     }   
  195.        
  196.     return false;   
  197. }   
  198.   
  199. void Banker_Algorithm::disp_Available()   
  200. {   
  201.     unsigned int i;   
  202.        
  203.     cout<<"AVAILABLE RESOURCE STATUS"<<endl;   
  204.     for(i = 0;i < resource_num;i++)   
  205.     {   
  206.         cout<<"NO "<<i + 1<<":"<<available[i]<<endl;   
  207.     }   
  208. }   
  209.   
  210. void Banker_Algorithm::disp_Allocation()   
  211. {   
  212.     unsigned int i,j;   
  213.        
  214.     cout<<"ALLOCATION STATUS"<<endl;   
  215.     for(i = 0;i < proc_num;i++)   
  216.     {   
  217.         cout<<"PROCESS "<<i + 1;   
  218.         for(j = 0;j < resource_num;j++)   
  219.         {   
  220.             cout<<" RES NO "<<j<<":"<<allocation[i][j]<<" ";   
  221.         }   
  222.         cout<<endl;   
  223.     }   
  224. }   

main.cpp

Code:
  1. #include <iostream>   
  2. #include "Banker_Algorithm.h"   
  3.   
  4. using namespace std;   
  5.   
  6. int main()   
  7. {   
  8.     cout<<"OS BIG WORK NO 2 BY MARCUSXING 2009 12 12"<<endl;   
  9.     cout<<"****************************************"<<endl;   
  10.     cout<<"please enter the process number:";   
  11.     unsigned int proc_num,res_num;   
  12.     cin>>proc_num;   
  13.     cout<<"please enter the resource number:";   
  14.     cin>>res_num;   
  15.        
  16.     Banker_Algorithm ba(proc_num,res_num);   
  17.        
  18.     unsigned int max,avai,i,j;   
  19.        
  20.     for(i = 0;i < proc_num;i++)   
  21.     {   
  22.         for(j = 0;j < res_num;j++)   
  23.         {   
  24.             cout<<"please enter No "<<i + 1<<" process No "<<j + 1<<" resource max:";   
  25.             cin>>max;   
  26.             ba.get_Max()[i][j] = max;   
  27.         }   
  28.     }   
  29.        
  30.     for(j = 0;j < res_num;j++)   
  31.     {   
  32.         cout<<"please enter No "<<j + 1<<" resource available number:";   
  33.         cin>>avai;   
  34.         ba.get_Available()[j] = avai;   
  35.     }   
  36.        
  37.     ba.init_Need();   
  38.        
  39.     unsigned int proc_no,req_array[res_num];   
  40.     char c;   
  41.     bool flag = true;   
  42.     //ba.request_Resource();   
  43.     if(ba.is_Safe())   
  44.     {   
  45.         cout<<"****************************************"<<endl;   
  46.         cout<<"please select a number to do something"<<endl;   
  47.         cout<<"h:help message"<<endl;   
  48.         cout<<"1:request resource"<<endl;   
  49.         cout<<"2:callback resource of a process"<<endl;   
  50.         cout<<"3:display available resource status"<<endl;   
  51.         cout<<"4:display allocation status"<<endl;   
  52.         cout<<"q:quit"<<endl;   
  53.         while(flag)   
  54.         {      
  55.             cout<<"please enter your choice:";   
  56.             if(!cin)   
  57.             {   
  58.                 cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  59.             }   
  60.             cin>>c;   
  61.                
  62.             switch(c)   
  63.             {   
  64.             case '1':   
  65.                 cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  66.                 cout<<"please enter the process No:";   
  67.                 cin>>proc_no;   
  68.                 for(j = 0;j < res_num;j++)   
  69.                 {   
  70.                     cout<<"please enter the No "<<j + 1<<" resource request number:";   
  71.                     cin>>req_array[j];   
  72.                 }   
  73.                 ba.request_Resource(proc_no - 1,req_array);   
  74.                 cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  75.                 break;   
  76.             case '2':   
  77.                 cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  78.                 cout<<"please enter the process No:";   
  79.                 cin>>proc_no;   
  80.                 ba.callback(proc_no);   
  81.                 cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  82.                 break;   
  83.             case '3':   
  84.                 ba.disp_Available();   
  85.                 cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  86.                 break;   
  87.                    
  88.             case '4':   
  89.                 ba.disp_Allocation();   
  90.                 cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  91.                 break;   
  92.             case 'q':   
  93.                 flag = false;   
  94.                 break;   
  95.             case 'h':   
  96.                 cin.ignore(numeric_limits<streamsize>::max(),'/n');   
  97.                 cout<<"****************************************"<<endl;   
  98.                 cout<<"please select a number to do something"<<endl;   
  99.                 cout<<"h:help message"<<endl;   
  100.                 cout<<"2:callback resource of a process"<<endl;   
  101.                 cout<<"1:request resource"<<endl;   
  102.                 cout<<"2:display available resource status"<<endl;   
  103.                 cout<<"3:display allocation status"<<endl;   
  104.                 cout<<"q:quit"<<endl;   
  105.                 break;   
  106.             default:   
  107.                 cout<<"are you kidding me?"<<endl;   
  108.                 return -1;   
  109.                 break;   
  110.             }   
  111.         }   
  112.     }   
  113.     else  
  114.     {   
  115.         cerr<<"NOT SAFE"<<endl;   
  116.         return -1;   
  117.     }   
  118.        
  119.        
  120.     return 0;   
  121. }   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值