Operating System: Deadlock

The main reference herein "computer operating system (fourth edition)" (Xi'an University of Electronic Science and Technology Publishing House) and Tsinghua operating system open class (to Yong, Yu Chen), finishing the basic concepts of the operating system for their own review inspection.

Deadlock

If each process a group process are waiting only by the event in the group process, other processes can be triggered, then the process is called the group of deadlock.

Causes Deadlock

Causes deadlock can be attributed to the following three categories: competition can not seize resources, competition can consume resources, promote the process of improper order.

Generate the necessary conditions for deadlock

There are four prerequisites deadlock, while unable to meet these four conditions will not deadlock.

  1. Mutually exclusive conditions: i.e. resources to be allocated exclusive;
  2. Request and keeping conditions: if the process holds a resource at the same time we have a new resource request, and the resource has been occupied by another process, this time requesting process is blocked resources it holds and to maintain;
  3. Not seize conditions: the process of obtaining resources can not be preempted until you are finished, our only released when the process is finished using;
  4. Circular wait condition: that there is a process - circular chain resources, form a loop waiting.

Treatment deadlock

Deadlock prevention

4 due to the formation of the deadlock necessary condition, i.e. as long as the damaged one, it will not deadlock. Also, because the mutex is critical to ensure resource access security conditions, it is generally the other three conditions for the prevention of deadlock that damage requests and maintaining conditions, the conditions can not be seized or cycling conditions one can wait.

Avoid deadlock

Also avoid deadlock prevention strategies in advance, but it worked when deployed in a dynamic allocation of resources, and to avoid a deadlock by preventing the system into a state of insecurity. This is prevented in advance in a conventional method of phase, because of the limitations of this method applied is weak, the system performance is better.

Security status

In this method, the so-called unsafe condition that is likely to occur deadlock state. When using this method, whenever the system to allocate resources, this will first computing security resource allocation, if the results indicate the distribution system will enter an unsafe state, so that the process of waiting. The so-called security state, that is, the system can find a safe sequence to promote the process of implementation. Security sequence means: For all current processes on the system, there is some order to promote the process so that the maximum demand for resources each process can be satisfied, each process can be completed successfully final, the order that is to promote safe sequence.

Banker's algorithm

This algorithm is the most representative of the deadlock avoidance algorithm proposed by Dijkstra. Bankers algorithm requires each new process are the maximum number of resources needed to run must be declared when entering the system, and can not exceed the total amount of the corresponding system has also required system maintenance four data structures:

  1. A m-dimensional vector of the Available, \ (the Available [j] = K \) represents the current number of the j-th class of available system resources is K, the number of total resources available is clearly the initial value of the vector system;
  2. A \ (n \ times m \) matrix Max, \ (Max [i] [j] = K \) represents the maximum number i of the j-th class of the resource needs of the process K;
  3. A \ (n \ times m \) matrix Allocation, \ (Allocation [i] [j] = K \) represents the number of process i currently share resource class j is K;
  4. A \ (n \ times m \) matrix Need, \ (Need [i] [j] = K \) represents the j-th process class i resource needed to complete the task of K.

This matrix has three apparent to: \ (Max [I] [J] = Need [I] [J] + Allocation [I] [J] \) .

With these data structures can be checked for safety. For the arrival of the process \ (P_i \) request vector, set up its resources for the \ (Request - i \) , \ (Request - i [j] = K \) indicates that the demand for process j-class resource for K, followed by started checking:

  1. If \ (Request - i [J] \ Le Need [i] [J] \) , then the next step, otherwise considered an error, because it represents the number of resources it requests is greater than the number of resources it needs;

  2. If \ (Request - i [J] \ Le the Available [J] \) , then the next step, otherwise wait for the process to make, because the resources are insufficient to ensure the operation of the process;

  3. Modify the value of a data structure of the system (i.e., analog resource allocation): \
    [the Available [J] = the Available [J] -Request_i [J] \\ Allocation [I] [J] = Allocation [I] [J] + Request - i [J ] \\ Need [i] [j ] = Need [i] [j] -Request_i [j] \]

  4. The system performs a security algorithm to determine whether the system after resource allocation is still safe state, if the formal allocation of resources, or analog distribution set aside, for the process to wait.

Security algorithms require two auxiliary data structures:

  1. Vector Work, represents the number of the various resources of the system can be supplied, the initial state: \ (= the Available Work \) ;
  2. Finish vector, indicates whether the system has sufficient resources are allocated to make the process completed, initial state: for all I, \ (Finish [I] to false = \) .

The dimensions of the two vectors are consistent with Vector Available. Each execution will find a satisfy the following conditions of the process from the process set: \ (Finish [I] = to false \) , \ (Need [I] [J] \ Le Work [J] \) , found after the process execution: \ (Work [J] = Work [J] + Allocation [i] [J] \) and \ (Finish [i] = to true \) , when the process until the condition is not satisfied, checking whether Finish all processes it is true, if you believe the system remains secure, otherwise it is unsafe.

Detection, release deadlock

When the system is no time for preventive measures deadlock, the system will need to be equipped to detect deadlock and lifted algorithms.

Resource allocation map

To monitor deadlock, the system needs to save information request and allocation of resources, and provides a detection algorithm. Resource allocation map can help us understand the deadlock detection mechanism. FIG there is a resource allocation, a resource unit assigned by the edges represent the resource to the process diagram representative of the process or the resource node, whereas the release of a unit representative of resources.

Resource Allocation Graph

Is a simple resource allocation diagram, a circle represents the process node, the number of circles represent resource node represents a resource block, the frame of FIG. We know that with the advance of the process, the process is completed, that is, the application process resources will eventually be released, which will provide a basis for a simplified diagram of resource allocation. Find all neither blocking nor separate process nodes, and simultaneously remove all allocation requests and side edges (even if the isolated) attached to it. If all processes can be executed smoothly, then all edges in the graph should be eliminated, that is the ultimate resource allocation map can be completely reduced; conversely, if the system is in a deadlock, resource allocation map then the system is not completely simplified.

Deadlock Theorem

Since the former has been demonstrated: a different order to simplify the simplified diagram does not affect the finally obtained, which can be given is determined whether a deadlock theorems: sufficient condition is in a deadlock state if and only if system resources FIG allocation can not be completely reduced.

Lift the deadlock

Upon detection of the deadlock, the deadlock condition can generally be broken up in two ways:

  1. Seize resources, namely plundering resources from other processes to deadlock the process;
  2. To terminate the process, the system terminates several processes to break the deadlock loop.

Guess you like

Origin www.cnblogs.com/Li-F/p/11944370.html