Bounded-Buffer Problem

buffer의 사이즈가 n일 때, 각각의 버퍼는 하나의 아이템을 가질 수 있다.

변수 초기화

	int n; //pool의 수
    semaphore mutex = 1; //상호배제를 위한 Binary Semaphore
    semaphore empty = n; // n -> 0
    semaphore full = 0; // 0 -> n

Binary Semaphore : 상호 배제를 위해 이용. 값을 1로 초기화

Counting Semaphore : empty buffer는 n으로 초기화, full buffer는 0으로 초기화

Producer process

while (true) {
...
/* produce an item in next produced */
...
wait(empty); //empty-1
wait(mutex);
...
/* add next produced to the buffer */
while(count == bufferSize);
buffer[in] = next_produced; //채워넣는다.
...
signal(mutex);
signal(full); //full+1
}

→ buffer가 한 칸 비어있고, 한 producer만 buffer에 진입한 것이 보장된다.