In: Computer Science
5.25. The following pseudocode (next page) is a correct implementation of the producer/consumer problem with a bounded buffer:
Labels p1, p2, p3 and c1, c2, c3 refer to the lines of code shown above (p2 and c2 each cover three lines of code). Semaphores empty and full are linear semaphores that can take unbounded negative and positive values. There are multiple producer processes, referred to as Pa, Pb, Pc, etc., and multiple consumer processes, referred to as Ca, Cb, Cc, etc. Each semaphore maintains a FIFO (first-in-first-out) queue of blocked processes. In the scheduling chart below, each line represents the state of the buffer and semaphores after the scheduled execution has occurred. To simplify, we assume that scheduling is such that processes are never interrupted while executing a given portion of code p1, or p2, . . . , or c3. Your task is to complete the following chart.
item[3] buffer; // initially empty semaphore empty; // initialized to +3 semaphore full; // initialized to 0 binary_semaphore mutex; // initialized to 1 |
|
void producer() |
void consumer() |
{ |
{ |
... |
... |
while (true) { |
while (true) { |
item = produce(); |
c1: wait(full); |
p1: wait(empty); |
/ wait(mutex); |
/ wait(mutex); |
c2: item = take(); |
p2: append(item); |
\ signal(mutex); |
\ signal(mutex); |
c3: signal(empty); |
p3: signal(full); |
consume(item); |
} |
} |
} |
} |
Scheduled Step of Execution |
full’s State and Queue |
Buffer |
empty’s State and Queue |
Initialization |
full=0full=0 |
OOO |
empty=+3empty= +3 |
Ca executes c1 |
full=−1full= −1 (Ca) |
OOO |
empty=+3empty= +3 |
Cb executes c1 |
full=−2full= −2 (Ca, Cb) |
OOO |
empty=+3empty= +3 |
Pa executes p1 |
full=−2full= −2 (Ca, Cb) |
OOO |
empty=+2empty= +2 |
Pa executes p2 |
full=−2full= −2 (Ca, Cb) |
X OO |
empty=+2empty= +2 |
Pa executes p3 |
full=−1full= −1 (Cb) Ca |
X OO |
empty=+2empty= +2 |
Ca executes c2 |
full=−1full= −1 (Cb) |
OOO |
empty=+2empty= +2 |
Ca executes c3 |
full=−1full= −1 (Cb) |
OOO |
empty=+3empty= +3 |
Pb executes p1 |
full= full= |
empty= empty= |
|
Pa executes p1 |
full= full= |
empty= empty= |
|
Pa executes __ |
full= full= |
empty= empty= |
|
Pb executes __ |
full= full= |
empty= empty= |
|
Pb executes __ |
full= full= |
empty= empty= |
|
Pc executes p1 |
full= full= |
empty= empty= |
|
Cb executes __ |
full= full= |
empty= empty= |
|
Pc executes __ |
full= full= |
empty= empty= |
|
Cb executes __ |
full= full= |
empty= empty= |
|
Pa executes __ |
full= full= |
empty= empty= |
|
Pb executes p1-p3 |
full= full= |
empty= empty= |
|
Pc executes __ |
full= full= |
empty= empty= |
|
Pa executes p1 |
full= full= |
empty= empty= |
|
Pd executes p1 |
full= full= |
empty= empty= |
|
Ca executes c1-c3 |
full= full= |
empty= empty= |
|
Pa executes __ |
full= full= |
empty= empty= |
|
Cc executes c1-c2 |
full= full= |
empty= empty= |
|
Pa executes __ |
full= full= |
empty= empty= |
|
Cc executes c3 |
full= full= |
empty= empty= |
|
Pd executes p2-p3 |
full= full= |
empty= |
Solution:
The completed chart for scheduling processes with
corresponding values for full and =pc> semaphores are shown
below.
i hope it helps..
If you have any doubts please comment and please don't dislike.
PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME