There are three processes P1 , P2 and P3 sharing a semaphore for synchronizing…

2017

There are three processes P1 , P2 and 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 semaphore in following order :

(a) P2 needs to access

(b) P1 needs to access

(c) P3 needs to access

(d) P2 exits critical section

(e) P1 exits critical section

The final value of semaphore will be :

  1. A.

    0

  2. B.

    1

  3. C.

    - 1

  4. D.

    - 2

Attempted by 360 students.

Show answer & explanation

Correct answer: A

Key idea: wait (P) decrements the semaphore value and blocks the process if the result is negative; signal (V) increments the value and, if the result is less than or equal to zero, wakes one waiting process. A negative value indicates how many processes are waiting.

  • Event (a) P2 needs to access: 1 → 0 (P2 enters critical section).

  • Event (b) P1 needs to access: 0 → -1 (P1 blocks; 1 waiting).

  • Event (c) P3 needs to access: -1 → -2 (P3 blocks; 2 waiting).

  • Event (d) P2 exits (signal): -2 → -1 (one waiting process is awakened and enters the critical section; 1 waiting remains).

  • Event (e) P1 exits (signal): -1 → 0 (the remaining waiting process is awakened).

Final value: 0

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor