Skip to content

kasim672/DeadlockDetectionandAvoidanceSimulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🏦 Banker's Algorithm — Deadlock Avoidance Simulator

Operating Systems Project · Python · Streamlit


📌 Overview

This project simulates the Banker's Algorithm, a classic deadlock avoidance technique in Operating Systems, proposed by Edsger W. Dijkstra in 1965. The simulator allows you to interactively enter system state data, run the safety algorithm step-by-step, and visualize whether a system is in a safe or unsafe state.

The name "Banker's Algorithm" comes from its analogy to banking — just as a bank only grants loans if it can still serve all remaining customers, an OS only allocates resources if the system remains in a safe state afterward.


🎯 What This Simulator Does

The simulator acts as a mini operating system decision engine. Given a set of processes and resource types, it answers:

"If I allocate resources to these processes in this state — will the system ever get stuck?"

  • If YES, it can finish all processes safely → System is in a SAFE STATE
  • If NO, some process will be permanently blocked → System is in an UNSAFE STATE

🧠 Algorithm Explanation

Data Structures Used

Structure Dimension Description
Available[j] 1 × m Free instances of resource type Rⱼ
Max[i][j] n × m Maximum demand of process Pᵢ for resource Rⱼ
Allocation[i][j] n × m Resources of type Rⱼ currently held by Pᵢ
Need[i][j] n × m Remaining demand: Max[i][j] − Allocation[i][j]

Core Formula

Need[i][j] = Max[i][j] − Allocation[i][j]

This tells us how many more resources each process may still request before it finishes.

Safety Algorithm (Pseudocode)

Work  ←  Available                    // copy of free resources
Finish[i] ← False  for all i          // no process finished yet

REPEAT:
    Find process i such that:
        Finish[i] == False
        AND Need[i] ≤ Work             // can complete with current Work

    If found:
        Work      ← Work + Allocation[i]   // process finishes, releases resources
        Finish[i] ← True
        Add i to Safe Sequence

    Else:
        BREAK                          // no eligible process found

If all Finish[i] == True  →  SAFE STATE   ✅
Else                       →  UNSAFE STATE ❌

Time Complexity: O(n² × m)


⚠️ Coffman's Four Conditions for Deadlock

Deadlock can only occur if all four of the following conditions hold simultaneously:

  1. Mutual Exclusion — At least one resource is non-shareable
  2. Hold and Wait — A process holds a resource while waiting for more
  3. No Preemption — Resources cannot be forcibly taken away
  4. Circular Wait — A circular chain of processes waiting on each other

The Banker's Algorithm prevents deadlock before it happens by ensuring that no resource allocation can ever lead to a state where circular wait is possible.


🔐 Safe State vs Unsafe State vs Deadlock

Safe State Unsafe State Deadlock
Definition A safe execution sequence exists No guarantee all processes finish Processes are permanently stuck
Deadlock? Cannot occur May occur Currently occurring
OS Action Grant request Deny request Recovery needed

⚠️ Key Insight: Unsafe ≠ Deadlocked. An unsafe state means deadlock is possible — not guaranteed. The algorithm conservatively denies all requests that could lead to an unsafe state.


💻 Features

  • Interactive Matrix Input — Enter Allocation, Max, and Available matrices via editable tables
  • Live Validation — Catches errors like Allocation > Max before running
  • Auto Need Matrix — Computed instantly from Max − Allocation
  • Step-by-Step Trace — See every iteration: Work vector, candidates, condition checks, Finish array
  • Summary Table — Full simulation in clean tabular form
  • Safe/Unsafe Result Banner — Clear colour-coded final verdict
  • Built-in Presets — Load classic textbook examples with one click
  • Reset Option — Clear all data and start fresh

🚀 Installation & Run

# Install dependencies
pip install streamlit pandas numpy 

# Run the simulator
streamlit run app.py

Opens at: http://localhost:8501


� Demo

Banker's Algorithm Simulator Demo

Experience the interactive simulation in action — enter your system state and watch the safety algorithm work step-by-step!


�📚 Libraries Used

Library Purpose
streamlit Web UI framework and state management
pandas DataFrame handling and tabular display
numpy Numerical matrix operations

📐 Classic Textbook Example

5 Processes · 3 Resource Types (A, B, C) · Available = [3, 3, 2]

Process Alloc Max Need
P0 [0,1,0] [7,5,3] [7,4,3]
P1 [2,0,0] [3,2,2] [1,2,2]
P2 [3,0,2] [9,0,2] [6,0,0]
P3 [2,1,1] [2,2,2] [0,1,1]
P4 [0,0,2] [4,3,3] [4,3,1]

Safe Sequence: P1 → P3 → P0 → P2 → P4


Deadlock Avoidance Simulation

Operating Systems · 2026 · Vidyalankar Institute of Technology

About

A Deadlock Detection and Avoidance Simulator is a classic Operating Systems project where you simulate how processes and resources interact, and how the system prevents or detects deadlocks.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages