生产者消费者问题
设信号量mutex1, mutex2, full1, full2, empty1, empty2分别表示1和2号缓冲区的访问互斥, 是否满, 是否空
semaphore mutex1 = 1, empty1 = n, full1 = 0;
semaphore mutex2 = 1, empty2 = m, full2 = 0;
void A()
{
while(1)
{
produce next product1;
p(empty1);
p(mutex1);
buffer1(i) = product1;
i = (i + 1) % n;
v(mutex1);
v(full1);
}
}
void B()
{
while(1)
{
p(full1);
p(mutex1);
goods = buffer1(j);
j = (j + 1) % n;
v(mutex1);
v(empty1);
Consume goods and Produce next product2;
p(empty2);
p(mutex2);
buffer2(a) = product2;
a = (a + 1) % m;
v(mutex2);
v(full2);
}
}
void C()
{
while(1)
{
p(full2);
p(mutex2);
goods = buffer2(b);
b = (b + 1) % m;
v(mutex2);
v(empty2);
Consume product;
}
}
int main()
{
parbegin(A(), B(), C());
}