33. What is a deadlock and how do you prevent it?
HardOS, networking & concurrency
Technical
Show model answer
A deadlock occurs when threads each hold a resource and wait for another held by someone else, so none can proceed. It needs four conditions: mutual exclusion, hold and wait, no preemption, and circular wait.
Prevent it by acquiring locks in a consistent global order, using timeouts (tryLock), reducing lock scope, or using lock-free structures and message passing.