CS스터디/운영체제
Process Synchronization - Monitor
Junpyo Lee
2024. 4. 2. 13:46
반응형
세마포어의 문제점
- 코딩하기 힘들다.
- 정확성(correctness)의 입증이 어렵다.
- 자발적 협력(voluntary cooperation)이 필요하다.
- 한 번의 실수가 모든 시스템에 치명적인 영향을 줄 수 있다.
실수 예시
Mutual Exclusion
이 깨짐V(mutex); Critical Section P(mutex);
Deadlock
발생P(mutex); Critical Section P(mutex);
Monitor
동시 수행중인 프로세스 사이에서 abstract data type
의 안전한 공유를 보장하기 위한 high-level synchronization construct
이다. Monitor
에서 자체적으로 프로세스의 접근을 관리하기 때문에 세마포어와 달리 개발자가 직접 Lock을 걸 필요가 없다는 것이 큰 장점이다.
특징
- 모니터 내에서는 한번에 하나의 프로세스만이 활동 가능
- 프로그래머가 동기화 제약 조건을 명시적으로 코딩할 필요 없음
- 프로세스가 모니터 안에서 기다릴 수 있도록 하기 위해
condition variable
사용condition x, y;
- lock이나 접근 가능한 공유 자원의 수 등 값을 가지는 것이 아니라 sleep한 프로세스를 관리하는 변수
Condition Variable
condition variable
은 wait와 signal 연산에 의해서만 접근 가능
x.wait()
x.wait()
을 invoke한 프로세스는 다른 프로세스가x.signal()
을 invoke하기 전까지suspend
된다.x.signal()
x.signal()
은 정확하게 하나의suspend
된 프로세스를resume
한다.suspend
된 프로세스가 없으면 아무일도 일어나지 않는다.
유한 버퍼 문제의 Monitor 해결법
monitor bounded_buffer
{
int buffer[N];
condition full, empty;
void produce(int x) {
if there is no empty buffer
empty.wait();
add x to an empty buffer
full.signal();
}
void consume(int *x) {
if there is no full buffer
full.wait();
remove an item from buffer and store it to *x
empty.signal();
}
}
출처
반효경, "운영체제 2014-1", KOCW, http://www.kocw.net/home/cview.do?cid=3646706b4347ef09
반응형