①13年45题
②20年45题
③17年46题
④11年45题

1.生产者与消费者
semaphore empty = 5; //空缓冲区数,初始有5个空缓冲区
semaphore full = 0; //产品数,初始缓冲区中没有产品
semaphore mutex = 1; //互斥锁,生产者与生产者之间、生产者与消费者之间要互斥地访问缓冲区
CoBegin
{
Producer() //生产者进程
{
while(1)
{
生产一个产品;
P(empty);
P(mutex);
生产者将产品放入缓冲区;
V(mutex);
V(full); //放入产品后,缓冲区内多了一个产品
}
}
Consumer() //消费者进程
{
while(1)
{
P(full);
P(mutex);
从缓冲区取走一个产品;
V(mutex);
V(empty); //取走产品后,多了一个空缓冲区
消费产品;
}
}
}
CoEnd

2.多类生产者-多类消费者
取水果
semaphore mutex = 1;
semaphore empty = 1;
semaphore full = 0;
semaphore apple = 0;
semaphore orange = 0;
CoBegin
{
Dad() Mom() Son() Daughter()
{ { { {
while(1) while(1) while(1) while(1)
{ { { {
削苹果; 剥橘子; P(full); P(full);
P(empty); P(empty); P(apple); P(orange);
P(mutex); P(mutex); P(mutex); P(mutex);
向盘中放入一个苹果; 向盘中放入一个橘子; 从盘中取一个苹果; 从盘中取一个橘子;
V(mutex); V(mutex); V(mutex); V(mutex);
V(full); V(full); V(empty); V(empty);
V(apple); V(orange); 吃苹果; 吃橘子;
} } } }
} } } }
}
CoEnd
例题:
14年47.
19年43.