Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider this code for the producer-consumer problem: semaphore e = n, f = 0; /*

ID: 3680157 • Letter: C

Question

Consider this code for the producer-consumer problem:

                   semaphore e = n, f = 0;    /* e = empty buffer, f = full buffer */

                   binary_semaphore b = 1;

                   producer:

                           while (1) {

                                   P(e);

                                   bP(b);

                                   add_to_buffer;

                                   bV(b);

                                   V(f);

                           }

                   consumer:

                           while (1) {

                                   P(f);

                                   bP(b);

                                   take_from_buffer;

                                   bV(b);

                                   V(e);

                           }

(1) What is the effect of interchanging bP(b) and P(e) in the producer process in the producer-consumer problem?

(2) What is the effect of exchanging bV(b) and V(f) in the producer process in the producer-consumer problem?

Explanation / Answer

(1) When you interchange bP(b) and P(e) in producer process.

Your code looks like

while (1) {

                              

                                   bP(b);

                                   P(e)

                                   add_to_buffer;

                                   bV(b);

                                   V(f);

                           }

                   consumer:

                           while (1) {

                                   P(f);

                                   bP(b);

                                   take_from_buffer;

                                   bV(b);

                                   V(e);

                           }

Now, here consider a situation such that e=0 and f=n b =1 and producer started executing so, it will first do b=0 and it will wait at e as it is equal to 0 and as the time slice ends of producer process it will run consumer process. The consumer process have f=n which will become as n-1 after running P(f) but when it reaches bP(b) as b=0 it wait till its time slice ends. As time slice ends it move to producer process but here e=0 so it again waits and this process goes continuously undefinitely. Both of the process are in deadlock now.

(2) When you exchange bV(b) and V(f) in producer process it does not effect at all to any process as it is a signal call that is it is unblocking call. So interchanging their order does not make any difference.