Why is my producer-consumer blocking?

Posted by User007 on Stack Overflow See other posts from Stack Overflow or by User007
Published on 2012-11-14T20:51:47Z Indexed on 2012/11/14 23:00 UTC
Read the original article Hit count: 240

My code is here: http://pastebin.com/Fi3h0E0P

Here is the output

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

The goal is waiter must take orders and then give them to the cook. The waiter has to wait cook finishes all pizza, deliver the pizza, and then take new orders.

I asked how P-V work in my previous post here.

I don't think it has anything to do with \n consuming? I tried all kinds of combination of wait(), but none work.

Where did I make a mistake?

The main part is here:

//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");

    }
  }

So I imagine this is the execution path: enter waiter_as_producer, then go to child process (cooker), then transfer the control back to parent, finish waiter_as_consumer, switch back to child. The two waits switch back to parent (like I said I tried all possible wait() combination...).

© Stack Overflow or respective owner

Related posts about c

    Related posts about algorithm