避免死锁之银行家算法

上篇博客中 进程管理之死锁 我们讲到了进程管理中死锁的各种问题,其中留下了死锁避免算法中著名的银行家算法没讲,下面就为大家详细解读。

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实现一遍。
/*PROGRAM TO IMPLEMENT BANKER'S ALGORITHM
  *   --------------------------------------------*/
#include 
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 avl[j]){
                        exec=0;
                        break;
                    }
                }
                if(exec)
                {
                    printf("\nProcess%d is executing\n",i+1);
                    running[i]=0;
                    count--;
                    safe=1;
 
                    for(j=0;j



下面是java代码实现

import java.util.Scanner;

public class Bankers{
    private int need[][],allocate[][],max[][],avail[][],np,nr;
    
    private void input(){
     Scanner sc=new Scanner(System.in);
     System.out.print("Enter no. of processes and resources : ");
     np=sc.nextInt();  //no. of process
     nr=sc.nextInt();  //no. of resources
     need=new int[np][nr];  //initializing arrays
     max=new int[np][nr];
     allocate=new int[np][nr];
     avail=new int[1][nr];
     
     System.out.println("Enter allocation matrix -->");
     for(int i=0;i");
     for(int i=0;i");
        for(int j=0;j
1 2 2 1
1 0 3 3
1 2 1 0
Enter max matrix -->
3 3 2 2
1 1 3 4
1 3 5 0
Enter available matrix -->
3 1 1 2
Allocated process : 0
Allocated process : 1
Allocated process : 2
Safely allocated


参考资料:http://en.wikipedia.org/wiki/Banker's_algorithm#Safe_and_Unsafe_States



转载请注明出处:http://blog.csdn.net/speedme


你可能感兴趣的:(Operating,System)