如何在 UNIX 上实际查找进程死锁原因?
在unix上,一个进程卡住了,您怀疑可能是死锁,找出死锁的原因以及如何消除和避免它?
我知道死锁的4个条件:
互斥:一次最多可以将一种资源分配给一个进程(不共享)。
保持并等待:允许持有资源的处理请求另一个资源。
无抢占:进程必须释放其资源;它们不能被夺走。
循环等待:必须存在一个进程链,使得链中的每个成员都在等待链中下一个成员所持有的资源。
但是,它们是理论上的,如何在 UNIX 上实际确定死锁? 只看到一个进程不取得进展? 如何查找哪部分代码导致了死锁以及原因? 如果允许使用工具,可以使用什么?
谢谢
On unix, a process is stuck, you doubt that it may be deadlock, find the reasons for deadlocking and how to remove and avoid it ?
I know the 4 conditions for deadlock:
Mutual exclusion: A resource can be assigned to at most one process at a time (no sharing).
Hold and wait: A processing holding a resource is permitted to request another.
No preemption: A process must release its resources; they cannot be taken away.
Circular wait: There must be a chain of processes such that each member of the chain is waiting for a resource held by the next member of the chain.
But, they are theoretical, how to determine a deadlock on unix practically ?
Only by seeing that a process does not make progress ?
How to find which part of the code cause the deadlock and the reasons ?
If you are allowed to use tools, what can be used ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用调试器。
调试死锁的快速提示
或这个简单的:
如何使用 gdb 查找死锁场景
Use debugger.
Quick tip for debugging deadlocks
or this simple one:
how to find deadlock scenario using gdb
还有 Valgrind 的 Helgrind 工具:Helgrind:线程错误检测器< /强>
此类问题通常会导致不可重现的、与时间相关的崩溃、死锁和其他不当行为,并且很难通过其他方式找到。
Helgrind 知道所有 pthread 抽象并尽可能准确地跟踪它们的效果。在 x86 和 amd64 平台上,它理解并部分处理因使用 LOCK 指令前缀而产生的隐式锁定。
当您的应用程序仅使用 POSIX pthreads API 时,Helgrind 效果最佳。但是,如果您想使用自定义线程原语,您可以使用 helgrind.h 中定义的 ANNOTATE_* 宏向 Helgrind 描述它们的行为。此功能是在 Valgrind 3.5.0 版本中添加的,并且被认为是实验性的。
There is also Valgrind's Helgrind tool: Helgrind: a thread error detector
Problems like these often result in unreproducible, timing-dependent crashes, deadlocks and other misbehaviour, and can be difficult to find by other means.
Helgrind is aware of all the pthread abstractions and tracks their effects as accurately as it can. On x86 and amd64 platforms, it understands and partially handles implicit locking arising from the use of the LOCK instruction prefix.
Helgrind works best when your application uses only the POSIX pthreads API. However, if you want to use custom threading primitives, you can describe their behaviour to Helgrind using the ANNOTATE_* macros defined in helgrind.h. This functionality was added in release 3.5.0 of Valgrind, and is considered experimental.