Lab 3: Producer/Consumer

In this lab, you will be implementing the producer/consumer problem. This problem involes a bounded buffer which is modified concurrently by multiple processes. A producer adds items to the buffer and a consumer removes items from it. The buffer is bounded, therefore a producer can only add items when the buffer isn't full and a consumer can only remove items when the buffer isn't empty. In this lab, there will be exactly 1 producer and 1 consumer.

Download the source, buffer.cpp, to get you started. You will then need to:
  1. Modify the produce task to add items to the buffer. The producer needs to pick a random value, allocate memory which will store the random value, and then insert the pointer to the allocated memory into the buffer. The taskDelay function that is provided causes your added code to occur at random times, thus creating a more realistic simulation.
  2. Modify consumer task to remove items from the buffer. Make sure the consumer deletes the memory that was allocated by the producer. The provided code will also cause this to occur at random times.
  3. Add printf statements to the producer and consumer function that state what values were produced or consumed.
  4. Use counting semaphores to implement concurrency control and mutual exclusion (don't allow producer to insert into full buffer or consumer to remove from empty buffer). You will need the functions semCCreate, semGive, and semTake. See help->manual index for information on how to use these functions. For more information on semaphores and the producer/consumer problem, see your text book.
  5. Use a binary semaphore to provide mutual exclusion for the printf statements.
  6. Depending on your implementation, you may need to add to or change the given code in order to get the program to terminate when you call progStop from the shell.
  7. Explain in your lab report how your solution guarantees there won't ever be any conflicts between the producer and the consumer. Also, discuss the effect on performance and functionality if busy waiting (i.e. while(flag)) was used instead of semaphores.