이화여자대학교 반효경 교수님의 운영체제 강의 14강을 듣고 정리한 내용이다.
📌 프로세스 동기화(Process synchronization)는 다른 말로 병행제어(Concurrency control)이라고도 한다.
✏️ Dining Philosophers Example
monitor dining_philosopher {
enum {thinking, hungry, eating} state[5];
condition self[5];
void pickup(int i) {
state[i] = hungry;
test(i);
if(state[i] != eating)
self[i].wait(); /* wait here */
}
}
void putdown(int i) {
state[i] = thinking;
/* test left and right neighbors */
test((i+4) % 5); /* if L is waiting */
test((i+1) % 5);
}
void test(int i) {
if((state[(i+4) % 5] != eating) && (state[i] == hungry) && (state[(i+1) % 5] != eating)) {
state[i] = eating;
self[i].signal(); /* wake up Pi */
}
}
void init() {
for (int i= 0;i<5;i++) {
state[i] = thinking;
}
}
Each Philosopher:
{
pickup(i);
eat();
putdown();
think();
}while(1);
- 왼쪽과 오른쪽 젓가락이 모두 사용 가능할 때 실행가능
- 세마포어에서는 lock을 걸지만, 모니터에서는 락을 걸지 않음
'운영체제' 카테고리의 다른 글
15. Deadlocks 2 (0) | 2022.03.12 |
---|---|
14. Deadlocks 1 (0) | 2022.03.05 |
12. Process Synchronization 3 (0) | 2022.02.24 |
11. Process Synchronization 2 (0) | 2022.02.24 |
10. CPU Scheduling 2 & Process Synchronization 1 (0) | 2022.02.19 |