Deadlock Detection Using Banker’s Algorithm (Safety Algorithm)
Deadlock Detection Using Banker’s Algorithm (Safety Algorithm)
1. Aim
To detect whether a system is in a deadlocked (unsafe) state by applying the Banker’s Algorithm safety check and to identify a safe sequence if it exists.
2. Objective
-
To apply Banker’s Algorithm to the current system state
-
To determine whether the system is safe or unsafe
-
To identify processes that may be deadlocked
3. Theory
Banker’s Algorithm
Banker’s Algorithm avoids deadlock by ensuring the system always remains in a safe state.
A system is:
-
Safe → if a safe execution sequence exists
-
Unsafe (Deadlock-prone) → if no safe sequence exists
When used for deadlock detection:
-
If the safety algorithm fails → deadlock detected
4. Data Structures Used
| Structure | Description |
|---|---|
Allocation[n][m] | Currently allocated resources |
Max[n][m] | Maximum resource requirement |
Need[n][m] | Remaining resource need |
Available[m] | Currently available resources |
Finish[n] | Process completion status |
5. Given Process Mix
Processes: P0, P1, P2
Resource Types: R0, R1
Allocation Matrix
| Process | R0 | R1 |
|---|---|---|
| P0 | 1 | 0 |
| P1 | 0 | 1 |
| P2 | 1 | 1 |
Maximum Requirement Matrix
| Process | R0 | R1 |
|---|---|---|
| P0 | 1 | 1 |
| P1 | 1 | 1 |
| P2 | 2 | 2 |
Available Resources
6. Need Matrix Calculation
| Process | R0 | R1 |
|---|---|---|
| P0 | 0 | 1 |
| P1 | 1 | 0 |
| P2 | 1 | 1 |
7. Banker’s Safety Algorithm (for Detection)
Steps
-
Initialize
Work = Available -
Set
Finish[i] = false -
Find a process such that
-
If found:
-
Work = Work + Allocation[i] -
Finish[i] = true
-
-
Repeat until no process satisfies the condition
-
If any
Finish[i] == false→ Deadlock detected
8. C Program Implementation (Banker’s Algorithm – Deadlock Detection)
9. Sample Output
10. Observations
-
No process can satisfy
Need ≤ Available -
No safe sequence exists
-
All processes remain unfinished
11. Result
Using Banker’s Algorithm safety check, the system was found to be in an unsafe (deadlocked) state, and the deadlocked processes were successfully identified.
Comments
Post a Comment