!!!Chapter 7 Deadlock

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.

7.1 System Model

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.

7.2 Deadlock Characterization

7.2.1 Necessary Conditions

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...

7.2.2 Resource-Allocation Graph

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.

!!!Chapter 7 Deadlock_第1张图片

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.

7.3 Methods for Handling Deadlocks

We can deal with deadlock in three ways:

  • User protocol to prevent or avoid deadlocks.
  • We can allow system enter deadlock, detect it, and recover
  • We can ignore the problem, pretend deadlock never occur.  (This is used by most OS)

7.4 Deadlock Prevention

Deadlock prevention provides a set of methods for ensuring that at least one of the necessary conditions cannot hold.

7.4.1 Mutual Exclusion

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.

7.4.2 Hold and Wait          Method 1

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

7.4.3 No Preemption        Method 2

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.

7.4.4 Circular Wait           Method 3

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.

7.5 Deadlock Avoidance

An alternative method for avoiding deadlocks is to require additional information about how resources are to be requested.

The simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.

A deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that a circular-wait condition can never exist.

7.5.1 Safe State

A system is in a safe state only if there exists a safe sequence.

A sequence of processes is a safe sequence for the current allocation state if, for each Pi, the resource requests that Pi can still make can be satisfied by the currently available resources plus the resources held by all Pj, with j < i.


We can define avoidance algorithms that ensure the system will always remain in a safe state.

7.5.2 Resource-Allocation-Graph Algorithm

If the resource-allocation system has only one instance of each resource type, we can use resource-allocation-graph algorithm.

In addition to the request and assignment edges, we add a new type of edge called claim edge. A claim edge Pi ---> Rj indicates that process Pi may request resource Rj at some time in the future.
Suppose process Pi requests resource Rj. The request can be granted only if converting the request edge Pi ---> Rj to an assignment edge Rj ---> Pi does not result in the information of a cycle in the resource-allocation-graph. The cycle-detection algorithm takes n^2, n is the number of processes. 








你可能感兴趣的:(=>!!!Operating,System,Concepts)