Deadlock
Deadlock
Mutual exclusion:
Only one process at a time can use a resource.
Hold and wait:
A process holding at least one resource is waiting to acquire
additional resources held by other processes.
No preemption:
A resource can be released only voluntarily by the process holding
it, after that process has completed its task.
Circular wait:
Waiting processes: {P0, P1, …, P0}
P0 is waiting for a resource that is held by P1, P1 is waiting for a
resource that is held by P2,
…, Pn–1 is waiting for a resource that is held by Pn,
P0 is waiting for a resource that is held by P0.
Resource-Allocation Graph
Process
Pi
Pi requests instance of Rj
Rj
Pi
Pi is holding an instance of Rj
Rj
Example of a Resource Allocation
Graph
Resource Allocation Graph With A
Deadlock
Graph With A Cycle But No Deadlock
Basic Facts
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
Preempted resources are added to the list of resources for
which the process is waiting
Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting
Circular Wait – impose a total ordering of all
resource types, and require that each process
requests resources in an increasing order of
enumeration
Deadlock Avoidance
That is:
If Pi resource needs are not immediately available, then Pi can
wait until all Pj have finished
When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate
When Pi terminates, Pi +1 can obtain its needed resources, and
so on
Basic Facts
Multiple instances
Each process must a priori claim maximum use
When a process requests a resource it may have to
wait
When a process gets all its resources it must return
them in a finite amount of time
Data Structures for the Banker’s Algorithm
2. If Finish [i] == true for all i, then the system is in a safe state
Resource-Request Algorithm for
Process Pi
Requesti = request vector for process Pi. If Requesti
[j] = k then process Pi wants k instances of resource
type Rj
1. If Requesti Needi go to step 2. Otherwise, raise error condition,
since process has exceeded its maximum claim
2. If Requesti Available, go to step 3. Otherwise Pi must wait,
since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state
as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If safe the resources are allocated to Pi
If unsafe Pi must wait, and the old resource-allocation state is
restored
Example of Banker’s Algorithm
The system is in a safe state since the sequence < P1, P3,
P4, P2, P0> satisfies safety criteria
Example: P1 Request (1,0,2)
Detection algorithm
Recovery scheme
Single Instance of Each Resource Type
Pi Pj if Pi is waiting for Pj