CS 302 / ECE 468 - Programming Assignment 5

This assignment makes use of the files contained in this zip file.

You are to write a program that simulates the producer consumer synchronization pattern. Your program will create three (bounded) buffers, three producer threads, and three consumer threads. Each producer thread will be in an endless loop where it randomly chooses one of the three buffers, puts an "item" into the buffer, and then displays the contents of the buffer in the console window. Each consumer thread will be in an endless loop where it randomly chooses one of the three buffers, takes an "item" from the buffer, and then displays the contents of the buffer in the console window.

Be sure that you have read Section 6.1 (pages 225-227) and Section 6.6.1 (page 240) from our operating systems textbook and Section 4.1 (pages 61-71) from "The Little Book of Semaphores".

In the zip file there is a file ProducersConsumers.c which outlines a producer-consumer program that creates the producer and consumer threads and the three buffers. In the file are directions for what parts of the program you need to implement.

The ProducersConsumers.c file does not define what a "Buffer" is. Your finished program should work with two different implementations of the Buffer data structure, a stack type buffer (implemented by me) and a circular type buffer (which you will implement). At the beginning of the ProducersConsumers.c program there is an #include directive that chooses which buffer implementation to use. In the zip file there are two header files, StackBuffer.h and CircularBuffer.h, which define an array-based stack buffer (a Last-In-First-Out buffer) and an array-based circular queue buffer (a circular, First-In-First-Out buffer) respectively. In the CircularBuffer.h file are directions for what you need to implement in the definition of the circular buffer.

In the zip file there are two text files, stack-output.txt and circular-output.txt, which are examples of the producer-consumer program's outputs using 3 buffers, 3 producer threads, and 3 consumer threads. You switch the program ProducersConsumers.c to use circular buffers instead of stack buffers by changing only which header file is included into ProducersConsumers.c. Everything else in ProducersConsumers.c is independent of which kind of buffer is being used (this is because StackBuffer.h and CircularBuffer.h are two different implementations of the same "abstract data type" and they define the same "public interface").

When you work on this, first get the ProducersConsumers.c program working with the StackBuffer.h version of the buffer before you try to implement the circular buffer.

When you get this version of the producer-consumer problem working, you should notice that it is prone to deadlocks (when you run the program, it sometimes seems to stop several seconds before the message "Threads stopped." gets printed in the console window; that is the deadlock). Write a brief explanation of what is causing the deadlock. Also, if there is only a single producer and a single consumer but multiple buffers, can there be a deadlock? What if there are multiple producers and multiple consumers but only one buffer? Is there an easy way to avoid the deadlock situation when there are multiple producers, consumers, and buffers?

Turn in a zip file containing your explanation of the deadlock problem and your answers to the above questions, your versions of the ProducersConsumers.c and CircularBuffer.h files, and also the StackBuffer.h file.

This assignment is due Wednesday, April 8.

Here are links to some Win32 API functions that you will need to use in this assignment.

Here are a few more MSDN references. These are not directly related to this assignment, but you might find them informative.


Return to the main homework page.
Return to the CS 302 home page.


compliments and criticisms