27. Banker Algorithm 範例練習
④為Safety Algorithm做前提的變數假設,設想系統借資源給Process i之後
的情形:
Allocation[i, 1..m] += Request(i)[1..m]
Need[i, 1..m] -= Request(i)[1..m]
Available[1..m] -= Request(i)[1..m]
27
Request
A B C
1 0 2P1
Available
A B C
2 3 0
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Available
A B C
3 3 2
Allocation
A B C
0 1 0
3 0 2
3 0 2
2 1 1
0 0 2
Need
A B C
7 4 3
0 2 0
6 0 0
0 1 1
4 3 1
Need
A B C
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
→ → →
28. Banker Algorithm 範例練習
⑤- ①執行Safety Algorithm
Work[1..m] = Available[1..m]
Finish[1..n] = false
28
Available
A B C
2 3 0
Work
A B C
2 3 0
P0
P1
P2
P3
P4
Finish
false
false
false
false
false
=
29. Banker Algorithm 範例練習
⑤- ②模擬資源的分發:
while(Finish[i]==false && Need[i, j]<=Work){
Finish[i] ==true
Work[j] += Allocation[i, j]
}
29
Work
A B C
2 3 0
P0
P1
P2
P3
P4
Finish
false
false
false
false
false
Need
A B C
7 4 3
0 2 0
6 0 0
0 1 1
4 3 1
Allocation
A B C
0 1 0
3 0 2
3 0 2
2 1 1
0 0 2
Safety Queue
<null>
P0
P1
P2
P3
P4
30. Banker Algorithm 範例練習
⑤- ②模擬資源的分發:
while(Finish[i]==false && Need[i, j]<=Work){
Finish[i] ==true
Work[j] += Allocation[i, j]
}
30
Work
A B C
2 3 0
P0
P1
P2
P3
P4
Finish
false
true
false
false
false
Need
A B C
7 4 3
0 2 0
6 0 0
0 1 1
4 3 1
Allocation
A B C
0 1 0
3 0 2
3 0 2
2 1 1
0 0 2
Safety Queue
<P1>
Work
A B C
5 3 2→
+(3, 0, 2)
其實真實是先-(0, 2, 0)再+(3, 2, 2)
P0
P1
P2
P3
P4
31. Banker Algorithm 範例練習
⑤- ②模擬資源的分發:
while(Finish[i]==false && Need[i, j]<=Work){
Finish[i] ==true
Work[j] += Allocation[i, j]
}
31
Work
A B C
5 3 2
P0
P1
P2
P3
P4
Finish
false
true
false
true
false
Need
A B C
7 4 3
0 0 0
6 0 0
0 1 1
4 3 1
Allocation
A B C
0 1 0
0 0 0
3 0 2
2 1 1
0 0 2
Safety Queue
<P1, P3>
Work
A B C
7 4 3→
+(2, 1, 1)
其實真實是先-(0, 1, 1)再+(2, 2, 2)
P0
P1
P2
P3
P4
32. Banker Algorithm 範例練習
⑤- ②模擬資源的分發:
while(Finish[i]==false && Need[i, j]<=Work){
Finish[i] ==true
Work[j] += Allocation[i, j]
}
32
Work
A B C
7 4 3
P0
P1
P2
P3
P4
Finish
false
true
false
true
true
Need
A B C
7 4 3
0 0 0
6 0 0
0 0 0
4 3 1
Allocation
A B C
0 1 0
0 0 0
3 0 2
0 0 0
0 0 2
Safety Queue
<P1, P3, P4>
Work
A B C
7 4 5→
+(0, 0, 2)
其實真實是先-(4, 3, 1)再+(4, 3, 3)
P0
P1
P2
P3
P4
33. Banker Algorithm 範例練習
⑤- ②模擬資源的分發:
while(Finish[i]==false && Need[i, j]<=Work){
Finish[i] ==true
Work[j] += Allocation[i, j]
}
33
Work
A B C
7 4 5
P0
P1
P2
P3
P4
Finish
true
true
false
true
true
Need
A B C
7 4 3
0 0 0
6 0 0
0 0 0
0 0 0
Allocation
A B C
0 1 0
0 0 0
3 0 2
0 0 0
0 0 0
Safety Queue
<P1, P3, P4, P0>
Work
A B C
7 5 5→
+(0, 1, 0)
其實真實是先-(7, 4, 3)再+(7, 5, 3)
P0
P1
P2
P3
P4
34. Banker Algorithm 範例練習
⑤- ②模擬資源的分發:
while(Finish[i]==false && Need[i, j]<=Work){
Finish[i] ==true
Work[j] += Allocation[i, j]
}
34
Work
A B C
7 5 5
P0
P1
P2
P3
P4
Finish
true
true
true
true
true
Need
A B C
0 0 0
0 0 0
6 0 0
0 0 0
0 0 0
Allocation
A B C
0 0 0
0 0 0
3 0 2
0 0 0
0 0 0
Safety Queue
<P1, P3, P4, P0, P2>
Work
A B C
10 5 7→
+(3, 0, 2)
其實真實是先-(6, 0, 0)再+(9, 0, 2)
P0
P1
P2
P3
P4