Roger L. Kraft

CS 59000-05 - Programming Assignment 1

This assignment makes use of the files contained in this zip file. This assignment is due Tuesday, October 14.

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.

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 the implementation of the Buffer data structure defined in StackBuffer.h, which is a stack type buffer (implemented by me). At the beginning of the ProducersConsumers.c program there is an #include directive that determines which buffer implementation to use.

In the zip file there is a text file, sample-output.txt, which is an example of the producer-consumer program's output using 3 stack-buffers, 3 producer threads, and 3 consumer threads.

When you get the producer-consumer program 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?

Finally, write a version of the producer-consumer program that avoids deadlock. Your solution should still have three buffers, three producer threads, and three consumer threads. The main tool for solving this problem is to use the PThread function pthread_cond_timedwait() in place of pthread_cond_wait().

Turn in a zip file containing your explanation of the deadlock problem and your answers to the above questions, and your versions of the ProducersConsumers.c and ProducersConsumers-no-deadlock.c files.

This assignment is due Tuesday, October 14.

Here are links to some pthreads functions that you might need to use in this assignment.