How are deadlocks handled?

Dealing with deadlocks in computer systems can be approached in three main ways:

  1. Prevention or Avoidance: This approach involves implementing protocols to ensure that deadlocks do not occur. By preventing or avoiding deadlocks, the system is kept in a state where deadlock conditions are never met. Prevention strategies focus on negating one or more of the necessary conditions for a deadlock, such as mutual exclusion, hold and wait, no preemption, and circular wait, by imposing constraints on how resources are requested. Avoidance techniques, on the other hand, require the system to have prior knowledge of the resources each process will request and use throughout its lifetime. With this information, the system can make informed decisions on whether to grant or delay resource requests to avoid deadlocks.

  2. Detection and Recovery: In scenarios where prevention or avoidance is not implemented, systems can still manage deadlocks by detecting when they occur and then recovering from them. This method involves monitoring the system's state to identify deadlocks and then taking action to resolve them, such as terminating processes or forcefully taking resources away from them. This approach accepts that deadlocks may occur but deals with them as they arise.

  3. Ignoring the Problem: Surprisingly, many operating systems, including Linux and Windows, choose to ignore the deadlock problem. This doesn't mean they are unaware of deadlocks but rather that they accept the risk of their occurrence as part of the system's operation. It becomes the responsibility of application developers to manage deadlocks within their programs. This approach is chosen primarily because deadlocks are relatively rare and the cost of implementing comprehensive deadlock management strategies may not justify the benefits for every system.

Each of these methods has its pros and cons, and some researchers argue that no single approach is universally best for all types of resource-allocation problems. Systems can, therefore, adopt a combination of strategies tailored to different resource types and application needs.

  • Prevention and Avoidance: Detailed algorithms for these strategies are explored in specific sections of the discussion (in further blogs), highlighting methods to ensure that systems either do not reach deadlock conditions or smartly navigate resource requests to avoid potential deadlocks.

  • Detection and Recovery: When neither prevention nor avoidance is in place, systems need mechanisms to detect deadlocks and strategies to recover from them. Without these mechanisms, undetected deadlocks can severely degrade system performance and eventually necessitate manual intervention.

Ignoring deadlocks is a pragmatic choice for some systems, prioritizing cost savings and simplicity over the complexity of managing deadlocks. This approach relies on the infrequency of deadlocks and the existence of general recovery methods for system issues, which can also be applied to resolve deadlock situations when they are identified manually.