Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the cause of deadlock in linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "what is the cause of deadlock in linux", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the cause of deadlock in linux".

The causes of deadlock in linux are: 1, competition can not preempt resources and cause deadlock; 2, competition can consume resources and cause deadlock; 3, process advance sequence is improper and lead to deadlock (in the process of running, the process deadlock is caused by the improper order of requesting and releasing resources).

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

I. the concept of deadlock:

If each process (or thread) in a set of processes (or threads) is waiting for an event that can only be raised by other processes (or threads) in that group, then the set of processes (or threads) is Deadlock.

Second, the cause of deadlock:

(1) deadlocks caused by competition that cannot preempt resources

For example, deadlock occurs when files are shared

There are two processes P1 and P2 in the system, both of which are ready to write two files F1 and F2. Both of them are reusable and non-preemptive resources. If the process P1 opens the F1 while the P2 process opens the F2 file, when P1 wants to open F2, it is blocked because the F2 knot is occupied, and when P2 wants to open 1, it is blocked because the F1 knot is occupied, then it will wait wirelessly and form a deadlock.

(2) deadlock caused by competition for consumable resources

For example, a deadlock occurs when a process communicates

There are three processes P1, P2 and P3 in the system. M1, m2, m3 are 3 consumable resources. On the one hand, process P1 generates message M1 and sends it to P2, on the other hand, it receives message m3 from P3. On the one hand, process P2 generates message m2 and sends it to P3, on the other hand, it receives message M1 from P1. Similarly, process P3 generates message m3 on the one hand, sends it to P1, and receives message m2 from P2 on the other.

If all three processes first send messages generated by themselves and then receive messages from others, they can run smoothly without deadlocks, but if all three processes receive messages from others first without generating messages, they will wait forever, resulting in deadlocks.

(3) deadlock is caused by improper process advance sequence: during the process running, the process deadlock is caused by the improper order of request and release of resources.

In the figure above, if you advance in the order of curve 1, the two processes can be completed successfully; if you advance in the order of curve 2, the two processes can be completed smoothly; if you advance in the order of curve 3, the two processes can be completed smoothly. If you advance in the order of curve 4, the two processes will enter the unsafe zone D, while P1 maintains the resource R1, P2 maintains the resource R2, and the system is in an insecure state. If you continue to move forward, a deadlock may occur.

The necessary conditions for the generation of deadlocks

(1) Mutual exclusion condition. The resources requested by a process (thread) can only be occupied by one process (thread) lock for a period of time.

(2) request and maintain conditions. The process (thread) already occupies at least one resource, but a new resource request is made, and the resource is occupied by other processes (threads).

(3) non-preemptive condition (irrevocable condition). Resources acquired by a process (thread) cannot be preempted until they are used up.

(4) Loop waiting condition (loop waiting condition). When a deadlock occurs, there must be a process (thread)-- a circular chain of resources.

IV. Avoidance, prevention and solution of deadlock

Deadlock avoidance is to avoid the final occurrence of deadlock during the operation of the system.

The first three conditions of deadlock are the necessary conditions for deadlock, that is to say, the necessary conditions for deadlock, but not the existence of these three conditions, deadlock can be avoided as long as the fourth condition is avoided logically.

The basic idea of deadlock avoidance: the system dynamically checks every resource request that can be satisfied by the process, and decides whether to allocate resources according to the check results; if the system is likely to have a deadlock after allocation, it will not be allocated; otherwise, it will be allocated.

(a) commonly used methods to avoid deadlocks:

1. Orderly allocation of resources

2. Banker algorithm, basic idea: in the method of avoiding deadlock, the process is allowed to apply for resources dynamically, but the system should calculate the security of this allocation before allocating resources. If the allocation will not cause the system to enter an insecure state, then allocate, otherwise wait.

(2) deadlock prevention:

1. Destroy the "inalienable" condition: when a process occupies a resource and then applies for another resource and cannot meet it, it withdraws from the original resource.

This strategy is complex and costly to implement. Because a resource is forcibly deprived after being used for a period of time, it will result in the invalidation of the previous stage of work.

2. Destroy the "request and hold" condition: adopt a static one-time resource allocation strategy, that is, apply for all resources before the process is run, run if it is satisfied, or wait, so that it will not be occupied and applied.

3. Breaking the "mutual exclusion" condition: this strategy is almost impossible, because the mutual exclusion of resources is determined by their own nature.

4. Destroy the condition of "cycle waiting": all resources in the system are numbered sequentially. The general principle is that the numbers of relatively scarce and scarce resources are larger. When the process applies for resources, it must be carried out strictly according to the order of resource numbers, otherwise the system will not allocate them. In other words, a process can only apply for resources with a higher number when it gets a resource with a lower number, and when releasing resources, it should proceed in descending order.

(C) ways to solve deadlocks:

At present, there are two ways, one is not to let the deadlock occur; the other is to allow the deadlock to occur and then solve it after it occurs.

There are four specific methods:

1. Prevent deadlock. The deadlock can be prevented by setting some strict restrictions on the conditions caused by breaking the deadlock, but this method will lead to low utilization of system resources.

2. Avoid deadlock. In the process of dynamic resource allocation, a certain method is adopted to prevent the system from entering an unsafe state and avoid deadlock. This method can obtain higher resource utilization at the cost of weaker constraints.

3. Detect deadlock. Allow deadlocks to occur during the operation of the system, by setting up a detection mechanism in the system, timely detect whether the deadlock really occurs, and accurately determine the processes and resources related to the deadlock, and then take measures to release the deadlock.

4. Release the deadlock. This is a measure matched with the detection of deadlocks to free the process from the deadlock.

The above is the content of this article on "what is the cause of deadlock in linux". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report