如何检测并找出程序是否陷入死锁?

发布于 2025-01-07 12:25:21 字数 176 浏览 0 评论 0原文

这是一道面试题。

如何检测并找出程序是否陷入死锁?是否有一些工具可用于在 Linux/Unix 系统上执行此操作?

我的想法:

如果一个程序没有任何进展,并且其状态为运行,那么它就是死锁。但是,其他原因也可能导致此问题。开源工具有valgrind(halgrind)可以做到这一点。正确的?

This is an interview question.

How to detect and find out if a program is in deadlock? Are there some tools that can be used to do that on Linux/Unix systems?

My idea:

If a program makes no progress and its status is running, it is deadlock. But, other reasons can also cause this problem. Open source tools are valgrind (halgrind) can do that. Right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请叫√我孤独 2025-01-14 12:25:21

如果您怀疑出现死锁,请执行 ps aux | grep,如果在输出中,PROCESS STATE CODED(不间断睡眠),则表示出现死锁。
因为正如 @daijo 所解释的,假设你有两个线程 T1 & T2 和两个临界区,每个临界区均由信号量 S1 和 S1 保护。 S2 然后,如果 T1 获取 S1 并且 T2 获取 S2,之后它们尝试获取在放弃已持有的锁之前尝试其他锁,这将导致死锁,并且在执行 ps aux | 时会导致死锁。 grep进程状态代码将为D(即不间断睡眠)。

工具:

Valgrind、Lockdep(Linux 内核实用程序)

查看有关死锁类型以及如何避免死锁的链接:
http://cmdlinelinux.blogspot.com /2014/01/linux-kernel-deadlocks-and-how-to-avoid.html

编辑:ps aux 输出D“could”表示进程处于死锁状态,从此

不间断睡眠状态
不间断睡眠状态是一种
这不会立即处理信号。它只会被唤醒
等待的资源变得可用或超时后
发生在该等待期间(如果在进程时指定了超时)
进入睡眠状态)。

If you suspect a deadlock, do a ps aux | grep <exe name>, if in output, the PROCESS STATE CODE is D (Uninterruptible sleep) means it is a deadlock.
Because as @daijo explained, say you have two threads T1 & T2 and two critical sections each protected by semaphores S1 & S2 then if T1 acquires S1 and T2 acquires S2 and after that they try to acquire the other lock before relinquishing the one already held by them, this will lead to a deadlock and on doing a ps aux | grep <exe name>, the process state code will be D (ie Uninterruptible sleep).

Tools:

Valgrind, Lockdep (linux kernel utility)

Check this link on types of deadlocks and how to avoid them :
http://cmdlinelinux.blogspot.com/2014/01/linux-kernel-deadlocks-and-how-to-avoid.html

Edit: ps aux output D "could" mean process is in deadlock, from this redhat doc:

Uninterruptible Sleep State
An Uninterruptible sleep state is one
that won't handle a signal right away. It will wake only as a result
of a waited-upon resource becoming available or after a time-out
occurs during that wait (if the time-out is specified when the process
is put to sleep).

清风不识月 2025-01-14 12:25:21

我建议您查看 Helgrind:线程错误探测器。

此类问题的最简单示例如下。

想象一些共享资源 R,无论出于何种原因,它都由两个锁 L1 和 L2 保护,当访问 R 时,这两个锁都必须被持有。

假设一个线程获取了L1,然后是L2,然后继续访问R。这意味着程序中的所有线程都必须按照先L1,然后L2的顺序获取这两个锁。不这样做可能会陷入僵局。

如果两个线程(称为 T1 和 T2)都想要访问 R,则可能会发生死锁。假设 T1 首先获取 L1,T2 首先获取 L2。然后 T1 尝试获取 L2,T2 尝试获取 L1,但这些锁都已被持有。于是T1和T2陷入僵局。”

I would suggest you look at Helgrind: a thread error detector.

The simplest example of such a problem is as follows.

Imagine some shared resource R, which, for whatever reason, is guarded by two locks, L1 and L2, which must both be held when R is accessed.

Suppose a thread acquires L1, then L2, and proceeds to access R. The implication of this is that all threads in the program must acquire the two locks in the order first L1 then L2. Not doing so risks deadlock.

The deadlock could happen if two threads -- call them T1 and T2 -- both want to access R. Suppose T1 acquires L1 first, and T2 acquires L2 first. Then T1 tries to acquire L2, and T2 tries to acquire L1, but those locks are both already held. So T1 and T2 become deadlocked."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文