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 Prevention and Avoidance (Banker's Algorithm)

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

Prevention — Break a Coffman Condition

  1. Mutual exclusion — sometimes unavoidable (printers). Spooling avoids it for some devices.
  2. Hold and wait — require processes to request all resources upfront; or release everything before requesting more. Drawback: low utilization, possible starvation.
  3. No preemption — if a process holding resources requests more and is denied, force it to release everything; restart later.
  4. Circular wait — impose a total order on resource types; processes must request in increasing order. Effective and widely used in kernels.

Avoidance — Banker's Algorithm

Avoidance allows all four conditions but dynamically rejects any allocation that would put the system into an unsafe state. Requires advance knowledge of each process's maximum need.

State Variables

  • n = number of processes, m = number of resource types.
  • Available[m] — currently free instances of each type.
  • Max[n][m] — maximum demand of each process.
  • Allocation[n][m] — currently allocated.
  • Need[n][m] = Max - Allocation.

Safe State

A state is safe if there exists a sequence ⟨P_{i1}, P_{i2}, ..., P_{in}⟩ such that for each P_{ik}, its remaining Need can be satisfied by Available + the allocations of all P_{ij} (j < k).

Safety Algorithm

  1. Work = Available, Finish = [false, ...].
  2. Find i with Finish[i] = false and Need[i] ≤ Work. If none, stop.
  3. Work += Allocation[i]; Finish[i] = true. Goto 2.
  4. If all Finish[i] = true, state is safe.

Resource-Request Algorithm

On request Request[i] from process Pi:

  1. If Request[i] > Need[i]: error.
  2. If Request[i] > Available: Pi waits.
  3. Pretend to allocate: Available -= Request; Allocation[i] += Request; Need[i] -= Request.
  4. Run safety algorithm.
  • If safe → grant.
  • Else → roll back the pretence and Pi waits.

Worked Example

Three resource types A=10, B=5, C=7. Five processes:

ProcessAllocation A,B,CMax A,B,CNeed A,B,C
P00,1,07,5,37,4,3
P12,0,03,2,21,2,2
P23,0,29,0,26,0,0
P32,1,12,2,20,1,1
P40,0,24,3,34,3,1

Available = 10 - 7, 5 - 2, 7 - 5 = (3,3,2).

Run safety: Work = (3,3,2). Need P1 = (1,2,2) ≤ Work → finish P1, Work = (5,3,3). Need P3 = (0,1,1) ≤ Work → finish P3, Work = (7,4,4). Need P0, Need P2, Need P4 each fit in turn → safe sequence ⟨P1,P3,P4,P2,P0⟩.

Now P1 requests (1,0,2). Available becomes (2,3,0); Allocation P1 = (3,0,2); Need P1 = (0,2,0). Re-run safety → still safe → grant.

But if P0 then requests (0,2,0), Available (2,1,0) — re-run safety; if no sequence found, deny.

Summary

  • Prevention is conservative; Banker's avoidance is dynamic but needs Max info upfront.
  • Safety algorithm runs in O(m·n²) — feasible only for moderate workloads.
  • Most general-purpose OSes prefer detection or "ignore".