294 views
0 votes
0 votes

s = 0 initially

A : down(s)               B : down(s) 

     CS                           CS

     up(s)                        up(s)

C : down(s)              D : up(s) 

     CS                           CS

     up(s)                       down(s)

E : up(s)                   F : up(s) 

     CS                          CS

   down(s)                     down(s)

What is the maximum value of s?

Doubt : I think max value of s should be 1 as P and V are atomic operations. Also nowhere the value of s is stored in memory here. But the answer given is 3. What concept am I missing?

1 Answer

Best answer
2 votes
2 votes
A process can preempt even when its running at critical section, UP and DOWN are atomic operation so no preemption is possible.
Assuming we can run A, B, C, D, E, F in any sequence.
So First run D, D executes UP operation on S, So S = 1.. and enters in critical section, preempt it with E.
E execute UP operation on S, So S = 2.. and enters in critical section, preempt it with F.
F execute UP operation on S, so S = 3..  and it enters in critical section, preempt it with A
A will do DOWN operation on S, so S = 2, Completes its execution, S = 3
B will do DOWN operation on S, so S = 2, Completes its execution, S = 3
C will do DOWN operation on S, so S = 2, Completes its execution, S = 3
D completes its execution, S = 2
E completes its execution, S = 1
F completes its execution, S = 0
So maximum value possible  for S is 3, At the end of the execution of A, B, C, D, E, F..  S = 0, Try it with any sequence you will get the same result.
selected by

Related questions

0 votes
0 votes
1 answer
1
Mrityudoot asked Jan 27
274 views
Can a counting semaphore acquire a negative value?S = 2;15 P operations done, should the semaphore be 0 or -13
1 votes
1 votes
1 answer
4