n为进程的数目,m为资源类型的数目
Data Structures for the Banker’s Algorithm (Cont.):
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
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.用安全算法进行检查,看系统是否处于安全状态
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
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
注意两点:
1、通常,安全序列不唯一;
2、安全序列中的选取,总是将work中的资源首先分给need(Pi)<=work且max{allocation(Pi)}的进程,剩下的依次类推分配