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.
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
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.
posixsem
directory create a text file named readme
(not readme.txt
, or README
, or Readme
, etc.)
that contains:
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-copyA 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 |