3,060 views
0 votes
0 votes

What if lock=True initially and process P1, P2 ,P3 are trying to enter into critical section 

If already lock is true then again in Test - set Lock is becoming true

I didn't get how Test- set is ensuring Mutual Exclusion

1 Answer

Best answer
4 votes
4 votes

First of all let me tell you that you are absolutely right that, if lock variable is initially true then it will never go forward. It blocks all the three process there only. 

If you notice then test-set looks like a function, but it's not a function, it's an instruction. Check this,

So, Now question is that what is the meaning of having TestAndSet as instruction? The advantage of having TestAndSet, as instruction is that, When it executes then it executes completely, It never get preempted in between. 

Now Consider Lock = False

P1 will go for enter, P1 is going for execution, it gets value false in rv, it sets lock as True, and return false. Due to the false, condition is failed, hence P1 goes for executing critical section. 

Now suppose while P1 is in critical section, P2 goes for execution, its gets Lock value true in rv, assigning true in lock and return true, condition is true, it will wait there only. 

Similar for P3.

Hence Mutual exclusion preserved. 

selected by

Related questions

0 votes
0 votes
1 answer
2
ejaz asked Jun 7, 2018
734 views
https://gateoverflow.in/1319/gate2009-33In above link , test-and-set is deadlock-free buthttps://gateoverflow.in/871/gate2002-18-aIt is not deadlock-free here.It will be ...
0 votes
0 votes
1 answer
3
2 votes
2 votes
1 answer
4
Siramdas Vamshidhar asked Oct 15, 2014
2,375 views
suppose R=3,2,4,3,4,2,2,3,4,5,6,7,7,6,5,4,5,6,7,2,1 is a page reference stream.if the window size is 6 and assuming pure demand paging.how may page faults will it cause u...