The Producers-Consumers Problem

This is an individual assignment. In this assignment you will implement a solution to the producers-consumers problem discussed in class.

The problem describes two sets of processes, the producers and the consumers, who share a common, fixed-size buffer used as a queue. The producer's job is to generate a piece of data, put it into the buffer and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that a producer won't try to add data into a full buffer, and a consumer won't try to remove data from an empty buffer. Our solution to this problem will make use of POSIX semaphores.

What To Do

  1. Download this incomplete producer-consumer code into a file called prodcon.c in your posixsem directory.

  2. Compile the code and run it. You will notice that there is no ouput and the process seems blocked. Kill the process with CTRL-C or using the kill command.

    Remember to link the POSIX pthread and real-time libraries when compiling a program that uses pthreads and posix semaphores:

         gcc -o xfilename filename.c -lpthread -lrt 

  3. Add code to initialize the semaphore mutex in the main function, then repeat the step above. You will see output from the producers.

  4. Complete the code for the Consumer thread, then add code to create Consumer threads in the main function. The result should be a solution to the producer-consumer problem.

    Comment well your code. Compile and run your program and observe the output. Make sure to label each line in the output by the identifier for each producer and consumer (P0, P1, P2, ..., C0, C1, C2, ...). The output of your program should be similar to the following:

    [P0] Producing 0 ...
    [P1] Producing 0 ...
    [P2] Producing 0 ...
    [P2] Producing 1 ...
    ------> [C2] consumed 0
    ------> [C2] consumed 1
    [P1] Producing 1 ...
    ------> [C1] consumed 0
    ------> [C1] consumed 1
    [P0] Producing 1 ...
    ------> [C1] consumed 1
    ------> [C0] consumed 0
    [P2] Producing 2 ...
    [P2] Producing 3 ...
    [P1] Producing 2 ...
    [P1] Producing 3 ...
    ------> [C0] consumed 2
    ------> [C0] consumed 3
    ------> [C0] consumed 2
    ------> [C1] consumed 3
    [P0] Producing 2 ...
    [P0] Producing 3 ...
    ------> [C2] consumed 2
    ------> [C2] consumed 3
    Remember that you cannot control the order in which the producer and consumer threads are scheduled to run, so your output will likely be different. The only restriction is that items are consumed in the same order in which they are produced.

What to Submit

In your posixsem directory create a text file named readme (not readme.txt, or README, or Readme, etc.) that contains:
  1. Your name and assignment number.
  2. The machine you used to complete the assignment (i.e, tanner or your own machine). If you used your own machine, please be ready to demo your code to your TA or your instructor at any time upon request.
  3. An indication of how much time you spent doing the assignment outside of class.

Turn in a printout copy of the readme and prodcon.c files, and a sample output.

Leave the source code in your directory systems/posixsem. Do not make any changes to these files after the due date for this assignment. If you wish to continue working on this assignment after the due date, make a copy of your directory posixsem using the following Unix command:

    cp -r ~/systems/posixsem ~/systems/posixsem-copy
A new directory called posixsem-copy will be created in your systems directory. You may now make any changes you want to the files in your posixsem-copy directory, at any time.


No credit will be given for code that does not compile.
100     Total points possible
20 Program runs without crashing
70 Program works as expected
  5 Submission includes readme file
  5 Submission includes sample output

Have fun!