Operating System

⌘K
  1. Home
  2. Docs
  3. Operating System
  4. Deadlocks
  5. Handling Deadlocks

Handling Deadlocks

Handling deadlocks in an operating system involves various strategies to prevent, avoid, detect, and recover from situations where processes are stuck waiting indefinitely due to resource dependencies.

There are four primary approaches to handling deadlocks:

  • Deadlock Prevention
  • Deadlock Avoidance
  • Ignoring Deadlock (Ostrich Algorithm)
  • Deadlock Detection and Recovery

Deadlock prevention ensures that at least one of the Coffman conditions (Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait) is violated, thus preventing deadlock from occurring in the first place.

There are several ways to accomplish this:

  • Eliminate Mutual Exclusion: This approach tries to make resources shareable so that multiple processes can access them concurrently. However, this is only possible for resources that don’t require exclusivity, such as read-only files.
  • Eliminate Hold and Wait: Require processes to request all resources they need at once, rather than holding onto some and waiting for others. Although effective, this method can lead to underutilization of resources and increased waiting times.
  • Eliminate No Preemption: Allow preemption of resources by taking them away from a process if it is holding a resource while waiting for another. This works well for preemptable resources (e.g., CPU, memory), but it’s not feasible for non-preemptable resources like printers or disk drives.
  • Eliminate Circular Wait: Impose an ordering on resource types (e.g., assigning numbers to resources) and require processes to request resources in that order. This ensures that no circular dependencies can form, preventing deadlock.

It is a method used to ensure that a system never enters a deadlock state by carefully allocating resources to processes. It involves dynamically analyzing the system’s current state and resource requests to determine if granting a request will lead to a safe state.

  • Safe State
    • A state in which the system can allocate resources to all processes in a specific order without entering a deadlock is called as safe state
  • Unsafe State
    • A state in which allocation of resources might lead the system into a deadlock state is called unsafe state.
  • Deadlock State
    • A state in which two or more processes are permanently waiting for resources held by each other, preventing further progress is called a deadlock state.

The Banker’s Algorithm is a classic deadlock avoidance approach.

  • Banker’s Algorithm: This algorithm checks if a system is in a “safe state” before granting resources to a process. A state is considered safe if there is a sequence in which all processes can complete without getting stuck. If the requested resource allocation would lead to an unsafe state, the request is denied until the system returns to a safe state.

The Ostrich Algorithm is a strategy where the operating system deliberately ignores the possibility of deadlock, assuming that deadlocks are rare and their cost is lower than the cost of implementing deadlock prevention, avoidance, or detection mechanisms.

  • Relies on restarting systems or killing processes as a fallback.
  • Based on the idea that addressing every possible deadlock might be overkill.

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *