1,553 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

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 votes
1 votes

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

1 votes
1 votes
2 answers
2
sh!va asked Nov 2, 2016
4,412 views
A counting semaphore S is created with initial value as 3.Suppose 5 processes are calling P, what will be the final value of S?A. 8B. 0C. -2D. -1
3 votes
3 votes
3 answers
3
GateAspirant999 asked Feb 14, 2017
2,534 views
Below are four concurrent processes and three counting semaphores.What must be the initial values of the three semaphores, so that output “TGE” is obtained?