Here are the codes for producer and consumer problems Producer: while (true) {/*
ID: 3862449 • Letter: H
Question
Here are the codes for producer and consumer problems Producer: while (true) {/*produce an item in next produced*/while (counter - BUFFER SIZE);/*do nothing*/buffer[in] = next_produced; in = (in + 1)% BUFFER SIZE; counter++;} Consumer: while (true) {while (counter == 0);/*do nothing*/next consumed - buffer[out]; out - (out + 1)% BUFFER_SIZE; counter;/*consume the item in next consumed*/} counter++ could be implemented by Producer as register1 = counter register1 = register 1 + 1 counter = register1 counter-- could be implemented by Consumer as register2 = counter register2 = register2 - 1 counter = register2 If we set the current value of "counter "as 7, when Producer and Consumer concurrently execute one time, what are the possible values of "counter", please give an example for each values.Explanation / Answer
Let's say the compiler compiled the statements of counter++ and counter-- into the following:
P1 register1 = counter; C1 register2 = counter;
P2 register1 = register1 + 1; C2 register2 = register2 - 1;
P3 counter = register1; C3 counter = register2;
If we set the current value of "counter" as 7, as there is no locking or synchronization mechanism not implemeted,
Depending on how above statements are mix by alternating between them, the result of counter may be 6, 7, or 8.
C1 C2 P1 P2 C3 P3 results in counter being 8.
C1 statement executed-> counter is 7
C2 statement executed-> counter is 7
P1 statement executed-> counter is 7
P2 statement executed-> counter is 7
C3 statement executed-> counter is 6
P3 statement executed-> counter is 8
So at the end of the execution counter is 8
C1 C2 P1 P2 P3 C3 results in counter being 6.
C1 statement executed-> counter is 7
C2 statement executed-> counter is 7
P1 statement executed-> counter is 7
P2 statement executed-> counter is 7
P3 statement executed-> counter is 8
C3 statement executed-> counter is 6
So at the end of the execution counter is 6
P1 P2 C1 C2 C3 P3 results in counter being 8.
P1 statement executed-> counter is 7
P2 statement executed-> counter is 7
C1 statement executed-> counter is 7
C2 statement executed-> counter is 7
C3 statement executed-> counter is 6
P3 statement executed-> counter is 8
So at the end of the execution counter is 8
P1 P2 C1 C2 P3 C3 results in counter being 6.
P1 statement executed-> counter is 7
P2 statement executed-> counter is 7
C1 statement executed-> counter is 7
C2 statement executed-> counter is 7
P3 statement executed-> counter is 8
C3 statement executed-> counter is 6
So at the end of the execution counter is 6
C1 C2 C3 P1 P2 P3 results in counter being 7.
C1 statement executed-> counter is 7
C2 statement executed-> counter is 7
C3 statement executed-> counter is 6
P1 statement executed-> counter is 6
P2 statement executed-> counter is 6
P3 statement executed-> counter is 7
So at the end of the execution counter is 7
P1 P2 P3 C1 C2 C3 results in counter being 7.
P1 statement executed-> counter is 7
P2 statement executed-> counter is 7
P3 statement executed-> counter is 8
C1 statement executed-> counter is 8
C2 statement executed-> counter is 8
C3 statement executed-> counter is 7
So at the end of the execution counter is 7.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.