•Multiple instances.
•Each process must a priori claim maximum use.
•When a process requests a resource it may have to wait.
•When a process gets all its resources it must return them in a finite amount of time.
n为进程的数目,m为资源类型的数目
•Available: Vector of length m. –If available [j] = k, there are k instances of resource type Rj available.(如果available[j]=k,那么资源Rj有k个实例可用) •Max: n x m matrix. –If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.(如果Max[i,j]=k,那么进程Pi可以最多请求资源Rj的k个实例)
Data Structures for the Banker’s Algorithm (Cont.):
•Allocation: n x m matrix. –If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.(如果Allocation[i,j]=k,那么进程Pi当前分配了k个资源Rj的实例) •Need: n x m matrix. –If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.(如果Need[i,j]=k,那么进程Pi 还需要k个资源Rj的实例)
Need [i,j] = Max[i,j] – Allocation [i,j].
Safety Algorithm(安全算法):
1. Let Work and Finish be vectors of length m and n, respectively. Initialize(让Work和Finish作为长度为m和n的向量)
Work := Available
Finish [i] = false fori – 1,3, …, n.
2. Find an isuch that both: (找到i)
(a) Finish [i] = false
(b) Needi£Work
If no such iexists, go to step 4.
3. Work := Work + Allocationi
Finish[i] := true
go to step 2.
If Finish [i] = true for all i, then the system is in a safe state.
Resource-Request Algorithm for Process Pi
•Requesti = request vector for process Pi.
1. If Requesti £ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.
2. If Requesti £ Available, go to step 3. Otherwise Pi must wait, since resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as follows:假定给Pi 分配资源
Available := Available – Requesti;
Allocationi := Allocationi + Requesti;
Needi := Needi – Requesti
4.用安全算法进行检查,看系统是否处于安全状态
•If safe Þ the resources are allocated to Pi. •If unsafe Þ Pi must wait, and the old resource-allocation state is restored
Example of Banker’s Algorithm: •5 processes P0 through P4; 3 resource types A (10 instances),
B (5instances), and C (7 instances).(5个进程P0-P4,3类资源A(10个实例),B(5个实例),C(7个实例)) •Snapshot at time T0:(时刻T0的片段)
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
•The content of the matrix. Need is defined to be Max – Allocation. ( Need矩阵的内容可以由Max – Allocation 计算出来)
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
•用安全检测算法看能否找到一个安全序列 –Work[]=available=(3,3,2) –Finish[i]=false (i=0..4) – – Work need allocation finish – P1 3 3 2 1 2 2 2 0 0 T – P3 5 3 2 0 1 1 2 1 1 T – P4 7 4 3 4 3 1 0 0 2 T – P2 7 4 5 6 0 0 3 0 2 T – P0 10 4 7 7 4 3 0 1 0 T –存在安全序列:(P1,P3,P4,P2,P0)
注意两点:
1、通常,安全序列不唯一;
2、安全序列中的选取,总是将work中的资源首先分给need(Pi)<=work且max{allocation(Pi)}的进程,剩下的依次类推分配