The deadlock problem
Deadlocks
Prevenzione statica: preemption
Prevenzione dinamica: deadlock avoidance
A deadlock is a set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.
An arrow from to means the process called a wait(A).
An arrow from to means the process has control of the resource .
If we map like this and we get a cycle, this means we currently have a deadlock.
System Model
A system has resource types
These can be CPU cycles, memory space, I/O devices..
Each resource type has instances.
Each process utilizes a resource as follows:
- request
- use
- release
Deadlock Characterization
A deadlock can occur if these four conditions hold simultaneously:
- mutual exclusion
- hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes
- no preemption
- circular wait (cycle)
Resource-Allocation Graph
An arrow from to means the process called a wait(A).
An arrow from to means the process has control of the resource .
Basic Facts
- If a graph contains no cycles no deadlock.
- If a graph contains a cycle:
- if only there is one instance per resource type deadlock
- if several instances per resource type possibility of deadlock
Methods for handling Deadlocks
Methods can be:
- ensuring that the system will never enter a deadlock state
- allow the system to ender a deadlock and then recover
- ignore the problem and pretend that deadlock never occur in the system: this is used by most operating systems, including Unix
Deadlock Prevention
Prevention is done by restraining the ways requests can be made:
- mutual exclusion: this is not required for sharable resources, but must hold for nonsharable resources
- hold and wait: must guarantee that whenever a process request a resource it does not hold any other resource
- require processes to request and get allocated all the resources it will need, before it begins execution, or only allow processes to request resources when they have none
- low resource utilization: starvation possible
- no preemption:
- if a process that is holding some resources request another resource that cannot be immediately allocated to it, then all the resource currently being held are released
- preempted resources are added to the list of resource for which the process is waiting
- the 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 reqeust resources in an increasing order of enumeration: “if already holds , it cannot request “
Deadlock Avoidance
Requires that the system has some additional a priori information available:
- the simplest and most useful model requires that each process declares the maximum number of resources of each type that it may need
- this avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition
- the resource-allocation state is defined by:
- the number of available allocated resources
- the maximum demands of the processes
Safe State
The System is in a safe state if there exists a sequence of ALL processes in the systems such that for each process, the resource that it can still request can be satisfied by the currently available resource or the resources held by all processes scheduled before it.
When a process requests an available resource, the system must decide if immediate allocation leaves the system in a safe state.
So if a processe’s resource needs cannot be immediately met, this process waits for all the processes before it that hold resources it would need. When these processes finish, their resources get returned to the system and the process that was waiting can be executed.
The deadlock state is a subset of the unsafe states set.
Basic Facts
- If a system is in safe state no deadlocks
- if a system is in unsafe state possibility of deadlocks
- avoidance ensuring that the system will never enter an unsafe state
Avoidance Algorithms
Single instance of a resource type: use a resource-allocation graph.
Multiple instances of a resource type: use the banker’s algorithm.
Resource-Allocation
This algorithm is used when working with resource types with a single instance.
Graph Scheme
- Claim edge indicates that process may request resource ; represented by a dashed line
- Claim edge converts to request edge when a process requests a resource
- Request edge converted to an assignment edge when the is allocated to the process resource
- When a resource is released by a process, assignment edge reconverts to a claim edge
- Resources must be claimed a priori in the system
If a process has an outgoing request edge, it is bloccato.
Algorithm
Suppose that process requests a resource : the request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph.
Banker’s Algorithm
This algorithm is used when having multiple instances of a resource type.
- Each process must a priori claim its maximum resource use
- when a process request 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
Let n = number of processes and m = number of resources type.
- Available: Vector of length
m. IfAvailable[j] = k, there are
instances of resource type available - Max: matrix. If
Max[i,j] = k, then process may request at
most instances of resource type - Allocation: matrix. If
Allocation[i,j] = kthen is currently
allocated instances of - Need: matrix. If
Need[i,j] = k, then may need more
instances of to complete its task
we have:
Safety Algorithm
- Let
WorkandFinishbe vectors of length and , respectively. Initialize:Work = AvailableFinish[i] = false for i = 0, 1, …, n- 1
- Find an such that both:
- (a)
- (b)
If no such exists, go to step4
go to step2- If , then the system is in a safe state
Resource-Request Algorithm for Process
Request = request vector for process Pi.
If then process wants instances of
resource type
- If go to step
2.
Otherwise, raise error condition, since process has exceeded its maximum claim - If , go to step
3. Otherwise must wait, since resources are not available - Assume to allocate requested resources to by modifying the state as follows:
- ;
- ;
- ;- If safe the resources are allocated to
- If unsafe must wait, and the old resource-allocation state is restored
Deadlock Detection
This strategy allows the system to enter deadlock state, detects it and tries to recover from it.
Single instance of each resource type
The system maintains a wait-for graph:
- nodes are processes
- if is waiting for
The system periodically invokes an algorithm that detects cycles in the wait-for graph, if one is found, a deadlock is detected.
Note that an algorithm of cycle detection on a graph of vertices (nodes) requires an order of operations.
Multiple instances of a resource type
We keep:
available: a vector of length that indicates the number of available resources of each typeallocation: a matrix that defines the number of resources of each type currently allocated to each processrequest: a matrix that indicates the current requests of each process.
If , the process is requesting more instances of resource type
Detection algorithm
This algorithm requires an order of operations to detect a deadlock state.
- Let
WorkandFinishbe vectors of length and , respectively initialize:Work = Available- For
i = 1,2, …, n, if , thenFinish[i] = false; otherwise,Finish[i] = true
- Find an index such that both:
Finish[i] == false
If no such exists, go to step4
-
Finish[i] = true- go to step
2
- If
Finish[i] == false, for some , , then the system is in deadlock state.
Moreover, ifFinish[i] == false, then is deadlocked
When to invoke the detection algorithm
When and how often to invoke this algorithm depends on:
- how often can a deadlock occur
- how many processes will need to be rolled back for recovery: one for each disjoint cycle
Possibilities:
- every time a request cannot be satisfied
- every hour
- when CPU usage goes below 40%
Recovery from Deadlock
Possibilities of recovery are:
- abort all deadlocked processes
- abort one process at a time until the deadlock cycle is eliminated
Order of abortion of process:
- priority of processes
- how long processes have computed, and how much time is needed until completion
- number of resources processes have used
- number of resources processes need until completion
- how many processes will need to be aborted
- wether a process is interactive or batch
Resource Preemption
Instead of aborting processes we can select a “victim” process and roll it back to a safe state.
Starvation: there is the possibility of always the same processes being picked as victim. We solve this by including the number of rollbacks executed on every process when computing the cost of rolling back.