避免死結之銀行家演算法

來源:互聯網
上載者:User

上篇部落格中 進程管理之死結 我們講到了進程管理中死結的各種問題,其中留下了死結避免演算法中著名的銀行家演算法沒講,下面就為大家詳細解讀。 1.安全序列 講銀行家演算法之前,我們首先引入安全序列的定義:所謂系統是安全的,是指系統中的所有進程能夠按照某一種次序分配資源,並且依次地運行完畢,這種進程式列{P1,P2,...,Pn}就是安全序列。如果存在這樣一個安全序列,則系統是安全的;如果系統不存在這樣一個安全序列,則系統是不安全的。
安全序列{P1,P2,...,Pn}是這樣組成的:若對於每一個進程Pi,它需要的附加資源可以被系統中當前可用資源加上所有進程Pj當前佔有資源之和所滿足,則{P1,P2,...,Pn}為一個安全序列,這時系統處於安全狀態,不會進入死結狀態。  
雖然存在安全序列時一定不會有死結發生,但是系統進入不安全狀態(四個死結的必要條件同時發生)也未必會產生死結。當然,產生死結後,系統一定處於不安全狀態。 

2.銀行家演算法 (為了熟悉英語請原諒我借用wiki上的文字來描述) For the Banker's algorithm to work, it needs to know three things:
How much of each resource each process could possibly request[CLAIMS] How much of each resource each process is currently holding[ALLOCATED] How much of each resource the system currently has available[AVAILABLE] Resources may be allocated to a process only if it satisfies the following conditions:
request ≤ max, else set error condition as process has crossed maximum claim made by it. request ≤ available, else process waits until resources are available.
Basic data structures to be maintained to implement the Banker's Algorithm:

Available: A vector of length m indicates the number of available resources of each type. If Available[j] = k, there are k instances of resource type Rj available. Max: An n×m matrix defines the maximum demand of each process. If Max[i,j] = k, then Pi may request at most k instances of resource type Rj. Allocation: An n×m matrix defines the number of resources of each type currently allocated to each process. If Allocation[i,j] = k, then process Pi is currently allocated k instance of resource type Rj. Need: An n×m matrix indicates the remaining resource need of each process. If Need[i,j] = k, then Pi may need k more instances of resource type Rj to complete task. Note: Need[i,j] = Max[i,j] - Allocation[i,j].


銀行家演算法: 設進程i提出請求Request[j],則銀行家演算法按如下規則進行判斷。
(1) 如果Request[j]≤Need[i,j],則轉向(2),否則認為出錯。
(2) 如果Request[j]≤Available[j],則轉向(3);否則表示尚無足夠資源,Pi需等待。
(3) 假設進程i的申請已獲批准,於是修改系統狀態:
Available[j]=Available[j]-Request[i]
Allocation[i,j]=Allocation[i,j]+Request[j]
Need[i,j]=Need[i,j]-Request[j]

(4)系統執行安全性檢查,如安全,則分配成立;否則試探險性分配作廢,系統復原原狀,進程等待。
安全性檢查 (1) 設定兩個工作向量Work=Available;Finish[i]=False
(2) 從進程集合中找到一個滿足下述條件的進程,
      Finish [i]=False;
      Need[i,j]≤Work[j];
     如找到,執行(3);否則,執行(4)
(3) 設進程獲得資源,可順利執行,直至完成,從而釋放資源。
     Work[j]=Work[i]+Allocation[i,j];
Finish[i]=True;
go to step 2;
(4) 如所有的進程Finish[i]=true,則表示安全;否則系統不安全。


由於時間不早了就借用下wiki上的c語言實現代碼,改天用java實現一遍。 [cpp]  view plain copy print ? /*PROGRAM TO IMPLEMENT BANKER'S ALGORITHM    *   --------------------------------------------*/   #include <stdio.h>   int curr[5][5], maxclaim[5][5], avl[5];   int alloc[5] = {0,0,0,0,0};   int maxres[5], running[5], safe=0;   int count = 0, i, j, exec, r, p,k=1;       int main()   {       printf("\nEnter the number of processes: ");       scanf("%d",&p);           for(i=0;i<p;i++)       {           running[i]=1;           count++;       }           printf("\nEnter the number of resources: ");       scanf("%d",&r);           for(i=0;i<r;i++)       {            printf("\nEnter the resource for instance %d: ",k++);           scanf("%d",&maxres[i]);       }           printf("\nEnter maximum resource table:\n");       for(i=0;i<p;i++)       {           for(j=0;j<r;j++)           {               scanf("%d",&maxclaim[i][j]);           }       }           printf("\nEnter allocated resource table:\n");       for(i=0;i<p;i++)       {           for(

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.