Linux: Deadlock generation, prevention, avoidance, detection and resolution

1. Deadlock conditions

(1) Definition of deadlock

Multiple processes wait for each other's resources, and they will not release their existing resources until they get all the resources to continue running, which causes a phenomenon of circular waiting , called deadlock.

(2) Four necessary conditions for deadlock to occur

①Resources are mutually exclusive/resources are not shared

Each resource has either been assigned to a process or is available. Only in these two states, resources cannot be shared and used, so the so-called mutual exclusion means : 资源不共享, if used, it can only be used by one process

② Possess and wait/request and hold

Processes that have already obtained resources can continue to request new resources , so I think it 占有并请求may be better to understand

③Resources are inalienable

When a resource is allocated to a process, other processes that need the resource cannot forcefully obtain the resource unless the current occupant of the resource explicitly releases the resource

④Loop waiting

When a deadlock occurs, there must be a loop composed of two or more processes in the system, and each process on the loop is waiting for the resources occupied by the next process.

for example:

Xiaoming has a keyboard, Xiaobai has a mouse, Xiaoming uses a computer to play games, Xiaobai uses a computer to make PPT, Xiaoming can't play games without a mouse, Xiaobai can't make PPT without a keyboard, Xiaoming and other Xiaobai give themselves the mouse, Xiaobai also waited for Xiaoming to give him the keyboard, but Xiaoming was unwilling to give the keyboard to Xiaobai, and Xiaobai was also unwilling to give the mouse to Xiaoming, and Xiaoming and Xiaobai couldn't grab the keyboard and mouse from each other. Lock.
write picture description here

2. Methods to prevent deadlock

(0) Foreword by bloggers

①To prevent the occurrence of deadlock, you only need to destroy one of the four necessary conditions for the occurrence of deadlock.
②The cost of the following method is very large, and no operating system can implement
it at present. ③ Therefore, the method currently used is to avoid deadlock, not to prevent deadlock
. In these multiple-choice questions, it is enough to determine which of the following four methods the option corresponds to.

(1) Destruction of mutually exclusive conditions

method If all system resources are allowed to be shared, the system will not enter a deadlock state.
shortcoming Some resources cannot be accessed at the same time at all, and critical resources such as printers can only be used mutually exclusively. Therefore, it is not feasible to break the mutual exclusion condition to prevent deadlock, and in some cases this mutual exclusion should be protected.

(2) Destroy the request and keep the condition

method The pre-static allocation method is adopted, that is, the process applies for all the resources it needs at one time before running, and does not put it into operation until its resources are not satisfied. Once put into operation, these resources are always owned by it, and no other resource requests are made, so that the system can not be deadlocked.
shortcoming System resources are severely wasted, some of which may be used only at the beginning or near the end of a run, or not at all. And it will also lead to the phenomenon of "starvation". When the individual resource is occupied by other processes for a long time, the process waiting for the resource will not be able to start running.

(3) Destroy the condition of non-deprivation

method When a process that has held some inalienable resources requests new resources and cannot be satisfied, it must release all the resources it has held and reapply when needed later. This means that a resource occupied by a process will be temporarily released, or deprived, or the inalienability condition will be violated.
shortcoming This strategy is complicated to implement. Releasing the acquired resources may cause the failure of the previous stage of work. Repeatedly applying and releasing resources will increase the system overhead and reduce the system throughput. This method is often used for resources whose state is easy to save and restore, such as CPU registers and memory resources, but generally cannot be used for resources such as printers.

(4) Destroy the cyclic wait condition

method In order to destroy the circular waiting condition, the sequential resource allocation method can be used. First, number the resources in the system, and stipulate that each process must request resources in the order of increasing numbers, and the same resources should be applied for at one time. That is to say, as long as a process applies for allocating resource Ri, the process can only apply for resources with a number greater than Ri in subsequent resource applications.
shortcoming The problem with this approach is that the numbering has to be relatively stable, which limits the addition of new types of equipment; although resources are numbered taking into account the order in which most jobs actually use those resources, it often happens that jobs use resources If the order of resources is different from the order specified by the system, resources are wasted; in addition, this method of applying for resources according to the specified order will inevitably bring trouble to the user's programming.

3. ★Algorithms to avoid deadlocks

(1) Judging the "system security state" method

Before performing system resource allocation, calculate the security of this resource allocation . If the allocation does not cause the system to enter an unsafe state, allocate the resource to the process; otherwise, let the process wait.
For 系统安全状态details, see the link:

(2) Banker's algorithm

1. The loan amount applied for cannot exceed the total amount of funds available
to the bank. 2. Withdraw funds from the bank in batches, but the loan amount cannot exceed the total amount applied for at the beginning
. 4. The customer must repay the loan
within the specified time

4. ★Deadlock detection

This part describes how to determine whether a deadlock occurs

(1) The first step: draw a resource allocation diagram

System deadlocks can be described by resource allocation diagrams. As shown in the figure below, a rectangle is used to represent a process, and a box is used to represent a class of resources. Since there may be more than one resource of a type, a dot in the box represents a resource in a class of resources. The directed edge from a process to a resource is called a request edge, which means that the process applies for a unit of this type of resource; the edge from a resource to a process is called an allocation edge, which means that a resource of this type has already been allocated to the process.
write picture description here

(2) Step 2: Simplify the resource allocation diagram

• Step 1: Look at the A resource first. It has three arrows pointing outward, so it allocates a total of 3 resources to the process. At this time, A has no idle resources left.
• Step 2: Look at the B resource again. It has an arrow pointing outward, so it has allocated a total of 1 resource to the process. At this time, B still has an idle resource that has not been allocated.
• Step 3: After reading the resources, then look at the process, first look at the process P2, it only applies for one A resource, but the A resource has been used up at this time, so the process P2 enters the blocking state, therefore, the process P2 temporarily cannot be transformed into isolated point.
• Step 4: Look at the process P1 again. It only applies for one B resource. At this time, the system still has one B resource that has not been allocated. Therefore, the application of P1 can be satisfied. In this way, the process P1 gets all its required resources, so it will not enter the blocking state and can run all the time. After it finishes running, we will release all its resources. Equivalent to: you can remove all the edges of P1 and turn it into an isolated point, as shown in the following figure:
write picture description here
Step 5: After the process P1 finishes running, release the resources occupied by it (2 A resources and 1 B resource) resources), after the system reclaims these resources, the idle resources become 2 A resources and 1 B resource. Since the process P2 has been applying for an A resource, the system can satisfy its application at this time. In this way, process P2 gets all its required resources, so it will not enter the blocking state and can run all the time. After it finishes running, we will release all its resources. It is equivalent to: all the edges of P2 can be removed and turned into an isolated point, which becomes the following figure:
write picture description here
If all the edges in the figure can be eliminated, the figure is said to be completely simplistic , as shown in the above figure.

(3) Step 3: Use deadlock theory to judge

Deadlock principle:
①If there is no loop in the resource allocation diagram, the system has no deadlock;
②If there is a loop in the resource allocation diagram, the system may have a deadlock.
In other words:
if and only if the resource allocation diagram of the S state is not completely simplified, the system state is a deadlock state

5. Removal of deadlock

(1) Resource Deprivation Law

Suspend some deadlocked process and preempt its resources, allocating these resources to other deadlocked processes. However, it should be prevented that the suspended process cannot get resources for a long time and is in a state of resource shortage.

(2) Revocation process method

Forcibly revoke some or even all of the deadlocked processes and deprive them of their resources. The principle of cancellation can be carried out according to the priority of the process and the cost of canceling the process.

(3) Process fallback method

Let one (more) process fall back to the point where it is sufficient to avoid deadlock, and the process rolls back voluntarily to release resources instead of being deprived. The system is required to keep the historical information of the process and set a restore point.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737656&siteId=291194637