如何检测并找出程序是否陷入死锁?
这是一道面试题。
如何检测并找出程序是否陷入死锁?是否有一些工具可用于在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您怀疑出现死锁,请执行 ps aux | grep,如果在输出中,,
PROCESS STATE CODE
为D
(不间断睡眠),则表示出现死锁。因为正如 @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, thePROCESS STATE CODE
isD
(Uninterruptible sleep) means it is a deadlock.Because as @daijo explained, say you have two threads
T1
&T2
and two critical sections each protected bysemaphores S1 & S2
then ifT1
acquiresS1
andT2
acquiresS2
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 aps aux | grep <exe name>
, theprocess state code
will beD
(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
outputD
"could" mean process is in deadlock, from this redhat doc:我建议您查看 Helgrind:线程错误探测器。
I would suggest you look at Helgrind: a thread error detector.