Question
The following code is for a 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 a 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.
Question 1: Describe the situation that results from running the processes above in a multiprocessing environment. Be very specific in your answer. Elaborate on what has happened in the above scenario and why.
Question 2: In the space below, rewrite the producer and consumer code in such a way that it fixes the problem you described above. Be sure to highlight your changes in yellow like this.
// 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(); semWait(s); append(); n+t; if(n==1) semSignal(delay); semSignal(s); // Produce an item // Wait on Buffer // Critial Section // Critical Section // Critical Section void consumer() // One consumer sem Wait(delay); while (true) { sem Wait(s); take(); // Critical Section // Critical Section semSignal(s); consume(); // Consume an item if(n==0) sem Wait(delay); void main() n = 0; parbegin (producer, consumer); // Create producer and consumer entities. The table below traces through a possible outcome of running the processes above. Producer Consumer sn delay 1 2 3 4 5 6 produce() sem Waits) append() n++ if(n==1) semSignal(delay) semSignal(s) semWait(delay) semwait(s) take() semSignal(s) consume() 12 13 14 15 16 17 18 19 20 produce() sem Waits) semaiti append() n++ if(n==1) semSignal(delay) semSignals) 21 23 24 if(n==0) semWait(delay) semWait(s) take() n-- semSignals) consume() if(n==0) semWait(delay) sem Waits) take) 26 27 28 0 0 0 0 0 -1 0 // 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(); semWait(s); append(); n+t; if(n==1) semSignal(delay); semSignal(s); // Produce an item // Wait on Buffer // Critial Section // Critical Section // Critical Section void consumer() // One consumer sem Wait(delay); while (true) { sem Wait(s); take(); // Critical Section // Critical Section semSignal(s); consume(); // Consume an item if(n==0) sem Wait(delay); void main() n = 0; parbegin (producer, consumer); // Create producer and consumer entities. The table below traces through a possible outcome of running the processes above. Producer Consumer sn delay 1 2 3 4 5 6 produce() sem Waits) append() n++ if(n==1) semSignal(delay) semSignal(s) semWait(delay) semwait(s) take() semSignal(s) consume() 12 13 14 15 16 17 18 19 20 produce() sem Waits) semaiti append() n++ if(n==1) semSignal(delay) semSignals) 21 23 24 if(n==0) semWait(delay) semWait(s) take() n-- semSignals) consume() if(n==0) semWait(delay) sem Waits) take) 26 27 28 0 0 0 0 0 -1 0Step 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