我的代码在这里: http: //pastebin.com/Fi3h0E0P
这是输出
0
Should we take order today (y or n): y
Enter order number: 100
More customers (y or n): n
Stop serving customers right now. Passing orders to cooker:
There are total of 1 order(s)
1
Roger, waiter. I am processing order #100
目标是服务员必须接受订单,然后将其交给厨师。服务员必须等待厨师完成所有比萨饼,交付比萨饼,然后接受新订单。
我在上一篇文章中询问了 PV 是如何工作的。
我觉得跟\n
吃没关系?我尝试了各种组合wait()
,但没有奏效。
我在哪里做错了?
主要部分在这里:
//Producer process
if(pid > 0)
{
while(1)
{
printf("0");
P(emptyShelf); // waiter as P finds no items on shelf;
P(mutex); // has permission to use the shelf
waiter_as_producer();
V(mutex); // cooker now can use the shelf
V(orderOnShelf); // cooker now can pickup orders
wait();
printf("2");
P(pizzaOnShelf);
P(mutex);
waiter_as_consumer();
V(mutex);
V(emptyShelf);
printf("3 ");
}
}
if(pid == 0)
{
while(1)
{
printf("1");
P(orderOnShelf); // make sure there is an order on shelf
P(mutex); //permission to work
cooker_as_consumer(); // take order and put pizza on shelf
printf("return from cooker");
V(mutex); //release permission
printf("just released perm");
V(pizzaOnShelf); // pizza is now on shelf
printf("after");
wait();
printf("4");
}
}
所以我想这是执行路径:进入waiter_as_producer,然后转到子进程(cooker),然后将控制权移交给父进程,完成waiter_as_consumer,切换回子进程。这两个等待切换回父级(就像我说的我尝试了所有可能的 wait() 组合......)。