in Operating System
1,539 views
0 votes
0 votes
There are Three processesP1,P2,P3 sharing a semaphore for synchronizing a variable, Initial value of semaphore is one. Assume that negative value of semaphore tells us how many processes are waiting in queue. Processes access the semphore in following order

a. P2 needs to access

b. P1 needs to access

c. P3 needs to access

d. P2 exists critical section

e. P1 exits critical section

The final value of semaphore will be

1. 0

2. 1

3. -1

4. -2
in Operating System
1.5k views

2 Answers

3 votes
3 votes
initial value of semaphore $S$ is $1$

$P_2$ needs to access , decrement $S$ by $1$   $ ( S = S - 1 = 1-1 = 0)$                       

$P_1$ needs to access , decrement $S$ by $1$   $( S = S - 1 = 0 - 1 = -1)$                   

$P_3$ needs to access , decrement $S$ by $1$    $( S = S - 1 = -1 - 1 = -2)$

$P_2$ exits the critical section , increment $S$ by $1$    $( S = S + 1 = -2 + 1 = -1)$

$P_1$ exits the critical section , increment $S$ by $1$    $( S = S + 1 = -1 + 1 = 0)$

 The final value is $0$
1 vote
1 vote

needs to access the critical section means P() operation on Semaphore

exit critical section means V() operation on Semaphore.

given that intial value of semaphore = 1

final value of semaphore = intial value + no.of V() operations - no.of P() operations

                                           = 1+2-3

                                           = 0

Related questions