A process requests resources; if the resources are not available at that time, the process enters a waiting state. Sometimes, a waiting process is never again able to change state, because the resources it has requested are held by other waiting processes. This situation is called a deadlock.
A process may utilize a resource in following sequence:
1. Request. The process requests the resource. If the request cannot be granted immediately, then the requesting process must wait until it can acquire the resource.
2. Use. The process can operate on the resource.
3. Release. The process releases the resource.
1. Mutual exclusion. At least one resource must be held in a nonsharable mode; that is, only one process at a time can use the resource.
2. Hold and wait. A process must be holding at least one resource and waiting to acquire additional resources that are currently being held by other processes.
3. No preemption. Resources cannot be preempted; that is, a resource can be released only voluntarily by the process holding it, after that process has completed task.
4. Circular wait. A set{P0, P1...Pn} of waiting processes must exist such that P0 is waiting for resource hold by P1, P1 is waiting for...
System resource-allocation graph contains a set of vertices V and a set of edges E. Vertices V has two different types of nodes P = {P1, P2...} means active processes; R = {R1, R2...} means all resource types.
A directed edge Pi -> Rj is a request edge; a direct edge Rj -> Pi is called aassignment edge.
If the graph contains no cycles, then no process in the system is deadlocked. If the graph does contain cycle, then a deadlock may exist.
We can deal with deadlock in three ways:
Deadlock prevention provides a set of methods for ensuring that at least one of the necessary conditions cannot hold.
The mutual-exclusion condition must hold for nonsharable resources.
In general, however, we cannot prevent deadlocks by denying the mutual-exclusion condition, because some resources are intrinsically nonsharable.
To ensure hold-and-wait never occur, we must guarantee that whenever a process requests a resource, it does not hold any other resources.
One protocol can require each process to request and be allocated all its resources before it begins execution.
Another protocol allows a process to request resources only when it has none.
- resource utilization may be low
- starvation is possible
Protocol one: if a process is holding some resources and requests another resource that cannot be immediately allocated to it, then all resources the process is currently holding are preempted.
Protocol two: if a process request some resources, we first check whether they are available. If they are, we allocate them. If they are not, we check whether they are allocated to some other process that is waiting for additional resources. If so, we preempt the desired resources from the waiting process and allocate them to the requesting process.
This protocol is often applied to resources whose state can be easily saved and restored later.
One way to ensure this condition never hold is to impose a total ordering of all resource types and to require that each process requests resources in an increasing order of enumeration.