Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
    • Testbench Debug Puzzles: Why Did the Test Hang?
    • Deadlock Scenario Puzzles: Objection and Phase Stalls
      • Early Return Leaves Objection Raised
      • Background Poller Keeps Running After Traffic Ends
      • Semaphore Token Leak on Error Path
      • join_any Ends the Task with the Mailbox Full
      • Lock Inversion with Two Semaphores
      • disable fork Kills the Thread That Owned the Drop
    • Scoreboard Puzzles: Mismatch or Ordering Bug?
    • Real Interview Rounds: End-to-End Debug Walkthrough
    • Scenario & Debug Puzzles: Tricky Q&A
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←Deadlock Scenario Puzzles: Objection and Phase Stalls
  1. Topics
  2. DV Interview Puzzles
  3. Scenario & Debug Puzzles
  4. Deadlock Scenario Puzzles: Objection and Phase Stalls
  5. Lock Inversion with Two Semaphores
On this page

    VLSI DV Interview PuzzlesAll levels

    Lock Inversion with Two Semaphores

    Two agents deadlock rarely under high parallel load. The failure disappears if either agent runs alone.

    Puzzle

    Difficulty: Medium · Puzzle 5 of 6 · Topic: Deadlock Scenario Puzzles: Objection and Phase Stalls

    Two agents deadlock rarely under high parallel load. The failure disappears if either agent runs alone.

    Code

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    semaphore cfg_sem = new(1);
    semaphore bus_sem = new(1);
    
    task agent_a();
      cfg_sem.get(1);
      #1ns;
      bus_sem.get(1);
      bus_sem.put(1);
      cfg_sem.put(1);
    endtask
    
    task agent_b();
      bus_sem.get(1);
      #1ns;
      cfg_sem.get(1);
      cfg_sem.put(1);
      bus_sem.put(1);
    endtask

    Hint

    Classic AB-BA acquisition ordering bug.

    Step-by-step solution

    Diagram
    1) Capture semaphore acquisition timeline from both agents on failing seed.
    2) Confirm agent_a holds cfg_sem waiting bus_sem while agent_b holds bus_sem waiting cfg_sem.
    3) Enforce one global lock order across all users (for example cfg then bus everywhere).
    4) Optionally merge locks if independent parallelism is not needed.
    5) Add lock-order assertions in debug builds.

    Answer

    Answer: Bug: lock inversion causes circular wait (AB-BA deadlock). Fix: enforce consistent semaphore acquisition order across all threads.

    How this was checked

    Checked by running the code in Verilator 5; the printed output supports the answer above. Plain-SV semaphore model of AB-BA lock inversion: opposite acquire order deadlocks, one common order finishes.

    Why candidates get it wrong

    Random seed changes only shift timing; they do not remove the structural deadlock risk.

    Interviewer follow-up

    If lock ordering cannot be unified, what timeout/retry strategy would you choose and why? Model answer: Acquire the second lock with try_get; if it fails, release the first lock, back off for a random delay and retry, so a circular wait can never persist. Add a watchdog that logs who holds each lock; timeouts alone only detect the deadlock, they do not break it.

    Say it out loud

    First I list which thread holds which semaphore and which one it is waiting for, because a hang that depends on timing looks like a circular wait. The root cause is lock inversion, where one thread takes A then B and another takes B then A, so each waits on the other. Changing seeds only shifts the timing and does not remove the structural risk. I confirm it by drawing the wait cycle, and the fix is a consistent acquisition order across all threads.

    Related topics

    • Finding the Cycle

    • Who Is Waiting for Whom

    • Back to Deadlock Scenario Puzzles: Objection and Phase Stalls

    • Scenario & Debug Puzzles section

    Previous

    196 / 293

    Next: disable fork Kills the Thread That Owned the Drop

    Up next

    Up nextdisable fork Kills the Thread That Owned the Drop

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Early Return Leaves Objection Raised
    • Background Poller Keeps Running After Traffic Ends
    • Semaphore Token Leak on Error Path