检测模型
死锁的发生, 必然意味着有向图(依赖关系)的构建存在环.
一言以蔽之: 死锁的发生, 必然意味着有向图(依赖关系)的构建存在环. 关于检测模型, 我们可以这么假定, 锁为有向边, 申请锁的线程A为起点, 拥有锁的线程B为终点. 这样就形成线程A到线程B的一条有向边. 而众多的锁(边)和线程(点), 就构成了一个有向图. 于是乎, 一个死锁检测的算法, 就转变为图论中有向图的环判断问题. 而该问题, 可以借助成熟的拓扑遍历算法轻易实现.
死锁发生条件:
可证明结论:
死锁检测:
系统资源分配图(system resource-allocation graph) 二元组G=(V,E)
P={P1,P2,...,Pn},含有系统中全部的进程
R={R1,R2,...,Rm},含有系统中全部的资源
Daily Challenge 207. 课程表 (c++)
示例代码
https://ivanzz1001.github.io/records/post/cplusplus/2018/11/15/linux-deadlock-detect#4-%E4%BD%BF%E7%94%A8pstack%E5%92%8Cgdb%E5%B7%A5%E5%85%B7%E5%AF%B9%E6%AD%BB%E9%94%81%E7%A8%8B%E5%BA%8F%E8%BF%9B%E8%A1%8C%E5%88%86%E6%9E%90
[1] 死锁问题分析的利器:Helgrind https://www.valgrind.org/docs/manual/hg-manual.html
[2] MySQL 死锁检测源码分析https://leviathan.vip/2020/02/02/mysql-deadlock-check/
https://dev.mysql.com/doc/refman/8.0/en/innodb-deadlock-example.html
[3] How to detect and find out a program is in deadlock?
[4] https://www.valgrind.org/docs/manual/hg-manual.html 官方手册 必须看
In this section, and in general, to "acquire" a lock simply means to lock that lock, and to "release" a lock means to unlock it.
Helgrind monitors the order in which threads acquire locks. This allows it to detect potential deadlocks which could arise from the formation of cycles of locks. Detecting such inconsistencies is useful because, whilst actual deadlocks are fairly obvious, potential deadlocks may never be discovered during testing and could later lead to hard-to-diagnose in-service failures.
The simplest example of such a problem is as follows.