All mock tests are FREE till 31 December — register to save your progress

Three processes share a single counting semaphore S whose initial value is 1. All three execute wait(S) one after another, in the order P2, P1, P3. After this, P2 exits its critical section by executing signal(S), and then P1 exits by executing signal(S). What is the value of S after these five operations?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: A. 0

Explanation

Tracing S step by step from its initial value 1: wait(S) by P2 gives S=0 and P2 enters its critical section; wait(S) by P1 gives S=-1 and P1 blocks in the semaphore's queue; wait(S) by P3 gives S=-2 and P3 also blocks. P2 then calls signal(S), giving S=-1 and waking P1 (which now enters its critical section); P1 then calls signal(S), giving S=0 and waking P3. So after these three wait() and two signal() calls, S = 1 − 3 + 2 = 0, option (i). Note that P3 is still inside its critical section at this point — it has not yet executed its own signal(S).

More Computer System Architecture questions