Value of a counting semaphore — Aptitude Question
Value of a counting semaphore: A counting semaphore is initialised to 10. Six wait (P) operations and four signal… Answer and worked solution on CodeKairo.
- Topic: OS, DBMS & Networks
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 75 seconds
Question
A counting semaphore is initialised to 10. Six wait (P) operations and four signal (V) operations are then completed on it. What is its value?
Options
- 0
- 4
- 8
- 10
Show the answer and the worked solution
Correct answer: C. 8
Worked solution
- Each completed
waitdecrements the semaphore by one, so six of them subtract 6. - Each
signalincrements it by one, so four of them add 4. - The value is 10 − 6 + 4 = 8, meaning eight units of the resource are still free.
Answer: 8.
The idea
A counting semaphore's value is the number of units still available. A wait that would take it below zero blocks instead, so a stated 'completed' wait is one that got through.
Shortcut. Net change = signals − waits = 4 − 6 = −2, so 10 − 2 = 8.
Common trap. 4 is 10 − 6, applying the waits and forgetting the four signals that add units back.