This assignment makes use of the files contained in this zip file. This assignment is due Monday, November 6.
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 buffers. In the file are directions for what parts of the program you need to implement. Notice that, although this program instantiates three producers, three consumers, and three buffers, your code should be written to work with any number of producers, consumers, and buffers.
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.
In the zip file there are two text files, sample-output-windows.txt
and sample-output-linux.txt
, which are examples of the producer-consumer program's output using 3 stack-buffers, 3 producer threads, and 3 consumer threads. The Linux version looks correct but the Windows version doesn't. There seems to be something funny about how printf() works in Windows versus Linux. If you work on Windows, you will probably have to put up with the slightly garbled output.
When you get the producer-consumer program working, you should notice that it is prone to deadlocks. 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 work with three buffers, three producer threads, and three consumer threads but your code should be written to work with any number of producers, consumers, and buffers. There are two ways you can solve the deadlock problem. One way uses the PThread function pthread_cond_timedwait() in place of pthread_cond_wait()
. The other way "backs off" of a buffer if it detects that it is full or empty (instead of blocking on the buffer's condition variable).
The zip file contains Windows and Linux binaries for the ProducersConsumers
and ProducersConsumers-no-deadlock
programs. You can run these programs to see how your programs should behave.
Turn in a zip file called CS590Hw1Surname.zip
(where Surname
is your last name) containing your explanation of the deadlock problem and your answers to the above questions and your version of ProducersConsumers.c
and ProducersConsumers-no-deadlock.c
.
This assignment is due Monday, November 6.