Lab Assignment--Threads and Semaphores


(Please do this lab in LINUX)

Part 1

Read and complete Activity 2 from the following location http://www.csc.villanova.edu/~mdamian/threads/posixsem.html.

Part 2

Please get the producer-consumer code from your lab instructor for a producer-consumer problem that also came from this website. You will be writing code for the consumer function and adding code in main where the comments indicate to "Insert" code.

If you do not know how to start the consumer function, you might want to think about the answers to these questions:

Note: these questions do not need to be handed in.

A sample run is the following (yours might be slightly different):

[P0] Producing 0 ...
[P0] Producing 1 ...
[P0] Producing 2 ...
[P2] Producing 0 ...
[P1] Producing 0 ...
-----[C2] Consuming 0 ...
-----[C1] Consuming 1 ...
[P0] Producing 3 ...
[P2] Producing 1 ...
-----[C2] Consuming 2 ...
[P1] Producing 1 ...
-----[C1] Consuming 0 ...
-----[C0] Consuming 0 ...
-----[C2] Consuming 3 ...
-----[C1] Consuming 1 ...
[P2] Producing 2 ...
[P2] Producing 3 ...
-----[C1] Consuming 1 ...
-----[C0] Consuming 2 ...
-----[C0] Consuming 3 ...
[P1] Producing 2 ...
[P1] Producing 3 ...
-----[C2] Consuming 2 ...
-----[C0] Consuming 3 ...

Deliverables:

Submit 3 files to URCourses:

  1. goodcnt.c with the semaphore version of a mutex
  2. producer-consumer code with the 3 semaphores
  3. A log file showing: