如何在 UNIX 上实际查找进程死锁原因?

发布于 2024-12-12 10:47:45 字数 369 浏览 0 评论 0原文

在unix上,一个进程卡住了,您怀疑可能是死锁,找出死锁的原因以及如何消除和避免它?

我知道死锁的4个条件:

  1. 互斥:一次最多可以将一种资源分配给一个进程(不共享)。

  2. 保持并等待:允许持有资源的处理请求另一个资源。

  3. 无抢占:进程必须释放其资源;它们不能被夺走。

  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:

  1. Mutual exclusion: A resource can be assigned to at most one process at a time (no sharing).

  2. Hold and wait: A processing holding a resource is permitted to request another.

  3. No preemption: A process must release its resources; they cannot be taken away.

  4. 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 技术交流群。

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

发布评论

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

评论(2

明媚殇 2024-12-19 10:47:45

还有 Valgrind 的 Helgrind 工具:Helgrind:线程错误检测器< /强>

Helgrind 是一个 Valgrind 工具,用于检测使用 POSIX pthreads 线程原语的 C、C++ 和 Fortran 程序中的同步错误。

POSIX pthreads 中的主要抽象是:一组共享公共地址空间的线程、线程创建、线程加入、线程退出、互斥体(锁)、条件变量(线程间事件通知)、读写锁、自旋锁、信号量和屏障。

Helgrind 可以检测三类错误,这些错误将在接下来的三节中详细讨论:

  1. 滥用 POSIX pthreads API< /a>.


  2. 锁排序问题引起的潜在死锁.

  3. 数据争用 - 无需访问内存足够的锁定或同步

此类问题通常会导致不可重现的、与时间相关的崩溃、死锁和其他不当行为,并且很难通过其他方式找到。

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

Helgrind is a Valgrind tool for detecting synchronisation errors in C, C++ and Fortran programs that use the POSIX pthreads threading primitives.

The main abstractions in POSIX pthreads are: a set of threads sharing a common address space, thread creation, thread joining, thread exit, mutexes (locks), condition variables (inter-thread event notifications), reader-writer locks, spinlocks, semaphores and barriers.

Helgrind can detect three classes of errors, which are discussed in detail in the next three sections:

  1. Misuses of the POSIX pthreads API.

  2. Potential deadlocks arising from lock ordering problems.

  3. Data races -- accessing memory without adequate locking or synchronisation.

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.

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