私のコードはここにあります: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を終了し、子に切り替えます。2つの待機は親に戻ります(私がすべての可能なwait()の組み合わせを試したと言ったように...)。