Solution to the Producer-Consumer problem using Monitors

Monitors make solving the producer-consumer a little easier. Mutual exclusion is achieved by placing the critical section of a program inside a monitor. In the code below, the critical sections of the producer and consumer are inside the monitor ProducerConsumer. Once inside the monitor, a process is blocked by the Wait and Signal primitives if it cannot continue.

  monitor ProducerConsumer
    condition full, empty;
    int count;
  
  procedure enter();
  {
    if (count == N) wait(full);        // if buffer is full, block
    put_item(widget);                  // put item in buffer
    count = count + 1;                 // increment count of full slots
    if (count == 1)  signal(empty);    // if buffer was empty, wake consumer
  }

  procedure remove();
  {
    if (count == 0)  wait(empty);      // if buffer is empty, block
    remove_item(widget);               // remove item from buffer
    count = count - 1;                 // decrement count of full slots
    if (count == N-1)  signal(full);   // if buffer was full, wake producer
  }

  count = 0;
  end monitor;

  Producer();
  {
    while (TRUE)
   {
      make_item(widget);               // make a new item
      ProducerConsumer.enter;          // call enter function in monitor
   }
  }

  Consumer();
  {
    while (TRUE)
    {
      ProducerConsumer.remove;         // call remove function in monitor
      consume_item;                    // consume an item
   }
  }
   
    

               
IPC Home    Subway    Problems  Producer/  Message
  Page       Map                Consumer   Passing