Define an integer buffer with the size of 32 and implement a circular queue data structure that is used by producers and a consumer.

PRODUCER CONSUMER PROBLEM

Define an integer buffer with the size of 32 and implement a circular queue data structure that is used by producers and a consumer. Next, you will need to initialize the mutex lock and semaphores. In this assignment, you will use two semaphores where one indicates a full queue and the other indicates an empty queue. The producer function is used to insert a random number between 0 and 10 to the queue. The consumer function is used to remove a number from the queue in a FIFO fashion. In addition, inside the loops of the producer and consumer functions, add a random delay between 0 and 2 seconds to simulate various lengths of processes. In addition, you are required to use 2 producers and 1 consumer for this assignment where 0 and 2 are producers and 1 is consumer (3 threads). Finally, your code should run forever.

Assignment 2 (50 points) Due Date: 8/5/2018 (11:59pm) In this assignment, you are going to write a C program to utilize multi-threads, mutex lock, and semaphores for the producer and consumer problem. You will need to utilize your assignment 1 for your assignment 2. Define an integer buffer with the size of 32 and implement a circular queue data structure that is used by producers and a consumer. Next, you will need to initialize the mutex lock and semaphores. In this assignment, you will use two semaphores where one indicates a full queue and the other indicates an empty queue. The producer function is used to insert a random number between 0 and 10 to the queue. The consumer function is used to remove a number from the queue in a FIFO fashion. In addition, inside the loops of the producer and consumer functions, add a random delay between 0 and 2 seconds to simulate various lengths of processes. In addition, you are required to use 2 producers and 1 consumer for this assignment where 0 and 2 are producers and 1 is consumer (3 threads). Finally, your code should run forever. The expected results should look similar to the following Thread 2 – Produce 8 (1) Thread 2 – Produce 8 (2) Thread 2 – Produce 3 (3) Thread 2 – Produce 5 (4) Thread 1 – Consume 8 (3) Thread 0 – Produce 3 (4) Thread 1 – Consume 8 (3) Thread 0 – Produce 8 (4) Thread 0 – Produce 4 (5) Thread 2 – Produce 8 (6) Thread 1 – Consume 3 (5) Thread 0 – Produce 4 (6) Thread 2 – Produce 4 (7) Thread 0 – Produce 3 (8) Thread 0 – Produce 2 (9) Thread 0 – Produce 2 (10) Thread 0 – Produce 8 (11) Thread 1 – Consume 5 (10) Thread 0 – Produce 1 (11) Thread 2 – Produce 5 (12) Thread 0 – Produce 2 (13) Thread 0 – Produce 2 (14) Thread 0 – Produce 9 (15) Thread 0 – Produce 9 (16) Thread 1 – Consume 3 (15) Thread 2 – Produce 7 (16) Thread 0 – Produce 1 (17) Thread 1 – Consume 8 (16)

Thread 0 – Produce 5 (17) Thread 2 – Produce 6 (18) Thread 1 – Consume 4 (17) Thread 0 – Produce 2 (18) Thread 2 – Produce 0 (19) Thread 2 – Produce 0 (20) Thread 2 – Produce 2 (21) Thread 2 – Produce 6 (22) Thread 2 – Produce 9 (23) Thread 2 – Produce 0 (24) Thread 2 – Produce 3 (25) Thread 0 – Produce 7 (26) Thread 1 – Consume 8 (25) Thread 2 – Produce 8 (26) Thread 0 – Produce 9 (27) Thread 2 – Produce 6 (28) Thread 0 – Produce 8 (29) Thread 1 – Consume 4 (28) Thread 2 – Produce 2 (29) Thread 0 – Produce 1 (30) Thread 1 – Consume 4 (29) Thread 0 – Produce 2 (30) Thread 0 – Produce 2 (31) Thread 2 – Produce 4 (32) Thread 1 – Consume 3 (31) Thread 0 – Produce 3 (32) BUFFER is FULL … Thread 1 – Consume 2 (31) Thread 0 – Produce 4 (32) Thread 1 – Consume 2 (31) Thread 0 – Produce 0 (32) Thread 1 – Consume 8 (31) Thread 2 – Produce 0 (32) BUFFER is FULL … BUFFER is FULL … BUFFER is FULL … BUFFER is FULL … BUFFER is FULL … Thread 1 – Consume 1 (31) Thread 0 – Produce 2 (32) BUFFER is FULL … Thread 1 – Consume 5 (31) Thread 2 – Produce 0 (32) BUFFER is FULL … BUFFER is FULL … BUFFER is FULL … BUFFER is FULL … BUFFER is FULL … where the number is the thread identifier, the second number is the random number inserted to or removed from the queue, and the third number is the queue index. Also, print out a message when the buffer is full or empty.

All programs must be run under the department’s Unix server. To compile your code in Unix, you need to include pthread as follows >> gcc Assign2.c –pthread Submission Submit your code to blackboard.