上篇博客中 进程管理之死锁 我们讲到了进程管理中死锁的各种问题,其中留下了死锁避免算法中著名的银行家算法没讲,下面就为大家详细解读。
1.安全序列
讲银行家算法之前,我们首先引入安全序列的定义:所谓系统是安全的,是指系统中的所有进程能够按照某一种次序分配资源,并且依次地运行完毕,这种进程序列{P1,P2,...,Pn}就是安全序列。如果存在这样一个安全序列,则系统是安全的;如果系统不存在这样一个安全序列,则系统是不安全的。
安全序列{P1,P2,...,Pn}是这样组成的:若对于每一个进程Pi,它需要的附加资源可以被系统中当前可用资源加上所有进程Pj当前占有资源之和所满足,则{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:
(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)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。
(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,则表示安全;否则系统不安全。
下面是java代码实现
参考资料:http://en.wikipedia.org/wiki/Banker's_algorithm#Safe_and_Unsafe_States
- 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]
- 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].
- 银行家算法:
(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)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。
- 安全性检查
(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 <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(j=0;j<r;j++)
- {
- scanf("%d",&curr[i][j]);
- }
- }
- printf("\nThe resource of instances: ");
- for(i=0;i<r;i++)
- {
- printf("\t%d",maxres[i]);
- }
- printf("\nThe allocated resource table:\n");
- for(i=0;i<p;i++)
- {
- for(j=0;j<r;j++)
- {
- printf("\t%d",curr[i][j]);
- }
- printf("\n");
- }
- printf("\nThe maximum resource table:\n");
- for(i=0;i<p;i++)
- {
- for(j=0;j<r;j++)
- {
- printf("\t%d",maxclaim[i][j]);
- }
- printf("\n");
- }
- for(i=0;i<p;i++)
- {
- for(j=0;j<r;j++)
- {
- alloc[j]+=curr[i][j];
- }
- }
- printf("\nAllocated resources:");
- for(i=0;i<r;i++)
- {
- printf("\t%d",alloc[i]);
- }
- for(i=0;i<r;i++)
- {
- avl[i]=maxres[i]-alloc[i];
- }
- printf("\nAvailable resources:");
- for(i=0;i<r;i++)
- {
- printf("\t%d",avl[i]);
- }
- printf("\n");
- //Main procedure goes below to check for unsafe state.
- while(count!=0)
- {
- safe=0;
- for(i=0;i<p;i++)
- {
- if(running[i])
- {
- exec=1;
- for(j=0;j<r;j++)
- {
- if(maxclaim[i][j] - curr[i][j] > 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<r;j++) {
- avl[j]+=curr[i][j];
- }
- break;
- }
- }
- }
- if(!safe)
- {
- printf("\nThe processes are in unsafe state.\n");
- break;
- }
- else
- {
- printf("\nThe process is in safe state");
- printf("\nSafe sequence is:");
- for(i=0;i<r;i++)
- {
- printf("\t%d",avl[i]);
- }
- printf("\n");
- }
- }
- }
- /*SAMPLE OUTPUT
- -----------------
- Enter the number of resources:4
- Enter the number of processes:5
- Enter Claim Vector:8 5 9 7
- Enter Allocated Resource Table:
- 2 0 1 1
- 0 1 2 1
- 4 0 0 3
- 0 2 1 0
- 1 0 3 0
- Enter Maximum Claim table:
- 3 2 1 4
- 0 2 5 2
- 5 1 0 5
- 1 5 3 0
- 3 0 3 3
- The Claim Vector is: 8 5 9 7
- The Allocated Resource Table:
- 2 0 1 1
- 0 1 2 1
- 4 0 0 3
- 0 2 1 0
- 1 0 3 0
- The Maximum Claim Table:
- 3 2 1 4
- 0 2 5 2
- 5 1 0 5
- 1 5 3 0
- 3 0 3 3
- Allocated resources: 7 3 7 5
- Available resources: 1 2 2 2
- Process3 is executing
- The process is in safe state
- Available vector: 5 2 2 5
- Process1 is executing
- The process is in safe state
- Available vector: 7 2 3 6
- Process2 is executing
- The process is in safe state
- Available vector: 7 3 5 7
- Process4 is executing
- The process is in safe state
- Available vector: 7 5 6 7
- Process5 is executing
- The process is in safe state
- Available vector: 8 5 9 7
- ---------------------------------------------------------*/
- 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<np;i++)
- for(int j=0;j<nr;j++)
- allocate[i][j]=sc.nextInt(); //allocation matrix
- System.out.println("Enter max matrix -->");
- for(int i=0;i<np;i++)
- for(int j=0;j<nr;j++)
- max[i][j]=sc.nextInt(); //max matrix
- System.out.println("Enter available matrix -->");
- for(int j=0;j<nr;j++)
- avail[0][j]=sc.nextInt(); //available matrix
- sc.close();
- }
- private int[][] calc_need(){
- for(int i=0;i<np;i++)
- for(int j=0;j<nr;j++) //calculating need matrix
- need[i][j]=max[i][j]-allocate[i][j];
- return need;
- }
- private boolean check(int i){
- //checking if all resources for ith process can be allocated
- for(int j=0;j<nr;j++)
- if(avail[0][j]<need[i][j])
- return false;
- return true;
- }
- public void isSafe(){
- input();
- calc_need();
- boolean done[]=new boolean[np];
- int j=0;
- while(j<np){ //until all process allocated
- boolean allocated=false;
- for(int i=0;i<np;i++)
- if(!done[i] && check(i)){ //trying to allocate
- for(int k=0;k<nr;k++)
- avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
- System.out.println("Allocated process : "+i);
- allocated=done[i]=true;
- j++;
- }
- if(!allocated) break; //if no allocation
- }
- if(j==np) //if all processes are allocated
- System.out.println("\nSafely allocated");
- else
- System.out.println("All proceess cant be allocated safely");
- }
- public static void main(String[] args) {
- new Bankers().isSafe();
- }
- }
- --------------------------------------------------------------------------------------------------------------------------
- Output
- --------------------------------------------------------------------------------------------------------------------------
- Enter no. of processes and resources : 3 4
- Enter allocation matrix -->
- 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