The producer-consumer problem is a well-known problem in concurrent programming. In this assignment, you will implement an extension of this problem using the POSIX Threads library and you would need to synchronize executions of the threads to have the program working properly.
We have two kinds of threads: producers and consumers. In this assignment there can be many producers and consumers. Each producer and each consumer must execute in a different thread. A producers' job is to produce items and put them into a bounded buffer. A consumers job is to remove items from the buffer. The buffer can hold 8 items. There are the following constraints on how producers and consumers can access the buffer.
1. Only one thread can access the buffer at a time.
2. A producer cannot write into the buffer if it is full.
3. A consumer cannot read from the buffer if it is empty.
Producer Behavior
Consumer Behavior
Delays
In order to demonstrate the behavior of the system under different conditions, you will add delays to the producer and consumer threads using the usleep() function. There will be two options for the insertion of delays. In the first option, a 0.5 second delay will be added to each producer thread immediately after it adds an item to the buffer. This will greatly slow down the rate of production. In the second option, a 0.5 second delay will be added to each consumer thread immediately after it removes an item from the buffer. The choice between these two delay options will be made by a command-line argument passed to your program.
Command-Line Arguments
Your program will accept 4 command-line arguments which specify parameters of the producer-consumer system.
Using pthread functions without specifying priorities, the order of lines in output may vary every time you run the code even with the same values. Here are some criteria for grading:
Running code as ./a.out p c i d your output should consist of producer_0 to p-1 each producing items for i times and consumer_0 to c-1 each consuming for p*i/c. Running your code with d = 0 and d = 1 you should see different behavior in your results (why?).
Here are some sample correct outputs:
$ ./a.out 2 4 10 0
producer_0 produced item 0
producer_0 produced item 1
producer_0 produced item 2
producer_0 produced item 3
producer_0 produced item 4
producer_0 produced item 5
producer_0 produced item 6
consumer_3 consumed item 0
producer_0 produced item 7
consumer_0 consumed item 1
consumer_2 consumed item 2
producer_0 produced item 8
producer_0 produced item 9
consumer_1 consumed item 3
producer_1 produced item 10
consumer_3 consumed item 4
consumer_2 consumed item 5
producer_1 produced item 11
producer_1 produced item 12
consumer_0 consumed item 6
consumer_1 consumed item 7
producer_1 produced item 13
producer_1 produced item 14
consumer_3 consumed item 8
consumer_2 consumed item 9
producer_1 produced item 15
producer_1 produced item 16
consumer_1 consumed item 10
consumer_0 consumed item 11
producer_1 produced item 17
producer_1 produced item 18
consumer_3 consumed item 12
consumer_2 consumed item 13
consumer_1 consumed item 14
producer_1 produced item 19
consumer_0 consumed item 15
consumer_3 consumed item 16
consumer_1 consumed item 17
consumer_2 consumed item 18
consumer_0 consumed item 19
$ ./a.out 2 4 10 1
producer_0 produced item 0
consumer_3 consumed item 0
producer_1 produced item 10
consumer_3 consumed item 10
producer_0 produced item 1
consumer_3 consumed item 1
producer_1 produced item 11
consumer_1 consumed item 11
producer_0 produced item 2
consumer_1 consumed item 2
producer_1 produced item 12
consumer_1 consumed item 12
producer_0 produced item 3
consumer_1 consumed item 3
producer_1 produced item 13
consumer_1 consumed item 13
producer_0 produced item 4
consumer_2 consumed item 4
producer_1 produced item 14
consumer_2 consumed item 14
producer_0 produced item 5
consumer_2 consumed item 5
producer_1 produced item 15
consumer_2 consumed item 15
producer_0 produced item 6
consumer_2 consumed item 6
producer_1 produced item 16
consumer_3 consumed item 16
producer_0 produced item 7
consumer_3 consumed item 7
producer_1 produced item 17
consumer_0 consumed item 17
producer_0 produced item 8
consumer_0 consumed item 8
producer_1 produced item 18
consumer_0 consumed item 18
producer_0 produced item 9
consumer_0 consumed item 9
producer_1 produced item 19
consumer_0 consumed item 19