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

P 1 : P 2 : . . . . . . . . . . . . . . . . . . . . . . P(s); P(s); CS 1 ; CS 2

ID: 3547117 • Letter: P

Question

                    P1: P2:                 

                    . . . . . . . . . . . . . . . . . . . . . .                 

                    P(s); P(s);                 

                    CS1; CS2;                 

                    V(s); . . . . . . . . . . .                 

                    . . . . . . . . . . . . . . . . . . . . . .                 

                     What is the problem with the following two concurrent processes P1 and P2 implemented using a binary semaphore s initialized to 1?                 

                    P1: P2:                 

                    . . . . . . . . . . . . . . . . . . . . . .                 

                    P(s); V(s);                 

                    CS1; CS2;                 

                    V(s); P(s);                 

                    . . . . . . . . . . . . . . . . . . . . . .

Explanation / Answer

s is intialized to 1

P1: P2:

. . . . . . . . . . . . . . . . . . . . . .

P(s); P(s);

CS1; CS2;

V(s); .V(s) assuming


IN the above code there is no problem of mutual exclusion. At a time only one process can be in the critical section. but there is problem of starvation.

Suppose P1 is inside the critical section.

At the same time P2 want to go inside the critical section then it should wait because of s will be 0 this time.

after finishing the work in critical section it will make s=1 and P1 again want to gon inside the critical section and it got the chance then P2 has to wait agin for P1. If P1 again and again want to inside the critical and always getting chance then there is question that how much time P2 shd wait for its chance. This phenomenon is known as a starvation.

There is Starvation problem in above code.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

P1: P2:

P(s); V(s);

CS1; CS2;

V(s); P(s);



In the above code there is problem of mutual exclusion. At a time both P1 and P2 can go inside the critical secion which is not allowed.

First P1 will go inside the CS and it wll make s=0.

Now P2 will execute V(S) it will just increse the value of s =1 so P2 will be in the critical section.