VLSI DV Interview Puzzles · All levels

Inner disable fork Does Not Kill Outer Sibling

Which prints survive this nested-fork pattern, and why?

Puzzle

Difficulty: Hard · Puzzle 6 of 6 · Topic: Fork Join Puzzles

Which prints survive this nested-fork pattern, and why?

Code

systemverilog
module p6;
  initial begin
    fork
      begin
        fork
          begin #2 $display("%0t A", $time); end
          begin #4 $display("%0t B", $time); end
        join_any
        disable fork;
        $display("%0t C", $time);
      end
      begin
        #3 $display("%0t D", $time);
      end
    join
    $display("%0t E", $time);
  end
endmodule

Hint

`disable fork` applies to child processes of the process that calls it (its innermost active fork context).

Step-by-step solution

diagram
1) In inner fork, A prints at t=2; `join_any` unblocks immediately.
2) `disable fork` then kills inner sibling B before t=4.
3) C prints at t=2 after cleanup.
4) Outer sibling D is in a different fork context, so it still prints at t=3.
5) Outer join completes at t=3 and E prints at t=3.

Answer

Answer: A@2, C@2, D@3, E@3; B does not print

Why candidates get it wrong

A common bug is assuming `disable fork` is global and kills unrelated sibling branches.

Interviewer follow-up

How would you explicitly terminate both outer branches from inside the first branch?

Related topics