Professional Documents
Culture Documents
The Deadlock Problem: Lock (A) Lock (B) Lock (B) Lock (A)
The Deadlock Problem: Lock (A) Lock (B) Lock (B) Lock (A)
Spring 2001
P0 P1
lock (A); lock (B)
lock (B); lock (A)
n Hold and wait: a process holding at least one resource is waiting n R = {R1, R2, …, Rm }, the set consisting of all resource
to acquire additional resources held by other processes. types in the system.
n Circular wait: there exists a set {P0, P1, …, Pn} of waiting n assignment edge – directed edge Rj → Pi
processes such that Pi is waiting for a resource that is held by Pi+1,
Pn is waiting for a resource that is held by P0.
1
Resource Allocation Graph Deadlocked Resource Allocation
n Process
n Pi requests instance of Rj
Pi
Rj
n Pi is holding an instance of Rj
Pi
Rj
2
Deadlock avoidance Safe state
n When a process requests an available resource, system must decide if
Requires that the system has some additional a priori information
immediate allocation leaves the system in a safe state.
available.
Resource-allocation graph
Deadlock avoidance algorithm
n If a system is in safe state ⇒ no deadlocks. n Claim edge Pi → Rj indicated that process Pi may request
resource Rj ; represented by a dashed line.
n If a system is in unsafe state ⇒ possibility of deadlock.
n Claim edge converts to request edge when a process
n Avoidance ⇒ ensure that a system will never enter an requests a resource.
unsafe state.
n When a resource is released by a process, assignment
edge reconverts to a claim edge.
3
Data Structures for Banker’s Algo Banker’s algorithm: safety test
Let n = number of processes, and m = number of resources types.
1. Let Work and Finish be vectors of length m and n, respectively.
n Available: Vector of length m. If available [j] = k, there are k Initialize:
instances of resource type Rj available. Work := Available
Finish [i] = false for i =1,2, …, n.
n Max: n x m matrix. If Max [i,j] = k, then process Pi may
request at most k instances of resource type Rj.
2. Find an i such that both:
n Allocation: n x m matrix. If Allocation[i,j] = k then Pi is (a) Finish [i] = false
currently allocated k instances of Rj. (b) Needi ≤ Work
If no such i exists, go to step 4.
n Need: n x m matrix. If Need[i,j] = k, then Pi may need k
more instances of Rj to complete its task.
3. Work := Work + Allocationi
Finish[i] := true
Need [i,j] = Max[i,j] – Allocation [i,j]. go to step 2.
4. If Finish [i] = true for all i, then the system is in a safe state.
resources are not available. n periodically searches for a cycle in the graph; when and how often
3. Pretend to allocate requested resources to Pi by modifying the state depends on:
as follows: n How often a deadlock is likely to occur?
Available := Available - Requesti; n How many processes will need to be rolled back