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 linux deadlock?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is linux deadlock". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "what is linux deadlock" together!

deadlock concept

Deadlock is a state in which multiple processes (threads) are blocked because they are waiting for resources that have been occupied by other processes. Deadlocks persist when waiting resources are not released. Once a deadlock occurs, the program itself cannot be solved, and can only rely on external forces to restore the program, such as restart, open-door dog reset, etc.

Linux provides deadlock detection mechanisms, which are mainly divided into D-state deadlock and R-state deadlock.

D-state deadlock

The process waits for I/O resources not to be satisfied, and is in TASK_UNINTERRUPTIBLE sleep state for a long time (system default configuration 120 seconds), in which the process does not respond to asynchronous signals (including kill -9). Such as: process and peripheral hardware interaction (such as read), usually use this state to ensure that the process and device interaction process is not interrupted, otherwise the device may be in an uncontrollable state. Linux provides a hung task mechanism for detecting such deadlocks. The trigger for this problem is complex and varied, possibly due to synchronized_irq, mutex lock, insufficient memory, etc. D state deadlock is only a partial multi-process interlock, generally only hang machine, frozen screen, some functions of the machine can not be used, but will not lead to no feeding dog, but was bitten to death by the dog.

R-state deadlock

Process for a long time (system default configuration 60 seconds) in TASK_RUNNING state monopoly CPU without switching, in general, the process off preemption or off interrupt after a long time to execute tasks, endless loop, this often leads to multi-CPU interlock, the whole system can not be scheduled normally, leading to the dog thread can not be executed, can not feed the dog and finally reset the watchdog restart. This problem is mostly caused by improper handling of concurrent operations between CPUs such as atomic operations and spinlock. The Lockdep Deadlock Detection Tools introduced in this article detect the type of deadlock that is R-state deadlock.

Common Mistakes:

AA: Repeated locking ABBA: Once locked using AB sequence, then locked using BA ABBCCA: This type is an extension of ABBA. AB order, AB order, CA order. Such locks are difficult to detect manually. Formation of AB-BA Deadlock with Multiple Unlocks

Suppose there are two code places (such as two functions thread_P and thread_Q of different threads) that want to acquire two locks (lockA and lockB, respectively). If thread_P holds lockA and then acquires lockB, and thread_Q happens to hold lockB and it is also trying to acquire lockA, then it is in a deadlock state. This is the simplest example of deadlock, which is called AB-BA deadlock.

thread_P()

{

......

spin_lock(&lockA);

spin_lock(&lockB);

spin_unlock(&lockA);

spin_unlock(&lockB);

......

}

thread_Q()

{

......

spin_lock(&lockB);

spin_lock(&lockA);

spin_unlock(&lockB);

spin_unlock(&lockA);

......

}

Let's look at the timing of deadlock occurrence in conjunction with the Timeline below:

The X axis represents a Timeline of execution of process P, and the Y axis represents a Timeline of execution of process Q.

This diagram shows six execution routes based on the different concurrent time points of the two processes:

Q acquires B, then acquires A; then releases B, then releases A; at this point P executes, it can acquire all resources Q acquires B, then acquires A; at this point P executes and blocks on requests to A;Q releases B and A, when P resumes execution, it can acquire all resources Q acquires B, then P executes to acquire A; at this point Q blocks on requests to A; P blocks on a request to B, everyone is waiting for each other's resources and deadlocks P to obtain A, and then Q executes to obtain B; in this case P blocks on a request to B;Q blocks on a request to A, everyone is waiting for each other's resources and deadlocks P to obtain A, and then obtains B; in this case Q executes and blocks on a request to B; P releases A and B, when Q resumes execution, it can obtain all resources P obtains A, and then obtains B; then releases A, and then releases B; at this time Q executes, it can obtain all resources Thank you for reading, the above is "linux deadlock is what" content, after the study of this article, I believe we have a deeper understanding of what linux deadlock is, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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

Internet Technology

Wechat

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

12
Report