ํ๋ก์ธ์ค ๋๊ธฐํ
์ฌ๋ฌ ํ๋ก์ธ์ค๊ฐ ๊ณต์ ์์์ ์ ๊ทผํ๋ค๋ฉด race condition(๊ฒฝํฉ์กฐ๊ฑด)์ด ๋ฐ์ํ๋ค.
์ด๋ฌํ ์ํฉ์ ๋ง๊ธฐ ์ํด์ ํ์ํ mechanism์ด ์๋ค.
๐น Locking Mechanism
- ์ด๋ค ์์
์ด ์ํ๋๋ ๊ฒ์ ๋ง๋๋ค
- ๐ before entering critical section
- ๐ before accessing shared data

- critical section(์๊ณ์์ญ)์ ์ต๋ํ ์งง๊ฒ ์ค์ ํด์ผํ๋ค
- multi-processors์์ interrupt-based locks์ ๊ตฌํํ๊ธฐ ์ด๋ ค์ ๐ read-write instructions ์ผ๋ก ํด๊ฒฐ
๐น Semaphore
- Synchronization tool
- Counting semaphore
- Binary semaphore โ 0 ๋๋ 1 ๊ฐ๋ง ๊ฐ์ง. mutex lock
- ์ฌ๋ฌ ๊ฐ์ ํ๋ก์ธ์ค๋ค์ด ์์(๋ฉ๋ชจ๋ฆฌ)์ ๊ณต์ ํ ๋ ํ๋์ ํ๋ก์ธ์ค๋ง ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ํจ = mutual exclusion = mutex
wait()
=P()
- waits for semaphore to become positive, then decrements it by 1
- ์์ ํ๋ํ์ง ๋ชปํ๋ฉด block()
signal()
=V()
- increments the semaphore by 1, waking up a waiting P
- ์์ ๋ฐ๋ฉํ๋ฉด wakeup()
- Semaphore Implementation with no busy waiting
Block()
: put the process on the waiting queue
โ ์ปค๋์ block์ ํธ์ถํ ํ๋ก์ธ์ค๋ฅผ suspend์ํค๊ณ ์ด ํ๋ก์ธ์ค์ PCB๋ฅผ semaphore์ ๋ํ wait queue์ ๋ฃ์
โ context switching ์ผ์ด๋์ ๋ค์ ํ๋ก์ธ์ค์๊ฒ CPU ์ฌ์ฉ๊ถ ๋์ด๊ฐ๊ฒ ํจ์ผ๋ก์จ CPU๊ฐ ๋์ง ์๋๋ก ๋ง๋ฆ.
Wakeup()
: remove one of processes in the waiting queue and put it in the ready queue
โ block๋ ํ๋ก์ธ์ค๋ฅผ wake up ์ํค๊ณ ์ด ํ๋ก์ธ์ค์ PCB๋ฅผ ready queue๋ก ์ฎ๊น
typedef struct{
int value;
struct process *list; /* process wait queue */
} semaphore;
P(semaphore *S){
S->value--;
if(S->value < 0){
add this process to S->list;
block();
}
}
V(semaphore *S){
S->value++;
if(S->value <= 0){
remove a process P from S->list;
wakeup(P);
}
}
๐น Java Synchronization
public class SynchronizedQueue {
public LinkedList<Integer> q = new LinkedList<Integer>();
public synchronized void enqueue (int item) {
q.add(item);
notify();
}
public synchronized int dequeue () {
try {
while (q.size() == 0) {
wait();
}
return q.removeFirst();
} catch (InterruptedException e) {
return 0;
}
}
}
๋๊ธฐํ์ ๋ฌธ์
๐น Producer-Consumer with Bounded Buffer
- ๋ฒํผ๋ ์ค์ง ํ๋์ ์ค๋ ๋๋ง ์กฐ์ํ ์ ์์ โ mutual exclusion (mutex)
- ๋ฒํผ๊ฐ ๊ฐ๋ ์ฐจ์๋ค๋ฉด producer๋ consumer๊ฐ ๋ฒํผ์์ ๊ฐ์ ธ๊ฐ ๋ ๋์ wait
- ๋ฒํผ๊ฐ ๋น์ด์๋ค๋ฉด consumer๋ producer๊ฐ ๋ฒํผ๋ฅผ ์ฑ์ธ ๋ ๋์ wait
๐น Readers-Writers Problem
- ํ ํ๋ก์ธ์ค๊ฐ DB์ write์ค์ผ ๋ ๋ค๋ฅธ ํ๋ก์ธ์ค๊ฐ ์ ๊ทผํ๋ฉด ์๋จ
- read๋ ์ฌ๋ฟ์ด ๋์์ ํด๋ ๋จ
- Solution
- writer๊ฐ DB์ ์ ๊ทผ ํ๊ฐ๋ฅผ ์์ง ์ป์ง ๋ชปํ ์ํ์์๋ ๋ชจ๋ ๋๊ธฐ ์ค์ธ reader๋ค์ ๋ค DB์ ์ ๊ทผํ๊ฒ ํด์ค
- writer๋ ๋๊ธฐ ์ค์ธ reader๊ฐ ํ๋๋ ์์ ๋ DB ์ ๊ทผ ํ์ฉ
- ์ผ๋จ writer๊ฐ DB์ ์ ๊ทผ ์ค์ด๋ฉด reader๋ค์ ์ ๊ทผ ๊ธ์ง
- writer๊ฐ DB์์ ๋น ์ ธ๋๊ฐ์ผ๋ง reader ์ ๊ทผ ํ์ฉ
- Shared Data
- readCount โ ํ์ฌ DB์ ์ ๊ทผ ์ค์ธ reader ์
- DB
- Synchronization variables
- mutex : readcount๋ฅผ ์ ๊ทผํ๋ ์ฝ๋(critical section)์ mutual exclusion ๋ณด์ฅ
- db : reader์ writer๊ฐ ๊ณต์ DB ์์ฒด๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ ๊ทผํ๊ฒ ํ๋ ์ญํ
- ์ฌ๋ฟ์ด ์ฝ์ ์ ์๋๋ก readcount ์ค์
- ๋๊ตฐ๊ฐ ์ด๋ฏธ ์ฝ๊ณ ์๋ค๋ฉด readcount++๋ readcount>1์ผ ๊ฒ์
๐ lock์ ๋ฐ๋ก ๊ฑธ ํ์๊ฐ ์๋ค. ์ด๋ฏธ ์ต์ด์ ์์ ์ฝ๊ณ ์๋ ๊ฒ์ด db์ lock์ ๊ฑธ์ด๋์๊ธฐ ๋๋ฌธ=P(db) - ๋ง์ง๋ง์ผ๋ก ์ฝ๊ณ ๋๊ฐ๋ ํ๋ก์ธ์ค์ธ ๊ฒฝ์ฐ readcount-- == 0
๐ db์ ๊ฑธ์ด๋์ lock์ ํผ๋ค = V(db) - reader๊ฐ ์ด๋ฏธ lock์ ๊ฑธ์ด๋์์ ๋ writer๋ณด๋ค reader๊ฐ ๋ฆ๊ฒ ์์ด๋ reader๋ฅผ ๋จผ์ ์ฒ๋ฆฌ
๐ Starvation ๋ฐ์ ๊ฐ๋ฅ โ ์ฐ์ ์์๋ฅผ ๋ถ์ฌํด์ ํด๊ฒฐ ๊ฐ๋ฅ
๐นDining Philosophers Problem
- Deadlock : ๋ ์ด์์ ํ๋ก์ธ์ค๊ฐ ์๋ก ์๋๋ฐฉ์ ์ํด ์ถฉ์กฑ๋ ์ ์๋ event๋ฅผ ๋ฌดํํ ๊ธฐ๋ค๋ฆฌ๋ ํ์
- Starvation : ์์ํ ์์์ ์ป์ง ๋ชปํ๊ณ ๋ฌดํํ ๊ธฐ๋ค๋ฆผ
Concurrency Problem
๐น Monitors
- mutual exclusion โ lock
- always acquire before accessing shared data structure
- always release after finishing with shared data
- conditional synchronization โ condition variables
- a queue of threads waiting for something inside a critical section
- ํน์ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋ ๋๊น์ง ์ค๋ ๋ ๊ธฐ๋ค๋ฆฌ๊ฒ ํจ
'CS > โ๏ธ ์ด์์ฒด์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[โ๏ธ OS] ํ๋ก์ธ์ค ๊ฐ ํต์ (IPC: Inter-Process Communication) (0) | 2024.03.09 |
---|---|
[โ๏ธ OS] CPU ์ค์ผ์ค๋ง (1) | 2024.03.03 |
[โ๏ธ OS] Thread (0) | 2024.02.29 |
[โ๏ธ OS] Process (1) | 2024.02.27 |