Deadlock
Deadlock
Deadlocks
❖Deadlocks
● System Model
● Deadlock Characterization
● Methods for Handling Deadlocks
● Deadlock Prevention
● Deadlock Detection, and Avoidance
● Recovery from Deadlock
System Model
● System consists of resources
● Resource types R1, R2, . . ., Rm
They are CPU cycles, memory space, I/O
Devices
● Each resource type Ri has Wi instances
For i = 1, 2, …, n
● Each process utilizes a resource as follows:
❖ Request
❖ Use
❖ Release
❖ Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual Exclusion
Mutual Exclusion
Not required for sharable resources (e.g., read-only
files);
No Preemption
If a process that is holding some resources
requests another resource that cannot be
immediately allocated to it, then all resources
currently being held are released
Circular Wait
Safe State
● For each Pi, the resources that Pi can still request can
be satisfied by
currently available resources + resources held by all the Pj, with j < i
● If Pi resource needs are not immediately
available, then Pi can wait until all Pj have
finished
Basic Facts
● If a system is in safe state no deadlocks
Resource-Allocation Graph
Unsafe State In Resource-Allocation Graph
Banker’s Algorithm
When a process gets all its resources; it must return them in a finite
amount of time
Data Structures for the Banker’s Algorithm
Let n = number of processes
m = number of resources types
For i,j = 1,2,...,n
Available: Vector of length m. If Available[j] = k, there are k
instances of resource type Rj available
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n - 1
4. If Finish [i] == true for all i, then the system is in a safe state
Resource-Request Algorithm for Process Pi
5 processes P1 – P5;
Need
ABC
P1 7 4 3
P2 1 2 2
P3 6 0 0
P4 0 1 1
P5 4 3 1
Banker’s Algorithm
Available Resources of A, B and C are 3, 3, and 2.
Check if each type of resource request is available for each process.