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
- 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
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started