CS 302 - Programming Assignment 6

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

You are to write a program that creates three 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.

The following file 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.
ProducersConsumers.c
The above 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. The following header file defines an array-based stack buffer (a Last-In-First-Out buffer).
StackBuffer.h
The following file outlines the definition of an array-based circular queue buffer (a circular, First-In-First-Out buffer). In the file are directions for what you need to implement in the definition of the circular buffer.
CircularBuffer.h

Here is an example of the producer-consumer program's output using 3 stack buffers and 3 producer and 3 consumer threads. And here is an example of the program's output using 3 circular buffers and 3 producer 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").

When you work on this, first get the ProducersConsumers.c program working with the StackBuffer.h version of the buffer before you try to do 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 up a brief explanation of what is causing the deadlock. Is there an easy way to avoid this particular deadlock situation?

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

This assignment is due Monday, April 4.

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