Question: Problem #2: The following code is for another producer and a consumer process. Assume that these processes are run in a multi-processing environment and answer
Problem #2: 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. }
To trace through the possibilities, you may find it helpful to fill in the table below to keep track of the values in s, n, and delay. You may add as many rows as needed to the table. This table will not be graded, however. Only your answers to the questions below will be graded.
Producer Consumer s n Delay 1 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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
Get step-by-step solutions from verified subject matter experts
