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

[์šด์˜์ฒด์ œ] 6. Process Synchronization

by nitronium102 2022. 12. 25.

๊ณต์œ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ

do {
	entry section
    critical section
    exit section
    remainder section
} while (1)

 

ํ”„๋กœ๊ทธ๋žจ์  ํ•ด๊ฒฐ๋ฒ•์˜ ์ถฉ์กฑ ์กฐ๊ฑด

1) Mutual Exclusion(์ƒํ˜ธ ๋ฐฐ์ œ)

ํŠน์ • ํ”„๋กœ์„ธ์Šค๊ฐ€ critical section์„ ์ˆ˜ํ–‰ ์ค‘์ด๋ฉด ๋‹ค๋ฅธ ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๋“ค์€ critical section์— ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ ๋œ๋‹ค

2) Progress

์•„๋ฌด๋„ critical section์— ์žˆ์ง€ ์•Š์€ ์ƒํƒœ์—์„œ critical section์— ๋“ค์–ด๊ฐ€๊ณ ์ž ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด critical section์— ๋“ค์–ด๊ฐ€๊ฒŒ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

3) Bounded Waiting(์œ ํ•œ ๋Œ€๊ธฐ)

ํ”„๋กœ์„ธ์Šค๊ฐ€ critical section์— ๋“ค์–ด๊ฐ€๋ ค๊ณ  ์š”์ฒญํ•œ ํ›„๋ถ€ํ„ฐ ๊ทธ ์š”์ฒญ์ด ํ—ˆ์šฉ๋  ๋•Œ๊นŒ์ง€ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋“ค์ด critical section์— ๋“ค์–ด๊ฐ€๋Š” ํšŸ์ˆ˜์— ํ•œ๊ณ„๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค

 

Peterson's Algorithm

1) sync var 

- int turn, initially turn = 0; / turn == 1์ด๋ฉด ready

- boolean flag[2] = false; / flag[i] == true์ด๋ฉด ready

 

2) algorithm

3) result

- ์„ธ ๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•œ๋‹ค

- ๋ฌธ์ œ : ๊ณ„์† CPU์™€ Memory๋ฅผ ์“ฐ๋ฉด์„œ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— Busy Waiting(while๋ฌธ์„ ๋Œ๋ฉด์„œ ๋ง‰๋Š” Spin Lock) ๋ฐœ์ƒํ•œ๋‹ค

 

Test and Set

ํ•˜๋“œ์›จ์–ด์ ์œผ๋กœ test & modify๋ฅผ atomicํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” instruction

 

์ง€๊ธˆ๊นŒ์ง€์˜ ๋ฌธ์ œ๋Š” read & write๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ–ˆ์Œ.

> read, write๋ฅผ ํ•˜๋‚˜์˜ instruction์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜๋ฉด ์†Œํ”„ํŠธ์›จ์–ด์ ์œผ๋กœ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ์งค ํ•„์š”๊ฐ€ ์—†์–ด์ง

> ๋ฐ์ดํ„ฐ์˜ ํ˜„์žฌ ๊ฐ’์„ ์ฝ๊ณ  / ๊ฐ’์„ 1๋กœ ๋ฐ”๊พธ์–ด์คŒ

1) ํ˜„์žฌ lock์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด, lock = 1๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  critical section์— ๋“ค์–ด๊ฐ

2) ํ˜„์žฌ lock์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฉด, while๋ฌธ์„ ๋Œ๋‹ค๊ฐ€ lock ํ’€๋ฆฐ ํ›„์— critical section์— ๋“ค์–ด๊ฐ

Semaphores

๊ฐœ๋ฐœ์ž์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด ์•ž์˜ ๋ฐฉ์‹๋“ค์„ ์ถ”์ƒํ™”์‹œํ‚ด

 

- integer variable S : ๊ณต์œ  ์ž์›์˜ ๊ฐœ์ˆ˜

- ๋‘ ๊ฐ€์ง€ atomic ์—ฐ์‚ฐ์— ์˜ํ•ด์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ

1) P์—ฐ์‚ฐ : ๊ณต์œ  ์ž์›์„ ํš๋“ํ•˜๋Š” ๊ณผ์ • (lock์„ ๊ฑฐ๋Š” ๊ณผ์ •) -> busy-waiting ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ!

2) V์—ฐ์‚ฐ : ๊ณต์œ  ์ž์›์„ ๋ฐ˜๋‚ฉํ•˜๋Š” ๊ณผ์ • (lock์„ ํ‘ธ๋Š” ๊ณผ์ •)

1) Critical Section Implementation

- ๋‹จ์  : busy-waiting

2) Block & Wakeup Implementation(Sleep)

1) semaphore

typedef struct {
	int value; 			// semaphore
    struct process *L; 	// process wait queue
} semaphore;

 

2) block & wakeup

- block : block์„ ํ˜ธ์ถœํ•œ ํ”„๋กœ์„ธ์Šค๋ฅผ suspend ์‹œํ‚ด / ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค์˜ PCB๋ฅผ semaphore์— ๋Œ€ํ•œ wait queue์— ๋„ฃ์Œ

S < 0์ด๋ฉด ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ž์›์„ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ์Œ(๊นจ์›Œ์•ผ ํ•  ๊ฒƒ์ด ์žˆ์Œ)

- wakeup(P) : block๋œ ํ”„๋กœ์„ธ์Šค๋ฅผ wakeup ์‹œํ‚ด / ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค์˜ PCB๋ฅผ ready queue์— ๋„ฃ์Œ

 

3) ๋‘˜์˜ ๋น„๊ต

- ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” block & wakeup์„ ์“ฐ๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ž„

- ๊ทธ๋Ÿฌ๋‚˜ critical section์˜ ๊ธธ์ด๊ฐ€ ๋งค์šฐ ์งง์€ ๊ฒฝ์šฐ์—๋Š” block & wakeup์˜ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ busy-wait ์˜ค๋ฒ„ํ—ค๋“œ๋ณด๋‹ค ์ปค์งˆ ์ˆ˜ ์žˆ์Œ

 

Two types of Semaphores

1) counting semaphores : ์ž์›์˜ ๊ฐœ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ

2) binary semaphore(mutex) : ์ž์›์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ๋ฐ–์— ์—†๋Š” ๊ฒฝ์šฐ

 

Deadlock and Starvation

1) Deadlock : ๋‘˜ ์ด์ƒ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์„œ๋กœ ์ƒ๋Œ€๋ฐฉ์— ์˜ํ•ด ์ถฉ์กฑ๋  ์ˆ˜ ์žˆ๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ฌดํ•œํžˆ ๊ธฐ๋‹ค๋ฆฌ๋Š” ํ˜„์ƒ

-> ์ž์›์„ ์–ป๋Š” ์ˆœ์„œ๋ฅผ ์ •ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ(ex. P๋ฅผ ์–ป๊ณ  ๋‚˜์„œ Q๋ฅผ ์–ป์–ด๋ผ)

2) Starvation : ํ”„๋กœ์„ธ์Šค๊ฐ€ suspend๋œ ์ด์œ ์— ํ•ด๋‹นํ•˜๋Š” semaphore ํ์—์„œ ๋น ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์—†๋Š” ํ˜„์ƒ(infinite locking)

ex) dining-philisophers problem : ํŠน์ • ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ž์›์„ ๋…์ ํ•˜๊ณ  ์žˆ์–ด์„œ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

 

Classical Problems of Synchronization

1) Bounded-Buffer Problem(Producer-Consumer Problem)

์œ ํ•œํ•œ ๋ฒ„ํผ. producer๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , consumer๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ธ๋‹ค.

1) shared data : buffer ์ž์ฒด ๋ฐ buffer ์กฐ์ž‘ ๋ณ€์ˆ˜(emtpy/full buffer์˜ ์‹œ์ž‘ ์œ„์น˜)

- ๋™์ผํ•œ ์œ„์น˜์— producer๋ผ๋ฆฌ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ consumer๋ผ๋ฆฌ ์ ‘๊ทผํ•˜๋ฉด ๋ฌธ์ œ ๋ฐœ์ƒ ๊ฐ€๋Šฅ

2) synchronization var

- mutual exclusion : shared data์˜ mutual exclusion(binary semaphore ํ•„์š”)

- resource count : ๋‚จ์€ full/emtpy buffer์˜ ์ˆ˜(integer semaphore ํ•„์š”)

 semaphore full = 0, empty = n, mutex = 1

 

2) Readers-Writers Problem

1) write & read

- write : ํ•œ process๊ฐ€ DB์— write ์ค‘์ผ ๋•Œ ๋‹ค๋ฅธ process๊ฐ€ ์ ‘๊ทผํ•˜๋ฉด ์•ˆ ๋จ

- read : ๋™์‹œ์— ์—ฌ๋Ÿฟ์ด ํ•ด๋„ ๋จ

 

- writer๋Š” ๋Œ€๊ธฐ ์ค‘์ธ reader๊ฐ€ ํ•˜๋‚˜๋„ ์—†์„ ๋•Œ DB ์ ‘๊ทผ์ด ํ—ˆ์šฉ

- wirter๊ฐ€ DB์—์„œ ๋น ์ ธ๋‚˜๊ฐ€์•ผ๋งŒ reader์˜ ์ ‘๊ทผ์ด ํ—ˆ์šฉ

 

2) shared data

int readcount = 0;
DB ์ž์ฒด;

 

3) sync var

semaphore mutex = 1, db = 1;

- ์ตœ์ดˆ์˜ reader : writer๋กœ ์ธํ•ด ๋‚ด์šฉ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด DB์— lock์„ ๊ฑธ๊ณ  ์ฝ๋Š”๋‹ค

- 2๋ฒˆ์งธ~ reader : ์ด๋ฏธ DB์— lock์ด ๊ฑธ๋ ค ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ฌด๋Ÿฐ ์กฐ์น˜ ์—†์ด ๊ทธ๋ƒฅ ์ฝ๋Š”๋‹ค

 

3) Dining-Philosopher Problem

1) ๋ฌธ์ œ : deadlock ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Œ

2) ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

- 4๋ช…์˜ ์ฒ ํ•™์ž๋งŒ์ด ํ…Œ์ด๋ธ”์— ๋™์‹œ์— ์•‰์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•จ

- ์ “๊ฐ€๋ฝ์„ ๋‘ ๊ฐœ ๋ชจ๋‘ ์ง‘์„ ์ˆ˜ ์žˆ์„ ๋•Œ๋งŒ ์ “๊ฐ€๋ฝ์„ ์ง‘์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•จ

- ๋น„๋Œ€์นญ : ์ง์ˆ˜(ํ™€์ˆ˜) ์ฒ ํ•™์ž๋Š” ์™ผ์ชฝ(์˜ค๋ฅธ์ชฝ) ์ “๊ฐ€๋ฝ๋ถ€ํ„ฐ ์ง‘๋„๋ก ํ•จ

 

semaphore ๋ฒ„์ „์˜ ํ•ด๊ฒฐ๋ฒ•

testํ•จ์ˆ˜์—์„œ ์ “๊ฐ€๋ฝ 2๊ฐœ๋ฅผ ๋ชจ๋‘ ์ง‘์„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ

1) ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์‚ฌ๋žŒ์ด ์ “๊ฐ€๋ฝ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ

2) ๋‚ด๊ฐ€ ๋ฐฐ๊ฐ€ ๊ณ ํ”ˆ ๊ฒฝ์šฐ

 

Monitor

* semaphore์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋„์ž…

- ์ •ํ™•ํ•˜๊ฒŒ ์ฝ”๋”ฉํ•˜๊ธฐ ํž˜๋“ค๋‹ค

- ์ •ํ™•์„ฑ correctness์˜ ์ž…์ฆ์ด ์–ด๋ ต๋‹ค

- ์ž๋ฐœ์  ํ˜‘๋ ฅ voluntary cooperation์ด ํ•„์š”ํ•˜๋‹ค

- ํ•œ ๋ฒˆ์˜ ์‹ค์ˆ˜๊ฐ€ ๋ชจ๋“  ์‹œ์Šคํ…œ์— ์น˜๋ช…์ ์ธ ์˜ํ–ฅ

 

* ๋ชจ๋‹ˆํ„ฐ ๋‚ด๋ถ€ procedure๋ฅผ ํ†ตํ•ด์„œ๋งŒ shared data์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ

์ž๋™์ ์œผ๋กœ ๋‚ด๋ถ€ procedure์˜ ๋™์‹œ ์‹คํ–‰์„ ๋ง‰๊ธฐ ๋•Œ๋ฌธ์—(ํ•˜๋‚˜๋งŒ ํ™œ์„ฑํ™”) ๊ฐœ๋ฐœ์ž๊ฐ€ lock์„ ๋”ฐ๋กœ ๊ฑธ ํ•„์š”๊ฐ€ ์—†์Œ

 

* condition variable : wait(), signal() ์—ฐ์‚ฐ

1) x.wait() : ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๊ฐ€ x.signal()์„ ํ˜ธ์ถœํ•˜๊ธฐ ์ „๊นŒ์ง€ suspend๋œ๋‹ค

2) x.signal() : ํ•˜๋‚˜์˜ suspen๋œ ํ”„๋กœ์„ธ์Šค๋ฅผ resumeํ•œ๋‹ค

 

1) Bounded-Buffer Problem(Producer-Consumer Problem)

semaphore์—์„œ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๊ฐ’์„ ๊ฐ€์ง€์ง€๋งŒ, monitor์—์„œ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๊ฐ’์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ๋Œ€์‹  ์กฐ๊ฑด ๋งŒ์กฑ ์—ฌ๋ถ€๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฒดํฌํ•œ๋‹ค

 

2) Dining-Philosopher Problem

์ “๊ฐ€๋ฝ์„ ์žก์€ ํ›„์˜ ์ฝ”๋“œ๋Š” ๋ชจ๋‹ˆํ„ฐ ๋‚ด๋ถ€์— ์กด์žฌ

๋Œ“๊ธ€