生产者与消费者问题

问题描述

image.png

问题分析

同步关系:

代码实现

sem mutex = 1;
sem empty = 0;
sem full = 5;

void producer() {
	while (1) {
		P(full);
		P(mutex);
		...
		V(mutex);
		V(empty);
	}
}

void consumer() {
	while (1) {
		P(empty);
		P(mutex);
		...
		V(mutex);
		V(full);
	}
}