This project demonstrates how deadlocks occur, how to detect them using graph-based methods, and how to resolve them using the Banker's Algorithm. The project is divided into three phases, each focusing on a specific aspect of deadlock management.
The Resource class represents a system resource and provides methods to allocate and release resource units safely.
Key Features:
- Initialization: Each resource has a
name,total_units(total available), andallocated_units(initially set to 0). - Allocation: The
allocatemethod ensures resources are only allocated if sufficient units are available. - Release: The
releasemethod frees up allocated units, maintaining the integrity of resource usage.
The simulation creates processes and resources to model a deadlock scenario.
Key Components:
- Process Class: Represents system processes, with methods to request and release resources.
- Deadlock Scenario:
- Resources
R1andR2are created with one unit each. - Processes
P1,P2, andP3attempt to allocate resources in a manner that leads to a deadlock:P1holdsR1and waits forR2.P2holdsR2and waits forR1.P3waits for bothR1andR2.
- Resources
Output: A clear demonstration of how deadlocks occur in a resource-constrained environment.
In this phase, a graph structure is implemented to represent processes, resources, and their interactions. Two approaches are provided:
A custom Graph class is used to simulate the resource allocation graph and detect deadlocks using Depth-First Search (DFS).
Features:
- Nodes: Represent processes and resources.
- Edges:
- Allocation edges (
P → R): A process holds a resource. - Request edges (
R → P): A process requests a resource.
- Allocation edges (
- Deadlock Detection: Cycles in the graph indicate deadlocks.
Example:
- Add nodes for processes (
P1, P2, P3) and resources (R1, R2). - Add edges to simulate resource allocation and requests.
- Detect deadlocks using the
detect_deadlockmethod.
This approach visualizes the graph and identifies deadlocks more efficiently using the networkx library.
Visualization:
- Nodes:
- Processes (gray rectangles).
- Resources (blue circles).
- Edges:
- Requests (red dashed lines).
- Allocations (green solid lines).
- Graph Drawing: Displayed using
matplotlibwith labeled edges and nodes.
Example:
- Create a directed graph (
DiGraph). - Add nodes and edges for processes and resources.
- Visualize the graph and detect deadlocks using
networkx's cycle detection.
The Banker's Algorithm is used to prevent deadlocks by ensuring the system remains in a safe state during resource allocation.
Attributes:
total_resources: Total available resources.max_demand: Maximum resource demand per process.allocation: Current resource allocation per process.need: Remaining resource needs.available: Currently available resources.
Key Methods:
-
is_safe:
- Determines if the system is in a safe state by simulating process completion.
- Returns
Trueand a safe sequence if found, otherwiseFalse.
-
request_resources:
- Checks if a process's resource request can be safely granted.
- Allocates resources if the system remains in a safe state, otherwise rolls back changes.
Example Usage:
- Initialize
BankersAlgorithmwith system resource data. - Request resources for a process and check if the request is granted.
- Output the safe sequence if the system remains safe.
To run this project, ensure the following libraries are installed:
pip install networkx matplotlibInput:
- Processes:
P1, P2, P3. - Resources:
R1, R2.
Output:
P1andP2hold resources and blockP3. Deadlock occurs.
Input:
- Graph edges representing requests and allocations.
Output:
- Deadlock detected. Graph visualization displayed.
Input:
- Resource allocation and maximum demands.
Output:
- Safe sequence found or request denied with explanation.