操作系統實驗-銀行家算法


銀行家算法

 

1. 實驗目的與要求

  1. 理解死鎖的概念,掌握並會編寫銀行家算法與安全性檢測算法;

  2. 使用高級程序設計語言設計並實現銀行家算法基本過程;

  3. 驗證銀行家算法對於避免死鎖的作用。

2. 實驗平台

操作系統:Windows

注:可使用自己熟悉的語言設計編程,但需在實驗報告中注明編譯環境以及編輯工具。

3. 實驗內容和要求

  1. 定義並初始化進程及其資源數據結構

  2. 提供一個用戶界面,用戶利用它可動態輸入進程和資源種類等相關參數

  3. 編制模擬銀行家算法的程序,並以下面給出的例子驗證所編寫的程序的正確性。

 

設系統中有三種類型的資源(A,B,C)和五個進程(P0,P1,P2,P3,P4),A資源的數量為17,B資源的數量為5,C資源的數量為20。當前時刻系統狀態如下所示。

 

進程 已占資源 最大需求數

資源種類 A B C A B C

P0 2 1 2 5 5 9

P1 4 0 2 5 3 6

P2 4 0 5 4 0 11

P3 2 0 4 4 2 5

P4 3 1 4 4 2 4

 

現在系統中A、B、C 3類資源分別還剩2、3、3個,請根據銀行家算法完成以下問題:

1、 現在系統是否處於安全狀態?若是,請給出安全序列。要求繪圖粘貼到實驗報告描述計算過程,並截圖實驗過程和結果驗證程序正確性。

2、 如果現在進程P1請求資源(0、3、4),能否實現資源分配?為什么?實驗報告附上計算過程,並截圖實驗過程和結果驗證程序正確性。

3、若進程P3請求資源(2、0、1),能否實現資源分配?為什么?實驗報告附上計算過程,並截圖實驗過程和結果驗證程序正確性。

 

 

附:測試用例為

5 5 9

5 3 6

4 0 11

4 2 5

4 2 4

 

2 1 2      

4 0 2    

4 0 5    

2 0 4

3 1 4

4. 實驗報告

 

《計算機操作系統》課程機房上機實驗報告        
題目 銀行家算法 姓名 波哥哥 日期:2020.6.29
實驗環境 Dev C++      
實驗內容 與完成情況 提示:填寫實驗原理或核心思想,核心代碼可附在實驗報告之后      
實驗運行結果

 

     
出現的問題 安全序列不是按照正確的(編號從小到大)順序輸出      
解決方案(列出遇到的問題和解決辦法,列出沒有解決的問題) 每輸出一個之后break,退出此循環,再次執行總的操作      

 

5. C語言代碼

#include <stdio.h>
​
#include <stdlib.h>
​
 
​
#define UP 100
​
 
​
int flag_need = 1;
​
int row,col;
​
int Max[UP][UP],Allocation[UP][UP],Need[UP][UP],finish[UP] = {0},Available[UP],Work[UP],P[UP];
​
  
​
void init(){   
​
    printf("輸入進程數:\n");
​
    scanf("%d",&row);
​
    printf("輸入資源數:\n");
​
    scanf("%d",&col);
​
    printf("輸入每個進程最多需要的資源數按照%d x %d矩陣輸入:\n",row,col);
​
    for(int i = 0;i < row;i ++){
​
       for(int j = 0;j < col;j ++){
​
           scanf("%d",&Max[i][j]);
​
       }      
    }
​
    printf("輸入每個進程已經分配的資源數按照%d x %d矩陣輸入:\n",row,col);
​
    for(int i = 0;i < row;i ++){
​
       for(int j = 0;j < col;j ++){
​
           scanf("%d",&Allocation[i][j]);
​
           Need[i][j] = Max[i][j]-Allocation[i][j];
​
           if(Need[i][j] < 0){
​
              printf("進程P%d的R%d資源出錯\n");
​
              flag_need = 0;
​
           }
​
       }      
​
    }
​
    printf("請輸入現有各資源數:\n");
​
    for(int i = 0;i < col;i ++){
​
       scanf("%d",&Available[i]);
​
       Work[i] = Available[i];
​
    }   
​
}
​
 
​
void print(bool x){
​
    int k;
​
    if(x){
​
       printf("系統是安全的\n安全序列為:");
​
       for(k = 0;k < row - 1;k ++){
​
              printf("%d ->",P[k]);
​
           }
​
       printf("%d\n",P[k]);
​
    }
​
    else
​
       printf("系統不安全\n"); 
​
} 
​
 
​
bool isSafe(){ 
​
    int k = 0,len = 0,lastlen = 0;
​
    while(len < row){ 
​
       for(int i = 0;i < row;i ++){
​
           if(finish[i] != 1){
​
              k = 0;
​
              for (int j = 0;j < col;j ++){
​
                  if (Need[i][j] <= Work[j]) 
​
                     k ++;   
​
              }
​
              if (k == col) {
​
                  finish[i] = 1;
​
                  P[len] = i;//放進程安全序列 
​
                  
​
                  for(int j = 0;j < col;j ++){
​
                     Work[j] += Allocation[i][j];
​
                  }
​
/*************************************調試代碼***************************************************
​
                  printf("Work:\n");
​
                  for (int j = 0;j < col;j ++){
​
                     printf("%d ",Work[j]);
​
                  }
​
                  printf("\n");
​
**********************************************************************************************/    
                  len ++;  //統計進程個數
break;
​
              }             
​
           }
​
       }
​
       //上一次循環長度與下一次相等,則出現不安全情況,進程未被完全訪問 
if (lastlen == len){
​
           break;
​
       }else{
​
           lastlen = len;
​
       }
​
    }
​
    if(len == row){
       return true;
​
    }
​
    else {
​
       return false;
​
    }
​
}
​
 
​
 
​
/********************************調試代碼***********************************************************
​
void test(){
​
    printf("Need:\n");
​
    for (int i = 0;i < row;i ++){
​
       for (int j = 0;j < col;j ++){
​
           printf("%d ",Need[i][j]);
​
       }
​
      printf("\n");
​
    }
​
    printf("Allocation:\n");
​
    for (int i = 0;i < row;i ++){
       for (int j = 0;j < col;j ++){
​
           printf("%d ",Allocation[i][j]);
​
       }
​
       printf("\n");
​
    }
​
    printf("\n");
​
    printf("Work:\n");
​
    for (int j = 0;j < col;j ++){
​
       printf("%d ",Work[j]);
​
    }
​
    printf("\n");
​
    printf("\nAvailable:\n");
​
    for (int j = 0;j < col;j ++){
​
       printf("%d ",Available[j]);
​
    }
​
    
​
    printf("\n");
​
}
​
******************************************************************************************************/int request(){
​
//   test();
int j,reqProcessNum,reqResource[col];
​
   printf("輸入要申請的資源的進程號:(第一個進程號為0,第二個進程號為1,以此類推.)\n");
​
    scanf("%d",&reqProcessNum);
​
    printf("請輸入所請求的各資源數:\n");
​
    
​
    //更新Need Available重置Work finish 
for (j = 0;j < col;j ++){
​
       scanf("%d",&reqResource[j]);
​
       if (reqResource[j] > Need[reqProcessNum][j] || reqResource[j] > Available[j]){
​
           printf("該進程請求資源數過大,不予分配"); 
​
           return 0;//請求數不合法 
​
       }
​
    }
​
    for(int i = 0;i < row;i ++){
​
       finish[i] = 0;
​
    }
​
    
​
    if(j == col){
​
       for(j = 0;j < col;j ++){
​
           Available[j] -= reqResource[j];
​
           Need[reqProcessNum][j] -= reqResource[j];
​
           Allocation[reqProcessNum][j] += reqResource[j];
​
           Work[j] = Available[j];
​
       }
​
    }
​
//   test();
return 1;//請求數合法 
​
} 
​

​
int main (){
​
    init();
​
    print(isSafe());
​
    
​
    //請求數合法判斷是否有安全序列  
if(request()){
​
       print(isSafe()); 
​
    }
​
    return 0;
​
}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM