Question
The following code is for another producer and a consumer process. Assume that these processes are run in a multi-processing environment and answer the questions
The following code is for another producer and a consumer process. Assume that these processes are run in a multi-processing environment and answer the questions below. Remember that in a multi-processing environment, the producer can execute one or a few lines of code, followed by the consumer executing one or a few lines of code, followed by the producer again, and so on.
// semWait(x) => if (x.value == 1) x.value = 0 and can continue running else block until signaled on x
// semSignal(x) => x.value = 1. Process waiting on x can run
| int n = 0; // number of items in the buffer binary_semaphore s = 1; // mutex for buffer access binary_semaphore delay = 0; // force consumer wait if buffer empty
void producer() // One producer { while (true) { produce(); // Produce an item semWait(s); // Wait on Buffer append(); //Critical Section n++; // Critical Section semSignal(delay); // Critical Section semSignal(s); } }
void consumer() // One consumer { semWait(delay); while (true) { semWait(s); take(); // Critical Section n--; // Critical Section consume(); // Consume an item semWait(delay); } } |
|
|
void main()
{
n = 0;
parbegin (producer, consumer); // Create producer and consumer entities.
}
Question 1: Describe the inevitable outcome of running these processes in a multiprocessing environment. Be very specific in your answer. Elaborate on what has happened and why.
Question 2: In the space below, rewrite the consumer and producer code in such a way that it fixes the problem you described above. Be sure to highlight your changes in yellow like this.
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