VLSI DV Interview Puzzles · All levels

Leaked Semaphore Token on Error Path

Predict output and explain why worker B times out even though worker A exits quickly.

Puzzle

Difficulty: Medium · Puzzle 4 of 6 · Topic: Semaphore & Mailbox Puzzles

Predict output and explain why worker B times out even though worker A exits quickly.

Code

systemverilog
module tb;
  semaphore sem = new(1);

  task worker_a();
    sem.get(1);
    $display("[%0t] A got token", $time);
    if (1) return; // missing sem.put(1)
  endtask

  task worker_b();
    int ok;
    #10;
    ok = sem.try_get(1);
    $display("[%0t] B try_get=%0d", $time, ok);
  endtask

  initial fork
    worker_a();
    worker_b();
  join
endmodule

Hint

Resource leaks are deadlocks in slow motion.

Step-by-step solution

diagram
1) A gets token at t=0 and exits without put.
2) Token count stays 0 permanently.
3) B tries at t=10; try_get returns 0.
4) No one can acquire semaphore afterward.

Answer

Answer: Prints [0] A got token and [10] B try_get=0. Missing put leaks token and blocks all future owners.

Why candidates get it wrong

Returning early without finally-style cleanup is a frequent infrastructure bug.

Interviewer follow-up

How would you enforce token release using begin...end with automatic cleanup patterns?

Related topics