Question: Problem #1: The following code is for a producer and a consumer process. Assume that these processes are run in a multi-processing environment and answer

Problem #1:

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. // 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(); // Critial Section n++; // Critical Section if (n==1) semSignal(delay); // Critical Section semSignal(s); } } void consumer() // One consumer { semWait(delay); while (true) { semWait(s); take(); // Critical Section n--; // Critical Section semSignal(s); consume(); // Consume an item if (n==0) semWait(delay); } } void main() { n = 0; parbegin (producer, consumer); // Create producer and consumer entities. }

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.

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!