AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Mediumsemaphoresynchronisation

~75s

Value of a counting semaphore

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?

Show the answer and the worked solution

Correct answer: C.

8

Worked solution

  1. Each completed wait decrements the semaphore by one, so six of them subtract 6.
  2. Each signal increments it by one, so four of them add 4.
  3. 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.

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.

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

  1. 0
  2. 4
  3. 8
  4. 10
Show the answer and the worked solution

Correct answer: C. 8

Worked solution

  1. Each completed wait decrements the semaphore by one, so six of them subtract 6.
  2. Each signal increments it by one, so four of them add 4.
  3. 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.

All OS, DBMS & Networks questions · placement mock tests