recategorized by
1,911 views
1 votes
1 votes

Q1. Consider the methods used by process P1 and P2 for accessing their critical sections whenever needed, as given below. The initial values of shared Boolean variables, S1 and S2 are random assigned.

For the program to guarantee mutual exclusion, the predicate P and Q in the while loop should beA

A.

B.  

C.

D.

ans is option B. My  doubt is what is the case in which option D will not satisfy mutual exclusion? I am confused between B and D.

recategorized by

1 Answer

0 votes
0 votes

a) P:flag[j]=true and S1!=S2

    Q:flag[i]=true and S1==S2

 Initially , flag[i]=flag[j]=false.

 

P1 will make flag[i] =false, true and Make S1=S2 so , if P:flag[j]=true and S1==S2 then P will be false hence no waiting in loop. So P1 will enter the CS.

Now check that P2 can enter the CS or not when P1 is in CS.

P2 will make flag[j]= false ,true and make S1=S2+1, so ,if    Q:flag[i]=true and S1==S2 then Q will be false hence no waiting in loop. So, P2 will enter the CS.

So, both processes will enter the critical section.

Hence, option (a) is false.

 

b) P:flag[j]=true and S1==S2

    Q:flag[i]=true and S1!=S2

P1 will make flag[i] =false, true and Make S1=S2 so , if P:flag[j]=true and S1==S2 then P will be false as flag[j]=false, hence no waiting in loop. So P1 will enter the CS.

Now check that P2 can enter the CS or not when P1 is in CS.

P2 will make flag[j]= false ,true and make S1=S2+1, so ,if    Q:flag[i]=true and S1!=S2 then Q will be true as (flag[i]=true and S1!=S2)=TRUE, hence,P2 will wait in the loop.

So, only one  process will enter the critical section.

Hence, option (b) is true.

 

c) P:flag[i]=true and S1!=S2

    Q:flag[j]=true and S1==S2

P1 will make flag[i] =false, true and Make S1=S2 so, if P:flag[i]=true and S1!=S2 then P will be false hence no waiting in loop. So P1 will enter the CS.

Now check that P2 can enter the CS or not when P1 is in CS.

P2 will make flag[j]= false ,true and make S1=S2+1, so ,if    Q: flag[j]=true and S1!=S2then Q will be false hence no waiting in loop. So, P2 will enter the CS.

So, both processes will enter the critical section.

Hence, option (c) is false.

 

d) P:flag[i]=true and S1==S2

    Q:flag[j]=true and S1!=S2

 

P1 will make flag[i] =false, true and Make S1=S2 so, if P:flag[i]=true and S1==S2 then P will be true hence  waiting in loop. So P1 will not enter the CS.

Now check for P2 ,

P2 will make flag[j]= false ,true and make S1=S2+1, so ,if    Q: flag[j]=true and S1!=S2then Q will be TRUE hence waiting in loop. So, P2 cannot enter the CS.

But Now, if again preemption happens, then P2 has made S1!=S2 So,P1 can enter the CS and P2 will wait in the loop. So, mutual exclusion holds.  

Hence, option (d) is true.

Correct me if i am wrong

 

 

 

Related questions

0 votes
0 votes
1 answer
3
N3314nch41 asked Sep 10, 2023
360 views
How to approach synchronization (specifically semaphore) question, there size are really intimidating and i’m unable to decode the code written? What to do??
0 votes
0 votes
2 answers
4
shivajikobardan asked Jul 22, 2023
800 views
Sorry if this is a stupid question. But it really intrigued me. Same resources at different algorithms are telling different ways to test these stuffs.Here's an algorith...