Question: Please use C programming. The producer/consumer problem is a classic problem in synchronization, create a program that does the following. - Create a global array

Please use C programming.

The producer/consumer problem is a classic problem in synchronization, create a program that does the following.

- Create a global array buffer of length 5, this is shared by producer and consumers and initialized to zero.

- Prompts the user for ten numbers (store in an array use #define NUMBERS 10 for the size)

- Creates two threads, one a producer, the other a consumer

- The producer thread calls the function producer which takes the array of numbers from the users as an argument and does the following:

- Loops until all ten items have been added to the buffer, each time with a random delay before proceeding

- Using semaphores gets access to the critical section (buffer)

- For each number added to buffer prints Produced , to indicate the number that has been added to the buffer

- If the buffer is full, it waits until a number has been consumed, so that another number can be added to the buffer

- The consumer thread calls the function consumer and does the following:

- Loops until ten items have been consumed from the buffer, each time with a random delay before proceeding

- Using semaphores gets access to the critical section (buffer)

- For each number consumed from the buffer, sets the buffer at that index to 0, indicating that the value has been consumed.

- For each number consumed, also prints Consumed , to indicate the number that has been consumed from the buffer

- If the buffer is empty, it waits until a number has been added, so that another number can be consumed from the buffer

- The program waits for both threads to finish using join(), and then prints the contents of buffer, the contents of buffer should be all zeros.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!