Project: Deadlock Prevention and Recovery Toolkit — Banker's Algorithm & Resource Allocation Graph
Author: Prajithaa Parani
Course: B.Tech Gen.AI, Lovely Professional University
A lightweight web-based simulator that detects, prevents, and recovers from deadlocks in real-time.
Implements the Banker’s Algorithm for safety checks, shows a live Resource Allocation Graph (RAG) using SVG, and provides an interactive interface to:
- create processes/resources,
- edit
Available,Max,Allocationmatrices, - compute
Needand check safety, - simulate requests,
- abort a process to recover from deadlock.
The UI is pure HTML/CSS/JS and is copy-paste runnable as a single index.html.
deadlock-prevention-and-recovery-toolkit/
│
├── index.html # Main project file (HTML + CSS + JS combined)
├── README.md # Complete project documentation
├── LICENSE # Optional (MIT or your preferred license)
│
├── assets/ # Folder for visuals (optional)
│ ├── demo.gif
│ ├── screenshot1.png
│ └── screenshot2.png
│
├── js/ # Folder for scripts (optional)
│ └── main.js # Banker's Algorithm & graph logic
│
├── css/ # Folder for styles (optional)
│ └── style.css # UI design & styling
│
└── docs/ # Folder for reports & presentations (optional)
├── system_design_report.pdf
├── algorithm_explanation.pdf
└── project_presentation.pptxindex.html— the full app (HTML, CSS, JS combined).Use the HTML you already have (the UI with matrices, Banker's logic, graph drawing & example).
README.md— (this file).LICENSE— optional.
- Create any number of processes (P) and resource types (R).
- Dynamic input fields for:
Availablevector (R length)Maxmatrix (P x R)Allocationmatrix (P x R)
- Auto-compute
Need = Max - Allocation. - Banker’s safety algorithm to check safe / unsafe states and produce safe sequence when available.
- Simulate
requestfrom any process; the algorithm tentatively allocates, checks safety, grants or denies accordingly (prevention). - Visual SVG Resource Allocation Graph:
- Processes as circles at top
- Resources as rounded rectangles at bottom
- Blue arrows = allocation (resource → process)
- Orange arrows = request (process → resource)
- Labels show allocation counts
Exampleprefill (classic Banker's demo) to test known safe/unsafe cases.Recoverybutton: abort the selected process to free its resources (simple recovery strategy).- Input validation: prevents Allocation > Max (auto-adjusts), prevents requests > Need, prevents allocation if Available insufficient.
- Friendly messages and status labels (safe/unsafe, granted/denied, adjustments made).
- Put
index.html(the single HTML file) in a folder. - Open
index.htmlin any modern browser (Chrome/Edge/Firefox). - Interact:
- Set process/resource counts → Create Matrices
- Fill tables or click Example
- Click Compute Need & Check Safety to view
Needand safety result - Use Simulate a resource request to try allocation
- Use Draw Allocation Graph to visualize
- Use Recovery to abort a process
No server required.
P— number of processesR— number of resource typesAvailable— array length R: how many instances of each resource freeMax— array of arrays P x R: maximum demand per processAllocation— array of arrays P x R: currently allocated units per processNeed— array of arrays P x R: computed asMax - Allocation
- Compute
Need[i][j] = Max[i][j] - Allocation[i][j]for all processes i and resources j. - Initialize
Work = Available(copy) andFinish[i] = falsefor all processes. - Find a process
isuch thatFinish[i] == falseandNeed[i] <= Work(component-wise). - If found:
Work = Work + Allocation[i](release resources after simulated completion)Finish[i] = true- append
ito safe sequence - repeat step 3
- If all
Finish[i]becometrue, system is SAFE; safe sequence returned. Otherwise NOT SAFE.
- Verify
req <= Need[pid]. If not, deny. - Verify
req <= Available. If not, request must wait. - Tentatively:
Available = Available - reqAllocation[pid] = Allocation[pid] + reqNeed[pid] = Need[pid] - req
- Run safety check:
- If safe → permanently grant (state stays)
- If unsafe → rollback tentative changes, deny to prevent unsafe state
- Choose a process
pidto abort. Available += Allocation[pid]- Set
Allocation[pid] = 0,Need[pid] = 0,Max[pid] = 0(or mark process terminated) - Recompute safety
- Always validate that
Allocation[i][j] <= Max[i][j]. If user mistakenly sets Allocation greater than Max, auto-correct and warn. - Use
Number.parseInt(..., 10)or+valueto parse ints (and default to 0 on NaN). - When drawing SVG graph, recompute positions on every change to keep visualization synchronized.
- Keep the
Needmatrix updated after any change toMax,Allocation, orAvailable. - Provide clear UI colors and text for states:
.status.safe→ green text.status.unsafe→ red text- Request results use subtle color cues
Available = [3, 3, 2]Max = [ [7,5,3], [3,2,2], [9,0,2], [2,2,2], [4,3,3] ]Allocation = [ [0,1,0], [2,0,0], [3,0,2], [2,1,1], [0,0,2] ]- After compute →
Needand safe sequence should show: P1 → P3 → P4 → P0 → P2 (classic demonstration).
(This is pre-filled in the example function in the provided HTML.)
Q: I entered Allocation > Max for some cell — what happens?
A: UI auto-adjusts Allocation down to Max for safety and informs you.
Q: Granting a request says “Not enough available” but I think there are enough?
A: Check Available vector; simulated tentative allocation reduces Available. Also ensure request ≤ Need.
Q: The graph arrows overlap and look messy for large P/R
A: This demo targets small P/R (practical for learning). For larger P/R, consider improved layout algorithms (force-directed graph) or grouping.
Q: I aborted a process but the UI still shows it
A: Aborting sets its Max and Need to 0. You can also reload the page or implement a “remove process” feature to hide terminated processes.
MIT License Copyright (c) 2025 Prajithaa Parani