Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Deadlock Detection and Recovery

Lesson 25 of 31 in the free Operating System & Linux Programming notes on Siksha Sarovar, written by Rohit Jangra.

Detection

If we run with no prevention or avoidance, we must periodically check whether deadlock has actually formed.

Single Instance per Resource Type

Build a wait-for graph by collapsing the resource-allocation graph: edge P → Q means P is waiting for a resource held by Q. Cycle → deadlock.

P1 → P2 → P3 → P1 forms a cycle, so {P1,P2,P3} are deadlocked. P4 is just waiting.

Multiple Instances

Use a Banker-style detection:

  • Available[m], Allocation[n][m], Request[n][m].
  • Same safety idea but with current Request instead of Need.
  • Any process whose Finish[i] remains false at the end is deadlocked.

When to Run Detection?

  • On every request — costly but immediate.
  • Periodically (e.g., every hour) — cheaper, deadlock lingers a while.
  • When CPU utilization drops — symptom of many waiting processes.

Recovery

Once deadlock is detected:

Option 1 — Process Termination

  • Abort all deadlocked processes — drastic but simple.
  • Abort one at a time until the cycle breaks. Choose the victim using:
  • Priority.
  • CPU time consumed and remaining.
  • Resources used / needed.
  • Number of processes that would need rollback.
  • Whether interactive or batch.

Option 2 — Resource Preemption

Take resources from some processes and hand them to others. Three issues:

  • Selecting a victim — minimize cost.
  • Rollback — return the victim to a safe state. Either total rollback (restart) or partial rollback to a checkpoint.
  • Starvation — same process must not always be the victim. Use a count of victim-selections in cost calculations.

Practical Considerations

  • Database transaction systems use deadlock detection and rollback heavily — transactions are designed to be redoable.
  • Operating systems usually rely on prevention (resource ordering) plus the ostrich algorithm for the rare slip.

Worked Example

Suppose A=7, B=2, C=6 instances. Processes:

ProcessAllocation A,B,CRequest A,B,C
P00,1,00,0,0
P12,0,02,0,2
P23,0,30,0,0
P32,1,11,0,0
P40,0,20,0,2

Available = 7-7, 2-2, 6-6 = (0,0,0). Run detection: P0 and P2 have zero requests → can finish, freeing (3,1,3). Now check P1 (2,0,2) ≤ (3,1,3) → finish, free (5,1,3). P3 (1,0,0) → finish (7,2,4). P4 (0,0,2) → finish. All finish — no deadlock.

If instead P1 had request (2,0,2) and P2 had request (0,0,1), the algorithm would find no candidate after P0, leaving P1, P3, P4, P2 as deadlocked.

Summary

  • Detection runs a Banker-style algorithm on current requests.
  • Recovery either kills processes or preempts resources, balancing cost and starvation.
  • Detection-and-recover is the practical default in most servers and DBMSs.