Deadlock prevention, detection and lifting

Deadlock prevention, detection and lifting

In the previous article , we define deadlock, harm, resulting in four necessary conditions for deadlock, deadlock and processing four methods to carry out detailed discussion, but because of space problems, did not have time to discuss in detail today, we study in detail how to deal with the deadlock.

This article focuses on the prevention of deadlock, deadlock to lift and how to detect deadlocks; to avoid a deadlock that involves a focus ---- banker algorithm, so there will be discussed in a separate article.

1. Prevention Deadlock

Deadlock prevention is a necessary condition for four or several deadlock by destruction to avoid deadlocks. Since the necessary conditions mutex critical resource, not only can not be changed, should be guaranteed, so that the three main conditions for deadlock damage, i.e., request and retaining , non-preemptive and rotational latency .

But with the development of technology, but also mutually exclusive conditions could be "sabotage", such as Spooling System (spooler), the exclusive equipment modification as a shared device, such as printer sharing system.

1. The destruction of the request and to maintain conditions : In order to keep the damage requests and conditions, OS must undertake: When a process requests a resource, it can not hold resources can not be preempted . There are two ways to achieve agreement: The first protocol: all the processes before starting the run, must apply for a one-time all the resources (And Semaphore) required in its entire run; second protocol: Allow only process after obtaining the resources needed early, you can begin the process during operation in the gradual release has been assigned to all the resources of their own, and has been used up, and then request the new resources required.

The second apparent efficiency of two protocols to be higher, because it provides higher concurrency process, because a small amount of resources can ensure that the process line up and running, this idea a bit coincide idea of ​​virtual memory, this behind us mention.

2. The damage can not seize condition : When one has to keep some resources in the process can not be seized, put forward a new request can not be met, he must release all resources have been maintained until all reapply later.

The methods are complex to implement, and will pay a high price, because a complete unused resources can not be seized, after the release could lead to failure of all the work of the previous stage (within this time off, not to seize resources may be accessed by two processes, data consistency can not be guaranteed, a bit like distributed under data consistency); this strategy needs to lead to repeated application process, the release of resources resulting in the implementation process is indefinitely postponed, which not only extended turnaround time, also increased overhead, reducing system throughput.

3. destruction cycle wait condition : all resources in the system by performing linear ordering, and given a different number. When the number of resources before a new application is greater than the number held resources before they can apply for new resources.

2. Deadlock detection

In some systems, the pursuit of higher concurrency and system resource utilization, which is taken to deadlock "ostrich policy", neither take precautions deadlock, nor with the deadlock avoidance algorithm. In this case, you need to have a system with deadlocks and deadlock detection algorithms lifting algorithm, we can guarantee deadlock occurred in the system will not infinite wait any longer, that is the definition of a deadlock in said external force, can break the deadlock the impasse.

Deadlock detection algorithm implemented, the OS needs to save information about the request and allocation of resources, then the system is determined by whether deadlock detection algorithms.

Figure 2.1 Resource Allocation

We can use to describe the resource allocation map that is a set of N nodes and a set of edges E thereof. Wherein N is composed of two mutually exclusive subsets, each process node is P = {p1, p2, ..., pn}, and resource nodes P = {p1, p2, ..., pn}, N = P∪R. For a set E of e∈E pass, are connected to a node of a node P and R, as is the directed graph, if e = <pi, rj>, is the resource request side, by the process resource rj point pi, pi represents the application process of a unit resource rj; if e = <rj, pi>, then the edge is a resource allocation, the process by the resource pointed pi rj, rj represents a resource has been allocated to the process pi.

A resource allocation map as shown in FIG, P = {p1, p2}, R = {r1, r2}, N = P∪R = {p1, p2} ∪ {r1, r2}, E = {<p1 , r2>, <r2, p2>, <p2, r1>, <r1, p2>, <r1, p1>}.

Grab tickets pictures

Use circle represents a process, with boxes represent a class of resources. Since one type of resource may have multiple, we use a point in the box represents a resource in a resource class, the request side is directed by a process resource block allocated to a starting point side of the block . The figure above, we can see that process p1 has acquired two resources r1, r2 and have to apply for a resource; p2 process or already have a resource r1 and r2 a resource, and then asked r1 resources.

2.2 Deadlock Detection

We can use a simplified method of adding the resource allocation in FIG detecting whether the system is in a deadlock state when this state S. Simplified as follows:

  1. In the resource allocation map, find neither blocking nor a separate process node pi. Because pi process is not blocked, and the possession of the resources (non-independent node), when it successfully completes, you can release all the resources they occupy, which is equivalent to eliminating the side edges and the distribution of pi request to turn it into isolated nodes.
  2. After a process to release resources, access to resources can meet other processes continue to run;
  3. After undergoing a series of simplification, if all sides can be eliminated, that is, resource allocation map can be completely simplified, it does not prove that the current state of deadlock; if not completely simplified, then a deadlock occurred.

This involves a deadlock Theorem: S sufficient conditions for deadlock condition is: When the resource allocation, and only the current state of FIG S is not completely simplified. Personally I feel there should be a necessary and sufficient condition, both should be able to infer from each other, here, I'll open a single blog to discuss the issue. Which big brother if you know the answer, please please answer the next.

Grab tickets pictures

The picture shows a resource allocation diagram a simplified example, as a first process P1 are neither blocked nor separate process nodes, which may be finished running, and releases the resources, which can eliminate the two sides and a resource allocation request resources side. After the release of resources, process P2 can also apply to the resources to run, and ultimately can release resources and they can eliminate side. The final resource allocation for all edges have been simplified drawing, so this state is not deadlock.

The following example is given back to a deadlock occurs, the reader is free to have a look:

Grab tickets pictures

3. deadlock to lift

If the deadlock detection algorithm detects a deadlock, it should take appropriate measures to relieve the deadlock. The method of releasing the deadlock is often used in two ways:

  1. Resource preemption, a sufficient number of preemption of resources from one or more processes assigned to the process deadlock, the deadlock condition to a contact;
  2. Terminating (or revocation) process, the system terminates one or more of the process deadlock, until a large number of loop cycles, the system is out of the deadlock state.

In fact, we use computers in the process, but also encounter deadlock, such as word no response, the computer crashes, a file can not be opened by the process of occupation and so on, in fact, have some relationship and deadlock, most of our solutions also Close all programs, restart or kill the process, and this is why the restart can solve 99% of the "novice" problem, because all the resources are reallocated, all the processes are restarted compete for resources.


Went to the dividing line below concludes this article, this article is to collate all the bloggers themselves and with their own understanding summarize, if there is something wrong, please correct me criticism.

If you are interested, you can also view a few of my other blog, all OS's dry, like it also please Like, comment Artist _ .

Heart practicing martial arts operating system

Published 31 original articles · won praise 232 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_34666857/article/details/104117321