๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป CS/์šด์˜์ฒด์ œ

[์šด์˜์ฒด์ œ] 7. Deadlocks

by nitronium102 2023. 1. 1.

Deadlock Problem

1) Deadlock : ์ผ๋ จ์˜ ํ”„๋กœ์„ธ์Šค๋“ค์ด ์„œ๋กœ๊ฐ€ ๊ฐ€์ง„ SW/HW ์ž์›์„ ๊ธฐ๋‹ค๋ฆฌ๋ฉฐ block๋œ ์ƒํƒœ

2) Resource : HW, SW ๋“ฑ์„ ํฌํ•จํ•˜๋Š” ๊ฐœ๋…

- I/O device, CPU cycle, memory space, semaphore ๋“ฑ

- ์ž์› ์‚ฌ์šฉ ์ ˆ์ฐจ : Request(์š”์ฒญ) > Allocate(ํ• ๋‹น) > Use(์‚ฌ์šฉ) > Release(๋ฐ˜๋‚ฉ)

 

Deadlock์ด ๋ฐœ์ƒํ•˜๋Š” 4๊ฐ€์ง€ ์กฐ๊ฑด๐Ÿ”ฅ

1) Mutual Exclusion(์ƒํ˜ธ ๋ฐฐ์ œ) : ๋งค ์ˆœ๊ฐ„ ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๋งŒ์ด ์ž์›์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

2) No preemption(๋น„์„ ์ ) : ํ”„๋กœ์„ธ์Šค๋Š” ์ž์›์„ ์Šค์Šค๋กœ ๋‚ด์–ด๋†“์„ ๋ฟ, ๊ฐ•์ œ๋กœ ๋นผ์•—๊ธฐ์ง€ ์•Š์Œ

3) Hold and wait(๋ณด์œ ๋Œ€๊ธฐ) : ์ž์›์„ ๊ฐ€์ง„ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋‹ค๋ฅธ ์ž์›์„ ๊ธฐ๋‹ค๋ฆด ๋•Œ, ๋ณด์œ  ์ž์›์„ ๋†“์ง€ ์•Š๊ณ  ๊ณ„์† ๊ฐ€์ง€๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ์Œ

4) Circular wait(์ˆœํ™˜๋Œ€๊ธฐ) : ์ž์›์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ํ”„๋กœ์„ธ์Šค ๊ฐ„์— ์‚ฌ์ดํด์ด ํ˜•์„ฑ๋˜์–ด์•ผ ํ•จ

 

Resource-Allocation graph(์ž์›ํ• ๋‹น๊ทธ๋ž˜ํ”„)

ํ”„๋กœ์„ธ์Šค->์ž์› : ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ž์›์„ ์š”์ฒญํ•˜์˜€๊ณ , ์•„์ง ์ž์› ์–ป์ง€ ๋ชปํ•จ

์ž์›->ํ”„๋กœ์„ธ์Šค : ์ž์›์ด ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ํ• ๋‹น๋จ

 

* ๊ทธ๋ž˜ํ”„์— cycle์ด ์—†์œผ๋ฉด deadlock์ด ์•„๋‹ˆ๋‹ค!

* ๊ทธ๋ž˜ํ”„์— cycle์ด ์žˆ์œผ๋ฉด 

1) ์ž์› ๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜๋ฐ–์— ์—†์œผ๋ฉด, ๋ฌด์กฐ๊ฑด deadlock

2) ์ž์› ๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์œผ๋ฉด, deadlock์ผ ์ˆ˜ ์žˆ์Œ!

(์ขŒ) deadlock, (์šฐ) cycle์ด ์žˆ์ง€๋งŒ not deadlock

 

Deadlock์˜ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•

1) ์‚ฌ์ „ ๋ฐฉ์ง€

(1) Deadlock Prevention : ์ž์› ํ• ๋‹น ์‹œ deadlock์˜ 4๊ฐ€์ง€ ํ•„์š” ์กฐ๊ฑด ์ค‘ ์–ด๋Š ํ•˜๋‚˜๊ฐ€ ๋งŒ์กฑ๋˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ

(2) Deadlock Avoidance : deadlock์˜ ๊ฐ€๋Šฅ์„ฑ์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ž์›์„ ํ• ๋‹น(์‹œ์Šคํ…œ state๊ฐ€ ์›๋ž˜ state๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ž์› ํ• ๋‹น)

 

2) Deadlock Detection & Recovery

3) Deadlock Ignorance : deadlock์„ ์‹œ์Šคํ…œ์ด ์ฑ…์ž„์ง€์ง€ ์•Š์Œ, ๋Œ€๋ถ€๋ถ„์˜ OS๊ฐ€ ์ฑ„ํƒ

* deadlock์€ ์ž์ฃผ ์ผ์–ด๋‚˜๋Š” ์ด๋ฒคํŠธ๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋น„ํ•˜๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ large overhead

 

1. Deadlock Prevention

์‚ฌ์ „์— ๋ชจ๋“  deadlock์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— utilization ์ €ํ•˜, throughput ๊ฐ์†Œ, starvation ๋ฌธ์ œ ๋ฐœ์ƒ ๊ฐ€๋Šฅ

1) Mutual Exclusion(์ƒํ˜ธ ๋ฐฐ์ œ) : ๊ณต์œ ํ•ด์„œ๋Š” ์•ˆ๋˜๋Š” ์ž์›์˜ ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ ์„ฑ๋ฆฝํ•ด์•ผ ํ•จ (๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ๋กœ๋Š” ์„ฑ๋ฆฝํ•˜๊ธฐ ์–ด๋ ค์›€)

2) Hold and wait(๋ณด์œ ๋Œ€๊ธฐ)  : ํ”„๋กœ์„ธ์Šค๋Š” ์ž์›์„ ์š”์ฒญํ•  ๋•Œ ๋‹ค๋ฅธ ์ž์›๋„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค

- ํ”„๋กœ์„ธ์Šค ์‹œ์ž‘ ์‹œ ํ•„์š”ํ•œ ๋ชจ๋“  ์ž์›์„ ํ• ๋‹น๋ฐ›๋Š” ๋ฐฉ๋ฒ• - ์ž์›์ด ๋‚ญ๋น„๋  ์ˆ˜ ์žˆ์Œ

- ์ž์›์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋ณด์œ ํ•œ ์ž์›์„ ๋ชจ๋‘ ๋†“์–ด๋‘๊ณ  ๋‹ค์‹œ ์š”์ฒญ(์ž์ง„๋ฐ˜๋‚ฉ)

3) No preemption(๋น„์„ ์ ) : process๊ฐ€ ๋Œ€๊ธฐ์ƒํƒœ์ธ ๊ฒฝ์šฐ ๋ณด์œ ํ•œ ์ž์› ๋บ๊ธธ ์ˆ˜ ์žˆ์Œ(๊ฐ•์ œ)

- ์‚ฌ์šฉ ์ƒํƒœ๋ฅผ ์‰ฝ๊ฒŒ save & restoreํ•  ์ˆ˜ ์žˆ๋Š” ์ž์›์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ(CPU, memory)

4) Circular wait(์ˆœํ™˜๋Œ€๊ธฐ) : ๋ชจ๋“  ์ž์› ์œ ํ˜•์— ํ• ๋‹น ์ˆœ์„œ๋ฅผ ์ •ํ•˜์—ฌ ์ˆœ์„œ๋Œ€๋กœ ์ž์› ํ• ๋‹น

 

2. Deadlock Avoidance

ํ•ญ์ƒ safe state๋ฅผ ์œ ์ง€

ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹œ์ž‘๋  ๋•Œ, ํ•„์š”๋กœ ํ•˜๋Š” ๊ฐ ์ž์›์˜ ์ตœ๋Œ€ ์‚ฌ์šฉ๋Ÿ‰์„ ๋ฏธ๋ฆฌ ์„ ์–ธํ•˜๋„๋ก ํ•จ

 

* safe state : ์‹œ์Šคํ…œ ๋‚ด์˜ ํ”„๋กœ์„ธ์Šค๋“ค์— ๋Œ€ํ•œ safe sequence๊ฐ€ ์กด์žฌํ•˜๋Š” ์ƒํƒœ

* safe sequence : ํ”„๋กœ์„ธ์Šค์˜ ์ž์› ์š”์ฒญ์ด <๊ฐ€์šฉ ์ž์› + ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์˜ ๋ณด์œ  ์ž์›>์— ์˜ํ•ด ์ถฉ์กฑ๋˜์–ด์•ผ ํ•จ

ํ•ญ์ƒ safe state๋ฅผ ์œ ์ง€ํ•จ

 

1) ์ž์› ๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ : Resource Allocation Graph ์‚ฌ์šฉ

2) ์ž์› ๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ : Banker's Algorithm ์‚ฌ์šฉ

 

(1) Resource Allocation Graph Algorithm

์ตœ์•…์˜ ์ƒํ™ฉ์„ ๊ฐ€์ • - ๊ฐ€๋Šฅํ•œ ์ž์›์ด ์žˆ๋‹ค๊ณ  ํ•ด์„œ ๋‹ค ๋‚ด์–ด์ฃผ์ง€ ์•Š๋Š”๋‹ค

์‹ค์„  : ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ•ด๋‹น ์ž์›์„ ์š”์ฒญํ–ˆ๋‹ค.

์ ์„  : ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ•ด๋‹น ์ž์›์„ ์š”์ฒญํ•  ์ผ์ด ์žˆ๋‹ค

Process 2๋ฒˆ์ด Resource 2๋ฒˆ์„ ์š”์ฒญํ•œ ๊ฒฝ์šฐ -> ์ ์„ ์„ ํฌํ•จํ•œ ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•œ๋‹ค(not deadlock)

 

(2) Banker's Algorithm

Need๊ฐ€ Available๋กœ ์ถฉ์กฑ์ด ๋˜๋Š” ๊ฒฝ์šฐ ์ž์›์„ ํ• ๋‹น

- Allocation : ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ํ• ๋‹น๋œ ์ž์›๋Ÿ‰

- Max : ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ์ž์›์˜ ์ตœ๋Œ€ ์‚ฌ์šฉ๋Ÿ‰

- Available : ๊ฐ€์šฉ ์ž์›๋Ÿ‰

- Need : ์ถ”๊ฐ€ ์š”์ฒญ ๊ฐ€๋Šฅ๋Ÿ‰

 

P1์ด need ๋งŒ์กฑ -> P1 ์ข…๋ฃŒํ•˜๋ฉด์„œ allocation + need ๋ฐ˜ํ™˜ (available ์ฆ๊ฐ€)

-> P3์ด need ๋งŒ์กฑ -> P3 ์ข…๋ฃŒํ•˜๋ฉด์„œ allocation + need ๋ฐ˜ํ™˜ (available ์ฆ๊ฐ€) ...

 

3. Deadlock Detection & Recovery

[ Deadlock Detection ]

1) ์ž์› ๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ : Resource Allocation Graph / Wait-for graph ์‚ฌ์šฉ

- wait-for graph : ํ”„๋กœ์„ธ์Šค๋งŒ์œผ๋กœ node๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค

- ํ”„๋กœ์„ธ์Šค๊ฐ€ n๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ฐ ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ํ™”์‚ดํ‘œ๋Š” ์ตœ๋Œ€ (n-1)๊ฐœ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค - ํƒ์ƒ‰ ์‹œ๊ฐ„ O(n^2)

์ž์›์„ ์ œ๊ฑฐํ•˜๊ณ  ํ”„๋กœ์„ธ์Šค๋ผ๋ฆฌ์˜ ๊ด€๊ณ„๋งŒ ๊ทธ๋ฆผ

 

2) ์ž์› ๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ : Banker's Algorithm ์‚ฌ์šฉ

- Allocation : ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ํ• ๋‹น๋œ ์ž์›๋Ÿ‰

- Request : ํ˜„์žฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์š”์ฒญํ•œ ์ž์›๋Ÿ‰

- Available : ๊ฐ€์šฉ ์ž์›๋Ÿ‰

 

* ๋‚™๊ด€์  ๊ฐ€์ • : ํ˜„์žฌ Request๊ฐ€ 0์ธ ๊ฒฝ์šฐ, Allocation์„ ๋ฐ˜๋‚ฉํ•  ๊ฒƒ์ด๋ผ๊ณ  ๊ฐ€์ •

 

1) ํ˜„์žฌ Request๊ฐ€ 0์ธ ๊ฒฝ์šฐ ๊ฐ€์šฉ ์ž์›์— ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค์˜ ์ž์› ํฌํ•จ

2) ๊ฐ€์šฉ ์ž์›์œผ๋กœ Request ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ 

- ๋ชจ๋“  Request๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด deadlock ๋ฐœ์ƒ

deadlock ๋ฐœ์ƒ

 

[ Deadlock Recovery ]

1) Process Termination(ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ)

- deadlock์— ์—ฐ๋ฃจ๋œ ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๋ฅผ abort

- deadlock์ด ์—†์–ด์งˆ ๋•Œ๊นŒ์ง€ deadlock์— ์—ฐ๋ฃจ๋œ ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๋ฅผ ํ•˜๋‚˜์”ฉ abortํ•ด๋ด„

 

2) Resource Preemption(์ž์› ๋บ๊ธฐ)

- ๋น„์šฉ์„ ์ตœ์†Œํ™”ํ•  victim์„ ์„ ์ • - safe state๋กœ rollbackํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค restart

- starvation ๋ฌธ์ œ : ๋™์ผํ•œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ณ„์†ํ•ด์„œ victim์œผ๋กœ ์„ ์ •๋˜๋Š” ๊ฒฝ์šฐ - rollback ํšŸ์ˆ˜๋„ ํ•จ๊ป˜ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค

 

4. Deadlock Ignorance

Deadlock์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์•„๋ฌด๋Ÿฐ ์กฐ์น˜๋„ ์ทจํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•

Deadlock์ด ๋งค์šฐ ๋“œ๋ฌผ๊ฒŒ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ deadlock์— ๋Œ€ํ•œ ์กฐ์น˜ ์ž์ฒด๊ฐ€ ๋” ํฐ overhead์ผ ์ˆ˜ ์žˆ์Œ

๋Œ“๊ธ€