When Should We Invoke the Deadlock Detection Algorithm

 

When Should We Invoke the Deadlock Detection Algorithm?

The timing of running the detection algorithm depends mainly on:

1️⃣ How often deadlocks occur

  • If deadlocks are frequent, detection should run frequently.

  • Otherwise, resources held by deadlocked threads remain idle for a long time.

  • The longer we wait, the more threads may become involved in the deadlock.


2️⃣ How many threads are affected

  • A deadlock may involve only a few threads initially.

  • If not detected early, the waiting chain may grow.

  • More threads → more resource wastage → worse system performance.


Extreme Approach: Run Detection on Every Failed Request

Deadlocks happen when a thread makes a request that cannot be granted immediately.

So in theory, we could:

Run the detection algorithm every time a resource request is denied.

Advantages

  • Detects deadlock immediately.

  • Identifies:

    • the set of deadlocked threads

    • the thread whose request completed the cycle

Reality Check

  • Every thread in the cycle contributes to the deadlock.

  • If there are many resource types, a single request may create multiple cycles.

  • Running detection this often causes high computational overhead.


Practical Approach: Run Detection Periodically

Instead of checking every request, systems often:

  • Run detection at fixed intervals

    • e.g., once per hour

  • Or based on system conditions

    • e.g., when CPU utilization drops below 40%

Why CPU drop?

  • Deadlocks reduce system throughput.

  • Waiting threads don’t use CPU.

  • So CPU usage falls — a sign something is wrong.


Problem with Periodic Detection

If detection runs only occasionally:

  • The resource graph may already contain many cycles

  • It becomes hard to determine:

    • which thread originally caused the deadlock


Real-World Example: Database Systems

Databases frequently face deadlocks because:

  • Transactions require multiple locks

  • Many transactions run concurrently

Most database systems include:

✔ Deadlock detection
✔ Deadlock recovery

They typically:

  1. Periodically search for cycles in the wait-for graph

  2. If deadlock is found:

    • Select a victim transaction

    • Abort and roll it back

    • Release its locks

  3. Resume other transactions

  4. Restart the aborted transaction later

For example, MySQL often chooses the victim transaction that minimizes the number of affected rows (insert/update/delete).


Simple Summary

  • Detection frequency depends on:

    • likelihood of deadlock

    • number of affected threads

  • Options:

    • Every failed request → accurate but expensive

    • Periodic checks → cheaper but less precise

  • Real systems (like databases) use periodic detection + rollback recovery.

Comments

Popular posts from this blog

Operating Systems OS PCCST403 Semester 4 BTech KTU CS 2024 Scheme

Introduction to Operating System -Virtualization, Concurrency, and Persistence

Differences Between Linux and Classic UNIX Kernels