SlideShare a Scribd company logo
1 of 51
OS第五次讀書會
2017/06/09
TA 鄭人瑋
Ch7 死結(Deadlock)
2
Case:回顧哲學家同步問題
3
chopstick[0] chopstick[1]
chopstick[2]
chopstick[3]
chopstick[4]
Philosopher[i]
do{
wait(chopstick[i]);
wait(chopstick[(i+1)%5]);
Philosopher[i]吃飯
signal(chopstick[i]);
signal(chopstick[(i+1)%5]);
}while(true)
Case分析
• 上述方法會產生Deadlock
• 若每個哲學家都拿起右邊的筷子(chopstick[i]),則會造成所有的
哲學家都陷入沉思等待無飯可吃
4
chopstick[0] chopstick[1]
chopstick[2]
chopstick[3]
chopstick[4]
Deadlock 定義
sss
在系統中存在一組Process陷入互相等待對方資源,而使所有
Process皆無法繼續執行,使得CPU執行效率大幅下降
5
形成Deadlock的四要件
1. Mutual Exclusion (互斥)
2. Hold and Wait (持有並等待)
3. No preemption (不可搶先)
4. Circular Waiting (循環式等候)
6
形成Deadlock的四要件
1. Mutual Exclusion (互斥)
-如ch6提到的Mutex lock,在同一時間內只能有某個Process
使用該資原,其他Process都必須等待,直到資源釋放。
2. Hold and Wait (持有並等待)
3. No preemption (不可搶先)
4. Circular Waiting (循環式等候)
7
形成Deadlock的四要件
1. Mutual Exclusion (互斥)
2. Hold and Wait (持有並等待)
-Process持有部分資源,且正在等待其他資源使用。
3. No preemption (不可搶先)
4. Circular Waiting (循環式等候)
8
形成Deadlock的四要件
1. Mutual Exclusion (互斥)
2. Hold and Wait (持有並等待)
3. No preemption (不可搶先)
-如ch5提到的FCFS,Process不可任意搶奪其他Process的資源
(或插隊),必需等待其Process自願釋放資源才可使用。
4. Circular Waiting (循環式等候)
9
形成Deadlock的四要件
1. Mutual Exclusion (互斥)
2. Hold and Wait (持有並等待)
3. No preemption (不可搶先)
4. Circular Waiting (循環式等候)
-系統內存在一組Process {P0, P1, …, Pn}。其P0正在等待P1釋
放資源,P1正在等待P2釋放資源,…,Pn-1正在等待Pn,且
Pn正在等待P0,則{P0,P1,…,Pn}形成Circular Waiting。
10
Deadlock的處理方式
1. Deadlock Prevention (死結的預防)
2. Deadlock Avoidance (死結的避免)
3. Deadlock Detection & Recovery (死結的偵測與回復)
11
Deadlock Prevention (死結的預防)
• 將Deadlock形成的四要件打破即可讓Deadlock不會發生
• 即將Mutual Exclusion (互斥)、Hold and Wait (持有並等待)、
No preemption (不可搶先)、Circular Waiting (循環式等候)其
中之一打破即可預防Deadlock發生
• 可以保證Deadlock不會發生,但會使系統效能下降
(Troughput↓)
12
Deadlock Prevention (死結的預防)
• 打破Mutual Exclusion (互斥)
• 非常困難! 因為這是某些資源必需的特性(e.g. Critical Section)
13
Deadlock Prevention (死結的預防)
• 打破Hold and Wait (持有並等待)
1. 除非Process能夠取得完成工作的所有資源,才允許持有資源。
(e.g.哲學家問題→必拿兩支筷子才吃飯)
2. Process在取得新資源時,必需釋放自己本身持有的資源。
• 兩種方法都會造成一定程度的系統效能↓
14
Deadlock Prevention (死結的預防)
• 打破No preemption (不可搶先)
• 只要將系統改成preemptive即可
• 會有機會造成Starvation,但不會造成Deadlock(可用Aging解決)
15
Deadlock Prevention (死結的預防)
• 打破Circular Waiting (循環式等候)
• 將系統內每個資源給予一個編號(R1,R2,…Rn),每個Process必需
按照資源編號遞增的方式提出申請。
16
Process持有資源 申請資源 結果
R1、R2 R5 申請成功
R3 R1 需釋放R3才可申請
R1、R2、R6 R4 需釋放R6才可申請
Deadlock Avoidance (死結的避免)
• 將系統分成兩種state:safe/unsafe
• 在safe state下的系統不會有deadlock發生
• 在unsafe state下的系統有deadlock的風險(但不一定會發生)
• deadlock avoidance的用意是使系統處於safe state的狀態,不
讓deadlock有發生的機會→Banker Algorithm
17
Banker Algorithm
• 原理類似銀行借貸的機制
a) 先看客戶有沒有能力償還?
b) 再看自己的錢夠不夠?
c) 最後再模擬錢借出之後,銀行還能不能運行?
• 針對提出資源申請的Process,來檢視系統「是否會因接受此一
Process的申請而進入死結」
18
Banker Algorithm
• 變數宣告(假設總共有n個process及m種資源)
 Request(i)[1..m]
表示某Process對某資源的申請量(e.g. Request(i)[j] = 1)
 Allocation[1...n, 1..m]
表示目前某Process對某資源的持有量(e.g. Allocation[i, j] = 2)
 Max[1..n, 1..m]
表示某Process對於某資源最大需求量(e.g. Max[i, j] = 3)
 Need[1..n, 1..m]
表示目前Process尚需某資源的數量(e.g. Need[i, j] = 4)
 Available[1..m]
表示某資源可用數量(e.g. Available[j] = 5)
19
Banker Algorithm
假設Process i 要提出資源申請
 計算Need[1..n, 1..m] = Max[1..n, 1..m] – Allocation[1..n,
1..m]
 檢查Request(i)[1..m] <= Need[i, 1..m]
 檢查Request(i)[1..m] <= Available[1..m]
 為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]
 執行Safety Algorithm,如果確定沒問題就借資源給Process i
20
Safety Algorithm
• 針對Banker Algorithm中④的假設,試算系統資源之後是否夠用
• 結果若是夠用的,就可以輸出一組Safety Queue<P1, P2,…,Pn>,
代表Deadlock不會發生
• 變數宣告:
 Work[1..m]
初值=Available[1..m],代表目前系統可配置之資源的累計
 Finish[1..n]
初值皆為false,表示某Process是否已完成,e.g. Finish[i] = true
21
Safety Algorithm
 Work[1..m] = Available[1..m]
Finish[1..n] = false
 模擬資源的分發:
while(Finish[i]==false && Need[i, j]<=Work[j]){
Finish[i] ==true
Work[j] += Allocation[i, j]
}
 檢查Finish陣列,若皆為true則系統處於safe state,如否則處
在unsafe state,可能發生deadlock
22
Banker Algorithm 範例練習
23
假設系統有三種資源A,B,C = (10, 5, 7),P0~P4資源配置如下,若
此時P1向系統申請(1, 0, 2),該資源申請會成功嗎?
P0
P1
P2
P3
P4
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Max
A B C
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Available
A B C
3 3 2
◎建議自己在旁邊拿張紙練習喔!
Request
A B C
1 0 2P1
Banker Algorithm 範例練習
①計算Need[1..n, 1..m] = Max[1..n, 1..m] – Allocation[1..n, 1..m]
24
Allocation
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Max
A B C
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Need
A B C
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
- =
Banker Algorithm 範例練習
②檢查Request(i)[1..m] <= Need[i, 1..m]
25
Need
A B C
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
Request
A B C
1 0 2
P0
P1
P2
P3
P4
P1 <=
(1, 0, 2)<=(1, 2, 2) (ok!)
Banker Algorithm 範例練習
③檢查Request(i)[1..m] <= Available[1..m]
26
Request
A B C
1 0 2P1 <=
(1, 0, 2)<=(3, 3, 2) (ok!)
Available
A B C
3 3 2
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
→ → →
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
=
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
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
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
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
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
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
Banker Algorithm 範例練習
⑤- ③檢查Finish陣列,若皆為true則系統處於safe state,
如否則處在unsafe state,可能發生deadlock
35
P0
P1
P2
P3
P4
Finish
true
true
true
true
true
Safety Queue
<P1, P3, P4, P0, P2>
由於可以找出一組Safety Queue<P1, P3, P4, P0, P2>,
且Finish陣列皆為true,因此演算法判定系統會處於safe
state,批准P1的資源申請!
分析Banker Algorithm:假設有n個Process執行的Time Complexity
計算Need[1..n, 1..m] = Max[1..n, 1..m] – Allocation[1..n, 1..m]
檢查Request(i)[1..m] <= Need[i, 1..m]
檢查Request(i)[1..m] <= Available[1..m]
Allocation[i, 1..m] += Request(i)[1..m]
Need[i, 1..m] -= Request(i)[1..m]
Available[1..m] -= Request(i)[1..m]
Work[1..m] = Available[1..m]
Finish[1..n] = false
while(Finish[i]==false && Need[i, j]<=Work){
Finish[i] ==true
Work[j] += Allocation[i, j]
}
檢查Finish陣列,若皆為true則系統處於safe state,如否則處在unsafe state,可能
發生deadlock
36
O(n x m)
O(n x m)
O(n)
O(m)
O(n)
O(n x m)
O(n x (n x m))
雖可避免deadlock但時間花費重!
(因為每申請一次就要做n x m的陣列檢查)
Deadlock Detection & Recovery (死結的偵測與回復)
• 不主動避免deadlock,而是等到deadlock發生在解開deadlock
即可
37
利用Detection Algorithm
偵測系統中所有的process
└ 若偵測到
deadlock
發生
→
Recovery方法
 結束所有Process (成本高)
 針對以下幾個process優先犧牲(釋放):
a) 最少cpu time
b) 最少工作量
c) 離deadline最遠
d) 分配最少resource
e) priority最小
→
Detection Algorithm
• 偵測所有的process的Request,看是否會有deadlock發生
• 變數宣告(假設總共有n個process及m種資源)
 Request[1..n, 1..m]
表示某Process對某資源的申請量(e.g. Request[i, j] = 1)
 Allocation[1...n, 1..m]
表示目前某Process對某資源的持有量(e.g. Allocation[i, j] = 2)
 Available[1..m]
表示某資源可用數量(e.g. Available[j] = 5)
 Work[1..m]
初值=Available[1..m],代表目前系統可配置之資源的累計
 Finish[1..n]
若某process i的Allocation[i]不為0,表示該process還握有資源未完成,因此初值設為
false;Available[i] = 0則設為true,e.g. Finish[i] = false
38
注意這和Banker Algorithm中的Safety Algorithm不同!
已知值
 初始值設定:
Work[1..m] = Available[1..m]
for(i = 1 to n){
if(Allocation[i][1..m] != 0){
Finish[i] = false
}else{Finish[i] = true}
}
 假設將資源配給給process i並釋放
while(Finish[i]==false & Request[i, j]<=Work[j]){
Work[j] += Allocation[i, j]
Finish[i] = true
}
 檢查Finish[1..n]中是否有無法完成的process
for(i = 1 to n){
if(Finish[i] == false){break} //代表process i 產生deadlock
}
39
Detection Algorithm範例練習
• 假設系統有三種資源A,B,C ,P0~P4當前資源配置及需求如下,
請問此系統會發生deadlock嗎?若有,是發生在哪幾個Process?
40
P0
P1
P2
P3
P4
Allocation
A B C
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Available
A B C
0 0 0
Detection Algorithm範例練習
①初始值設定:
Work[1..m] = Available[1..m]
for(i = 1 to n){
if(Allocation[i][1..m] != 0){
Finish[i] = false
}else{Finish[i] = true}}
41
Available
A B C
0 0 0
Work
A B C
0 0 0
P0
P1
P2
P3
P4
Finish
false
false
false
false
false
=
Allocation
A B C
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
←Allocation[i]
皆不為0因此
Finish皆設為
false
Detection Algorithm 範例練習
②假設將資源配給給process i並釋放
while(Finish[i]==false & Request[i, j]<=Work[j]){
Work[j] += Allocation[i, j]
Finish[i] = true
}
42
P0
P1
P2
P3
P4
Finish
false
false
false
false
false
Safety Queue
<null>
P0
P1
P2
P3
P4
Allocation
A B C
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Work
A B C
0 0 0
Detection Algorithm 範例練習
②假設將資源配給給process i並釋放
while(Finish[i]==false & Request[i, j]<=Work[j]){
Work[j] += Allocation[i, j]
Finish[i] = true
}
43
P0
P1
P2
P3
P4
Finish
true
false
false
false
false
Safety Queue
<P0>
P0
P1
P2
P3
P4
Allocation
A B C
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Work
A B C
0 0 0
Work
A B C
0 1 0→
+(0, 1, 0)
其實真實是先-(0, 0, 0)再+(0, 1, 0)
Detection Algorithm 範例練習
②假設將資源配給給process i並釋放
while(Finish[i]==false & Request[i, j]<=Work[j]){
Work[j] += Allocation[i, j]
Finish[i] = true
}
44
P0
P1
P2
P3
P4
Finish
true
false
true
false
false
Safety Queue
<P0, P2>
P0
P1
P2
P3
P4
Allocation
A B C
0 0 0
2 0 0
3 0 3
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Work
A B C
0 1 0
Work
A B C
3 1 3→
+(3, 0, 3)
其實真實是先-(0, 0, 0)再+(3, 0, 3)
Detection Algorithm 範例練習
②假設將資源配給給process i並釋放
while(Finish[i]==false & Request[i, j]<=Work[j]){
Work[j] += Allocation[i, j]
Finish[i] = true
}
45
P0
P1
P2
P3
P4
Finish
true
false
true
true
false
Safety Queue
<P0, P2, P3>
P0
P1
P2
P3
P4
Allocation
A B C
0 0 0
2 0 0
0 0 0
2 1 1
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
Work
A B C
3 1 3
Work
A B C
5 2 4→
+(2, 1, 1)
其實真實是先-(1, 0, 0)再+(3, 1, 1)
Detection Algorithm 範例練習
②假設將資源配給給process i並釋放
while(Finish[i]==false & Request[i, j]<=Work[j]){
Work[j] += Allocation[i, j]
Finish[i] = true
}
46
P0
P1
P2
P3
P4
Finish
true
false
true
true
true
Safety Queue
<P0, P2, P3, P4>
P0
P1
P2
P3
P4
Allocation
A B C
0 0 0
2 0 0
0 0 0
0 0 0
0 0 2
Request
A B C
0 0 0
2 0 2
0 0 0
0 0 0
0 0 2
Work
A B C
5 2 4
Work
A B C
5 2 6→
+(0, 0, 2)
其實真實是先-(0, 0, 2)再+(0, 0, 4)
Detection Algorithm 範例練習
②假設將資源配給給process i並釋放
while(Finish[i]==false & Request[i, j]<=Work[j]){
Work[j] += Allocation[i, j]
Finish[i] = true
}
47
P0
P1
P2
P3
P4
Finish
true
true
true
true
true
Safety Queue
<P0, P2, P3, P4, P1>
P0
P1
P2
P3
P4
Allocation
A B C
0 0 0
2 0 0
0 0 0
0 0 0
0 0 0
Request
A B C
0 0 0
2 0 2
0 0 0
0 0 0
0 0 0
Work
A B C
5 2 6
Work
A B C
7 2 6→
+(2, 0, 0)
其實真實是先-(2, 0, 2)再+(4, 0, 2)
Detection Algorithm 範例練習
③檢查Finish[1..n]中是否有無法完成的process
for(i = 1 to n){
if(Finish[i] == false){break}
}
48
P0
P1
P2
P3
P4
Finish
true
true
true
true
true
Safety Queue
<P0, P2, P3, P4, P1>
由於可以找出一組Safety Queue<P0, P2, P3, P4, P1>,
且Finish陣列皆為true,因此演算法判定系統沒有
deadlock發生!
Detection Algorithm分析
• 優點:系統可以知道是哪個process發生deadlock能夠針對處理
• 缺點:花費時間複雜度與Safety Algorithm一樣=O(nxnxm)
49
結論
• deadlock的發生會造成多個process無限等待,是一個非常嚴重
的系統問題
• 不論是prevention, avoidance,還是detection and recovery的方
法付出的代價都不小
• 因為正常情況deadlock發生的次數極少,因此多數的作業系統多
半會選擇忽略(不偵測也不處理)
50
Q & A
51

More Related Content

What's hot

Indeedなう B日程 解説
Indeedなう B日程 解説Indeedなう B日程 解説
Indeedなう B日程 解説AtCoder Inc.
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureTushar Gonawala
 
Dangerous on ClickHouse in 30 minutes, by Robert Hodges, Altinity CEO
Dangerous on ClickHouse in 30 minutes, by Robert Hodges, Altinity CEODangerous on ClickHouse in 30 minutes, by Robert Hodges, Altinity CEO
Dangerous on ClickHouse in 30 minutes, by Robert Hodges, Altinity CEOAltinity Ltd
 
Return to dlresolve
Return to dlresolveReturn to dlresolve
Return to dlresolveAngel Boy
 
Heap exploitation
Heap exploitationHeap exploitation
Heap exploitationAngel Boy
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
Rsa in CTF
Rsa in CTFRsa in CTF
Rsa in CTFSoL ymx
 
Topological Sort Algorithm.pptx
Topological Sort Algorithm.pptxTopological Sort Algorithm.pptx
Topological Sort Algorithm.pptxMuhammadShafi89
 
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021Altinity Ltd
 
Understanding PostgreSQL LW Locks
Understanding PostgreSQL LW LocksUnderstanding PostgreSQL LW Locks
Understanding PostgreSQL LW LocksJignesh Shah
 
美团点评技术沙龙09 - 美团外卖中的单量预估及列表优化
美团点评技术沙龙09 - 美团外卖中的单量预估及列表优化美团点评技术沙龙09 - 美团外卖中的单量预估及列表优化
美团点评技术沙龙09 - 美团外卖中的单量预估及列表优化美团点评技术团队
 
AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説AtCoder Inc.
 
CODE FESTIVAL 予選B 解説
CODE FESTIVAL 予選B 解説CODE FESTIVAL 予選B 解説
CODE FESTIVAL 予選B 解説AtCoder Inc.
 

What's hot (20)

Flowchart
FlowchartFlowchart
Flowchart
 
Quick sort
Quick sortQuick sort
Quick sort
 
Indeedなう B日程 解説
Indeedなう B日程 解説Indeedなう B日程 解説
Indeedなう B日程 解説
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
Dangerous on ClickHouse in 30 minutes, by Robert Hodges, Altinity CEO
Dangerous on ClickHouse in 30 minutes, by Robert Hodges, Altinity CEODangerous on ClickHouse in 30 minutes, by Robert Hodges, Altinity CEO
Dangerous on ClickHouse in 30 minutes, by Robert Hodges, Altinity CEO
 
請玄天上帝神咒
請玄天上帝神咒請玄天上帝神咒
請玄天上帝神咒
 
Return to dlresolve
Return to dlresolveReturn to dlresolve
Return to dlresolve
 
Heap exploitation
Heap exploitationHeap exploitation
Heap exploitation
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Rsa in CTF
Rsa in CTFRsa in CTF
Rsa in CTF
 
Topological Sort Algorithm.pptx
Topological Sort Algorithm.pptxTopological Sort Algorithm.pptx
Topological Sort Algorithm.pptx
 
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021
Building ClickHouse and Making Your First Contribution: A Tutorial_06.10.2021
 
Estrutura de dados em Java - Filas
Estrutura de dados em Java - Filas Estrutura de dados em Java - Filas
Estrutura de dados em Java - Filas
 
Kernel Pool
Kernel PoolKernel Pool
Kernel Pool
 
Understanding PostgreSQL LW Locks
Understanding PostgreSQL LW LocksUnderstanding PostgreSQL LW Locks
Understanding PostgreSQL LW Locks
 
美团点评技术沙龙09 - 美团外卖中的单量预估及列表优化
美团点评技术沙龙09 - 美团外卖中的单量预估及列表优化美团点评技术沙龙09 - 美团外卖中的单量预估及列表优化
美团点评技术沙龙09 - 美团外卖中的单量预估及列表优化
 
Os module 2 ba
Os module 2 baOs module 2 ba
Os module 2 ba
 
AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説
 
CODE FESTIVAL 予選B 解説
CODE FESTIVAL 予選B 解説CODE FESTIVAL 予選B 解説
CODE FESTIVAL 予選B 解説
 
Array
ArrayArray
Array
 

Similar to Os讀書會20170609

手把手打開Python資料分析大門
手把手打開Python資料分析大門手把手打開Python資料分析大門
手把手打開Python資料分析大門Yen-lung Tsai
 
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版逸 張
 
C语言学习100例实例程序
C语言学习100例实例程序C语言学习100例实例程序
C语言学习100例实例程序yiditushe
 
單元測試:Mocha、Chai 和 Sinon
單元測試:Mocha、Chai 和 Sinon單元測試:Mocha、Chai 和 Sinon
單元測試:Mocha、Chai 和 SinonHsin-Hao Tang
 
05 计算机的运算方法02
05 计算机的运算方法0205 计算机的运算方法02
05 计算机的运算方法02Huaijin Chen
 

Similar to Os讀書會20170609 (8)

手把手打開Python資料分析大門
手把手打開Python資料分析大門手把手打開Python資料分析大門
手把手打開Python資料分析大門
 
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
 
C语言学习100例实例程序
C语言学习100例实例程序C语言学习100例实例程序
C语言学习100例实例程序
 
單元測試:Mocha、Chai 和 Sinon
單元測試:Mocha、Chai 和 Sinon單元測試:Mocha、Chai 和 Sinon
單元測試:Mocha、Chai 和 Sinon
 
Ch10 習題
Ch10 習題Ch10 習題
Ch10 習題
 
Ch2 教學
Ch2 教學Ch2 教學
Ch2 教學
 
05 计算机的运算方法02
05 计算机的运算方法0205 计算机的运算方法02
05 计算机的运算方法02
 
Ch2
Ch2Ch2
Ch2
 

Os讀書會20170609

  • 6. 形成Deadlock的四要件 1. Mutual Exclusion (互斥) 2. Hold and Wait (持有並等待) 3. No preemption (不可搶先) 4. Circular Waiting (循環式等候) 6
  • 7. 形成Deadlock的四要件 1. Mutual Exclusion (互斥) -如ch6提到的Mutex lock,在同一時間內只能有某個Process 使用該資原,其他Process都必須等待,直到資源釋放。 2. Hold and Wait (持有並等待) 3. No preemption (不可搶先) 4. Circular Waiting (循環式等候) 7
  • 8. 形成Deadlock的四要件 1. Mutual Exclusion (互斥) 2. Hold and Wait (持有並等待) -Process持有部分資源,且正在等待其他資源使用。 3. No preemption (不可搶先) 4. Circular Waiting (循環式等候) 8
  • 9. 形成Deadlock的四要件 1. Mutual Exclusion (互斥) 2. Hold and Wait (持有並等待) 3. No preemption (不可搶先) -如ch5提到的FCFS,Process不可任意搶奪其他Process的資源 (或插隊),必需等待其Process自願釋放資源才可使用。 4. Circular Waiting (循環式等候) 9
  • 10. 形成Deadlock的四要件 1. Mutual Exclusion (互斥) 2. Hold and Wait (持有並等待) 3. No preemption (不可搶先) 4. Circular Waiting (循環式等候) -系統內存在一組Process {P0, P1, …, Pn}。其P0正在等待P1釋 放資源,P1正在等待P2釋放資源,…,Pn-1正在等待Pn,且 Pn正在等待P0,則{P0,P1,…,Pn}形成Circular Waiting。 10
  • 11. Deadlock的處理方式 1. Deadlock Prevention (死結的預防) 2. Deadlock Avoidance (死結的避免) 3. Deadlock Detection & Recovery (死結的偵測與回復) 11
  • 12. Deadlock Prevention (死結的預防) • 將Deadlock形成的四要件打破即可讓Deadlock不會發生 • 即將Mutual Exclusion (互斥)、Hold and Wait (持有並等待)、 No preemption (不可搶先)、Circular Waiting (循環式等候)其 中之一打破即可預防Deadlock發生 • 可以保證Deadlock不會發生,但會使系統效能下降 (Troughput↓) 12
  • 13. Deadlock Prevention (死結的預防) • 打破Mutual Exclusion (互斥) • 非常困難! 因為這是某些資源必需的特性(e.g. Critical Section) 13
  • 14. Deadlock Prevention (死結的預防) • 打破Hold and Wait (持有並等待) 1. 除非Process能夠取得完成工作的所有資源,才允許持有資源。 (e.g.哲學家問題→必拿兩支筷子才吃飯) 2. Process在取得新資源時,必需釋放自己本身持有的資源。 • 兩種方法都會造成一定程度的系統效能↓ 14
  • 15. Deadlock Prevention (死結的預防) • 打破No preemption (不可搶先) • 只要將系統改成preemptive即可 • 會有機會造成Starvation,但不會造成Deadlock(可用Aging解決) 15
  • 16. Deadlock Prevention (死結的預防) • 打破Circular Waiting (循環式等候) • 將系統內每個資源給予一個編號(R1,R2,…Rn),每個Process必需 按照資源編號遞增的方式提出申請。 16 Process持有資源 申請資源 結果 R1、R2 R5 申請成功 R3 R1 需釋放R3才可申請 R1、R2、R6 R4 需釋放R6才可申請
  • 17. Deadlock Avoidance (死結的避免) • 將系統分成兩種state:safe/unsafe • 在safe state下的系統不會有deadlock發生 • 在unsafe state下的系統有deadlock的風險(但不一定會發生) • deadlock avoidance的用意是使系統處於safe state的狀態,不 讓deadlock有發生的機會→Banker Algorithm 17
  • 18. Banker Algorithm • 原理類似銀行借貸的機制 a) 先看客戶有沒有能力償還? b) 再看自己的錢夠不夠? c) 最後再模擬錢借出之後,銀行還能不能運行? • 針對提出資源申請的Process,來檢視系統「是否會因接受此一 Process的申請而進入死結」 18
  • 19. Banker Algorithm • 變數宣告(假設總共有n個process及m種資源)  Request(i)[1..m] 表示某Process對某資源的申請量(e.g. Request(i)[j] = 1)  Allocation[1...n, 1..m] 表示目前某Process對某資源的持有量(e.g. Allocation[i, j] = 2)  Max[1..n, 1..m] 表示某Process對於某資源最大需求量(e.g. Max[i, j] = 3)  Need[1..n, 1..m] 表示目前Process尚需某資源的數量(e.g. Need[i, j] = 4)  Available[1..m] 表示某資源可用數量(e.g. Available[j] = 5) 19
  • 20. Banker Algorithm 假設Process i 要提出資源申請  計算Need[1..n, 1..m] = Max[1..n, 1..m] – Allocation[1..n, 1..m]  檢查Request(i)[1..m] <= Need[i, 1..m]  檢查Request(i)[1..m] <= Available[1..m]  為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]  執行Safety Algorithm,如果確定沒問題就借資源給Process i 20
  • 21. Safety Algorithm • 針對Banker Algorithm中④的假設,試算系統資源之後是否夠用 • 結果若是夠用的,就可以輸出一組Safety Queue<P1, P2,…,Pn>, 代表Deadlock不會發生 • 變數宣告:  Work[1..m] 初值=Available[1..m],代表目前系統可配置之資源的累計  Finish[1..n] 初值皆為false,表示某Process是否已完成,e.g. Finish[i] = true 21
  • 22. Safety Algorithm  Work[1..m] = Available[1..m] Finish[1..n] = false  模擬資源的分發: while(Finish[i]==false && Need[i, j]<=Work[j]){ Finish[i] ==true Work[j] += Allocation[i, j] }  檢查Finish陣列,若皆為true則系統處於safe state,如否則處 在unsafe state,可能發生deadlock 22
  • 23. Banker Algorithm 範例練習 23 假設系統有三種資源A,B,C = (10, 5, 7),P0~P4資源配置如下,若 此時P1向系統申請(1, 0, 2),該資源申請會成功嗎? P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Max A B C 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 Available A B C 3 3 2 ◎建議自己在旁邊拿張紙練習喔! Request A B C 1 0 2P1
  • 24. Banker Algorithm 範例練習 ①計算Need[1..n, 1..m] = Max[1..n, 1..m] – Allocation[1..n, 1..m] 24 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Max A B C 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 - =
  • 25. Banker Algorithm 範例練習 ②檢查Request(i)[1..m] <= Need[i, 1..m] 25 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Request A B C 1 0 2 P0 P1 P2 P3 P4 P1 <= (1, 0, 2)<=(1, 2, 2) (ok!)
  • 26. Banker Algorithm 範例練習 ③檢查Request(i)[1..m] <= Available[1..m] 26 Request A B C 1 0 2P1 <= (1, 0, 2)<=(3, 3, 2) (ok!) Available A B C 3 3 2
  • 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
  • 35. Banker Algorithm 範例練習 ⑤- ③檢查Finish陣列,若皆為true則系統處於safe state, 如否則處在unsafe state,可能發生deadlock 35 P0 P1 P2 P3 P4 Finish true true true true true Safety Queue <P1, P3, P4, P0, P2> 由於可以找出一組Safety Queue<P1, P3, P4, P0, P2>, 且Finish陣列皆為true,因此演算法判定系統會處於safe state,批准P1的資源申請!
  • 36. 分析Banker Algorithm:假設有n個Process執行的Time Complexity 計算Need[1..n, 1..m] = Max[1..n, 1..m] – Allocation[1..n, 1..m] 檢查Request(i)[1..m] <= Need[i, 1..m] 檢查Request(i)[1..m] <= Available[1..m] Allocation[i, 1..m] += Request(i)[1..m] Need[i, 1..m] -= Request(i)[1..m] Available[1..m] -= Request(i)[1..m] Work[1..m] = Available[1..m] Finish[1..n] = false while(Finish[i]==false && Need[i, j]<=Work){ Finish[i] ==true Work[j] += Allocation[i, j] } 檢查Finish陣列,若皆為true則系統處於safe state,如否則處在unsafe state,可能 發生deadlock 36 O(n x m) O(n x m) O(n) O(m) O(n) O(n x m) O(n x (n x m)) 雖可避免deadlock但時間花費重! (因為每申請一次就要做n x m的陣列檢查)
  • 37. Deadlock Detection & Recovery (死結的偵測與回復) • 不主動避免deadlock,而是等到deadlock發生在解開deadlock 即可 37 利用Detection Algorithm 偵測系統中所有的process └ 若偵測到 deadlock 發生 → Recovery方法  結束所有Process (成本高)  針對以下幾個process優先犧牲(釋放): a) 最少cpu time b) 最少工作量 c) 離deadline最遠 d) 分配最少resource e) priority最小 →
  • 38. Detection Algorithm • 偵測所有的process的Request,看是否會有deadlock發生 • 變數宣告(假設總共有n個process及m種資源)  Request[1..n, 1..m] 表示某Process對某資源的申請量(e.g. Request[i, j] = 1)  Allocation[1...n, 1..m] 表示目前某Process對某資源的持有量(e.g. Allocation[i, j] = 2)  Available[1..m] 表示某資源可用數量(e.g. Available[j] = 5)  Work[1..m] 初值=Available[1..m],代表目前系統可配置之資源的累計  Finish[1..n] 若某process i的Allocation[i]不為0,表示該process還握有資源未完成,因此初值設為 false;Available[i] = 0則設為true,e.g. Finish[i] = false 38 注意這和Banker Algorithm中的Safety Algorithm不同! 已知值
  • 39.  初始值設定: Work[1..m] = Available[1..m] for(i = 1 to n){ if(Allocation[i][1..m] != 0){ Finish[i] = false }else{Finish[i] = true} }  假設將資源配給給process i並釋放 while(Finish[i]==false & Request[i, j]<=Work[j]){ Work[j] += Allocation[i, j] Finish[i] = true }  檢查Finish[1..n]中是否有無法完成的process for(i = 1 to n){ if(Finish[i] == false){break} //代表process i 產生deadlock } 39
  • 40. Detection Algorithm範例練習 • 假設系統有三種資源A,B,C ,P0~P4當前資源配置及需求如下, 請問此系統會發生deadlock嗎?若有,是發生在哪幾個Process? 40 P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 3 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 0 1 0 0 0 0 2 Available A B C 0 0 0
  • 41. Detection Algorithm範例練習 ①初始值設定: Work[1..m] = Available[1..m] for(i = 1 to n){ if(Allocation[i][1..m] != 0){ Finish[i] = false }else{Finish[i] = true}} 41 Available A B C 0 0 0 Work A B C 0 0 0 P0 P1 P2 P3 P4 Finish false false false false false = Allocation A B C 0 1 0 2 0 0 3 0 3 2 1 1 0 0 2 ←Allocation[i] 皆不為0因此 Finish皆設為 false
  • 42. Detection Algorithm 範例練習 ②假設將資源配給給process i並釋放 while(Finish[i]==false & Request[i, j]<=Work[j]){ Work[j] += Allocation[i, j] Finish[i] = true } 42 P0 P1 P2 P3 P4 Finish false false false false false Safety Queue <null> P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 3 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 0 1 0 0 0 0 2 Work A B C 0 0 0
  • 43. Detection Algorithm 範例練習 ②假設將資源配給給process i並釋放 while(Finish[i]==false & Request[i, j]<=Work[j]){ Work[j] += Allocation[i, j] Finish[i] = true } 43 P0 P1 P2 P3 P4 Finish true false false false false Safety Queue <P0> P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 3 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 0 1 0 0 0 0 2 Work A B C 0 0 0 Work A B C 0 1 0→ +(0, 1, 0) 其實真實是先-(0, 0, 0)再+(0, 1, 0)
  • 44. Detection Algorithm 範例練習 ②假設將資源配給給process i並釋放 while(Finish[i]==false & Request[i, j]<=Work[j]){ Work[j] += Allocation[i, j] Finish[i] = true } 44 P0 P1 P2 P3 P4 Finish true false true false false Safety Queue <P0, P2> P0 P1 P2 P3 P4 Allocation A B C 0 0 0 2 0 0 3 0 3 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 0 1 0 0 0 0 2 Work A B C 0 1 0 Work A B C 3 1 3→ +(3, 0, 3) 其實真實是先-(0, 0, 0)再+(3, 0, 3)
  • 45. Detection Algorithm 範例練習 ②假設將資源配給給process i並釋放 while(Finish[i]==false & Request[i, j]<=Work[j]){ Work[j] += Allocation[i, j] Finish[i] = true } 45 P0 P1 P2 P3 P4 Finish true false true true false Safety Queue <P0, P2, P3> P0 P1 P2 P3 P4 Allocation A B C 0 0 0 2 0 0 0 0 0 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 0 1 0 0 0 0 2 Work A B C 3 1 3 Work A B C 5 2 4→ +(2, 1, 1) 其實真實是先-(1, 0, 0)再+(3, 1, 1)
  • 46. Detection Algorithm 範例練習 ②假設將資源配給給process i並釋放 while(Finish[i]==false & Request[i, j]<=Work[j]){ Work[j] += Allocation[i, j] Finish[i] = true } 46 P0 P1 P2 P3 P4 Finish true false true true true Safety Queue <P0, P2, P3, P4> P0 P1 P2 P3 P4 Allocation A B C 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 Request A B C 0 0 0 2 0 2 0 0 0 0 0 0 0 0 2 Work A B C 5 2 4 Work A B C 5 2 6→ +(0, 0, 2) 其實真實是先-(0, 0, 2)再+(0, 0, 4)
  • 47. Detection Algorithm 範例練習 ②假設將資源配給給process i並釋放 while(Finish[i]==false & Request[i, j]<=Work[j]){ Work[j] += Allocation[i, j] Finish[i] = true } 47 P0 P1 P2 P3 P4 Finish true true true true true Safety Queue <P0, P2, P3, P4, P1> P0 P1 P2 P3 P4 Allocation A B C 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 Request A B C 0 0 0 2 0 2 0 0 0 0 0 0 0 0 0 Work A B C 5 2 6 Work A B C 7 2 6→ +(2, 0, 0) 其實真實是先-(2, 0, 2)再+(4, 0, 2)
  • 48. Detection Algorithm 範例練習 ③檢查Finish[1..n]中是否有無法完成的process for(i = 1 to n){ if(Finish[i] == false){break} } 48 P0 P1 P2 P3 P4 Finish true true true true true Safety Queue <P0, P2, P3, P4, P1> 由於可以找出一組Safety Queue<P0, P2, P3, P4, P1>, 且Finish陣列皆為true,因此演算法判定系統沒有 deadlock發生!
  • 50. 結論 • deadlock的發生會造成多個process無限等待,是一個非常嚴重 的系統問題 • 不論是prevention, avoidance,還是detection and recovery的方 法付出的代價都不小 • 因為正常情況deadlock發生的次數極少,因此多數的作業系統多 半會選擇忽略(不偵測也不處理) 50