Update 计算机操作系统 - 死锁.md

重新绘制配图,补充程序中的死锁示例。
This commit is contained in:
CyC2018
2026-10-26 22:04:14 +08:00
parent 0a3fae9f5b
commit a16b1715a8
13 changed files with 615 additions and 14 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 KiB

+94 -14
View File
@@ -17,12 +17,16 @@
* [1. 安全状态](#1-安全状态)
* [2. 单个资源的银行家算法](#2-单个资源的银行家算法)
* [3. 多个资源的银行家算法](#3-多个资源的银行家算法)
* [程序中的死锁](#程序中的死锁)
* [1. Java 线程死锁](#1-java-线程死锁)
* [2. MySQL 死锁](#2-mysql-死锁)
* [参考资料](#参考资料)
<!-- GFM-TOC -->
## 必要条件
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/c037c901-7eae-4e31-a1e4-9d41329e5c3e.png"/> </div><br>
<div align="center"> <img src="pics/deadlock-transfer.png" width="740px"/> </div><br>
- 互斥:每个资源要么已经分配给了一个进程,要么就是可用的。
- 占有和等待:已经得到了某个资源的进程可以再请求新的资源。
@@ -54,26 +58,26 @@
### 1. 每种类型一个资源的死锁检测
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/b1fa0453-a4b0-4eae-a352-48acca8fff74.png"/> </div><br>
<div align="center"> <img src="pics/deadlock-detect-graph.png" width="720px"/> </div><br>
上图为资源分配图,其中方框表示资源,圆圈表示进程。资源指向进程表示该资源已经分配给该进程,进程指向资源表示进程请求获取该资源。
图 a 可以抽取出环,如图 b,它满足了环路等待条件,因此会发生死锁。
图中 P1、P2、P3 和 R1、R2、R3 构成一个环,满足环路等待条件,因此发生了死锁。
每种类型一个资源的死锁检测算法是通过检测有向图是否存在环来实现,从一个节点出发进行深度优先搜索,对访问过的节点进行标记,如果访问了已经标记的节点,就表示有向图存在环,也就是检测到死锁的发生。
### 2. 每种类型多个资源的死锁检测
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/e1eda3d5-5ec8-4708-8e25-1a04c5e11f48.png"/> </div><br>
<div align="center"> <img src="pics/deadlock-detect-matrix.png" width="760px"/> </div><br>
上图中,有三个进程四个资源,每个数据代表的含义如下:
上图中,有三个进程三种资源,每个数据代表的含义如下:
- E 向量:资源总量
- A 向量:资源剩余量
- C 矩阵:每个进程所拥有的资源数量,每一行都代表一个进程拥有资源的数量
- R 矩阵:每个进程请求的资源数量
进程 P<sub>1</sub> 和 P<sub>2</sub> 所请求的资源都得不到满足,只有进程 P<sub>3</sub> 可以,让 P<sub>3</sub> 执行,之后释放 P<sub>3</sub> 拥有的资源,此时 A = (2 2 2 0)。P<sub>2</sub> 可以执行,执行后释放 P<sub>2</sub> 拥有的资源,A = (4 2 2 1) 。P<sub>1</sub> 也可以执行。所有进程都可以顺利执行,没有死锁。
进程 P<sub>1</sub> 和 P<sub>3</sub> 所请求的资源都得不到满足,只有进程 P<sub>2</sub> 可以,让 P<sub>2</sub> 执行,之后释放 P<sub>2</sub> 拥有的资源,此时 A = (3 1 2)。P<sub>1</sub> 可以执行,执行后释放 P<sub>1</sub> 拥有的资源,A = (4 2 2)。P<sub>3</sub> 也可以执行。所有进程都可以顺利执行,没有死锁。
算法总结如下:
@@ -115,9 +119,9 @@
### 1. 安全状态
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/ed523051-608f-4c3f-b343-383e2d194470.png"/> </div><br>
<div align="center"> <img src="pics/deadlock-safe-state.png" width="760px"/> </div><br>
图 a 的第二列 Has 表示已拥有的资源数,第三列 Max 表示总共需要的资源数,Free 表示还有可以使用的资源数。从图 a 开始出发,先让 B 拥有所需的所有资源(图 b),运行结束后释放 B,此时 Free 变为 5(图 c);接着以同样的方式运行 C 和 A,使得所有进程都能成功运行,因此可以称图 a 所示的状态时安全的。
图中条形的长度表示进程总共需要的资源数(Max),浅蓝部分表示已拥有的资源数(Has),Free 表示还可以使用的资源数。从图 a 开始出发,先让 B 拥有所需的所有资源,运行结束后释放 B,此时 Free 变为 6(图 b);接着以同样的方式运行 A 和 C,使得所有进程都能成功运行,因此可以称图 a 所示的状态是安全的。
定义:如果没有死锁发生,并且即使所有进程突然请求对资源的最大需求,也仍然存在某种调度次序能够使得每一个进程运行完毕,则称该状态是安全的。
@@ -127,20 +131,96 @@
一个小城镇的银行家,他向一群客户分别承诺了一定的贷款额度,算法要做的是判断对请求的满足是否会进入不安全状态,如果是,就拒绝请求;否则予以分配。
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/d160ec2e-cfe2-4640-bda7-62f53e58b8c0.png"/> </div><br>
<div align="center"> <img src="pics/deadlock-banker-single.png" width="760px"/> </div><br>
上图 c 为不安全状态,因此算法会拒绝之前的请求,从而避免进入图 c 中的状态。
上图 b 为不安全状态,因此算法会拒绝 C 的这次申请,从而避免进入图 b 中的状态;图 c 仍然是安全的,可以分配。
### 3. 多个资源的银行家算法
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/62e0dd4f-44c3-43ee-bb6e-fedb9e068519.png"/> </div><br>
<div align="center"> <img src="pics/deadlock-banker-multi.png" width="760px"/> </div><br>
上图中有五个进程,四个资源。左边的图表示已经分配的资源,右边的图表示还需要分配的资源。最右边的 E、P 以及 A 分别表示:总资源、已分配资源以及可用资源,注意这三个为向量,而不是具体数值,例如 A=(1020),表示 4 个资源分别还剩下 1/0/2/0。
上图中有四个进程,三种资源。Allocation 表示已经分配的资源,Need 表示还需要分配的资源。E 和 Available 分别表示总资源和可用资源,它们都是向量,而不是具体数值,例如 Available = (1 1 2) 表示 3 种资源分别还剩下 1、1、2 个。
检查一个状态是否安全的算法如下:
- 查找右边的矩阵是否存在一行小于等于向量 A。如果不存在这样的行,那么系统将会发生死锁,状态是不安全的。
- 假若找到这样一行,将该进程标记为终止,并将其已分配资源加到 A 中。
- 查找 Need 矩阵中是否存在一行小于等于向量 Available。如果不存在这样的行,那么系统将会发生死锁,状态是不安全的。
- 假若找到这样一行,将该进程标记为终止,并将其已分配资源加到 Available 中。
- 重复以上两步,直到所有进程都标记为终止,则状态是安全的。
如果一个状态不是安全的,需要拒绝进入这个状态。
## 程序中的死锁
### 1. Java 线程死锁
两个线程反向转账时,T1 锁住 alice 再请求 bob,T2 锁住 bob 再请求 alice,就是开头图中的情况:
```java
static void transfer(Account from, Account to, int amount) {
synchronized (from) {
synchronized (to) {
from.balance -= amount;
to.balance += amount;
}
}
}
```
用 jstack 查看进程的线程栈,末尾会直接报告死锁(省略了部分内容):
```text
Found one Java-level deadlock:
=============================
"T1":
waiting to lock monitor 0x0000000135f26a70 (object 0x000000061fc14e18, a Transfer$Account),
which is held by "T2"
"T2":
waiting to lock monitor 0x0000000125f06e60 (object 0x000000061fc14dd0, a Transfer$Account),
which is held by "T1"
```
解决方法是破坏环路等待:所有线程按同一个顺序加锁,例如总是先锁 id 小的账户。
```java
Account first = from.id < to.id ? from : to;
Account second = from.id < to.id ? to : from;
synchronized (first) {
synchronized (second) {
from.balance -= amount;
to.balance += amount;
}
}
```
两个线程各反向转账 10 万次,原来的写法每次运行都会卡住,按 id 顺序加锁后都能完成,两个账户的余额仍为 1000。使用 ReentrantLock 时还可以用 tryLock 设置超时,拿不到锁就释放已持有的锁再重试,破坏的是占有和等待条件。
### 2. MySQL 死锁
事务按不同的顺序更新同一组行时也会死锁。例如两个事务同时反向转账,事务 1 先更新 id = 1,事务 2 先更新 id = 2,之后各自更新对方已经锁住的行:
```sql
-- 事务 1
BEGIN;
UPDATE account SET balance = balance - 100 WHERE id = 1;
UPDATE account SET balance = balance + 100 WHERE id = 2; -- 等待事务 2 释放 id = 2 的行锁
-- 事务 2
BEGIN;
UPDATE account SET balance = balance - 50 WHERE id = 2;
UPDATE account SET balance = balance + 50 WHERE id = 1;
-- ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
```
InnoDB 默认开启死锁检测,发现环后立即回滚其中一个事务(倾向于回滚修改行数少的事务),另一个事务继续执行。上例中事务 2 被回滚,事务 1 提交后余额为 900 和 1100。执行 SHOW ENGINE INNODB STATUS,在 LATEST DETECTED DEADLOCK 一节中可以看到两个事务各自持有和等待的锁。被回滚的事务需要由应用重试。
减少数据库死锁的方法:
- 按相同的顺序访问行,例如转账时先更新 id 小的账户;
- 事务尽量短,不在事务中调用外部接口或等待用户操作;
- 为更新的条件列建立索引,避免扫描时锁住过多的行。
## 参考资料
- Tanenbaum A S, Bos H. Modern Operating Systems [M]. Pearson, 2014.
- [Deadlocks in InnoDB | MySQL 8.0 Reference Manual](https://dev.mysql.com/doc/refman/8.0/en/innodb-deadlocks.html)
+121
View File
@@ -0,0 +1,121 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">多个资源的银行家算法:找出一个安全序列</text>
<text x="100" y="69" class="sub b e" style="fill:#444">E 总量</text>
<rect x="108" y="52" width="30" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="123" y="69" class="c sub mono" style="fill:#444">9</text>
<rect x="138" y="52" width="30" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="153" y="69" class="c sub mono" style="fill:#444">5</text>
<rect x="168" y="52" width="30" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="183" y="69" class="c sub mono" style="fill:#444">7</text>
<text x="290" y="69" class="sub b e" style="fill:#444">Available</text>
<rect x="298" y="52" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="313" y="69" class="c sub mono" style="fill:#444">1</text>
<rect x="328" y="52" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="343" y="69" class="c sub mono" style="fill:#444">1</text>
<rect x="358" y="52" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="373" y="69" class="c sub mono" style="fill:#444">2</text>
<text x="119" y="114" class="c sub b" style="fill:#444">Allocation 已分配</text>
<text x="54" y="139" class="sub e">P1</text>
<rect x="74" y="122" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="89" y="139" class="c sub mono" style="fill:#444">2</text>
<rect x="104" y="122" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="119" y="139" class="c sub mono" style="fill:#444">1</text>
<rect x="134" y="122" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="149" y="139" class="c sub mono" style="fill:#444">1</text>
<text x="54" y="165" class="sub e">P2</text>
<rect x="74" y="148" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="89" y="165" class="c sub mono" style="fill:#444">3</text>
<rect x="104" y="148" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="119" y="165" class="c sub mono" style="fill:#444">0</text>
<rect x="134" y="148" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="149" y="165" class="c sub mono" style="fill:#444">2</text>
<text x="54" y="191" class="sub e">P3</text>
<rect x="74" y="174" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="89" y="191" class="c sub mono" style="fill:#444">1</text>
<rect x="104" y="174" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="119" y="191" class="c sub mono" style="fill:#444">2</text>
<rect x="134" y="174" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="149" y="191" class="c sub mono" style="fill:#444">1</text>
<text x="54" y="217" class="sub e">P4</text>
<rect x="74" y="200" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="89" y="217" class="c sub mono" style="fill:#444">2</text>
<rect x="104" y="200" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="119" y="217" class="c sub mono" style="fill:#444">1</text>
<rect x="134" y="200" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="149" y="217" class="c sub mono" style="fill:#444">1</text>
<text x="289" y="114" class="c sub b" style="fill:#444">Need 还需要</text>
<text x="224" y="139" class="sub e">P1</text>
<rect x="244" y="122" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="259" y="139" class="c sub mono" style="fill:#444">3</text>
<rect x="274" y="122" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="289" y="139" class="c sub mono" style="fill:#444">2</text>
<rect x="304" y="122" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="319" y="139" class="c sub mono" style="fill:#444">2</text>
<text x="224" y="165" class="sub e">P2</text>
<rect x="244" y="148" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="259" y="165" class="c sub mono" style="fill:#444">1</text>
<rect x="274" y="148" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="289" y="165" class="c sub mono" style="fill:#444">0</text>
<rect x="304" y="148" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="319" y="165" class="c sub mono" style="fill:#444">2</text>
<text x="224" y="191" class="sub e">P3</text>
<rect x="244" y="174" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="259" y="191" class="c sub mono" style="fill:#444">5</text>
<rect x="274" y="174" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="289" y="191" class="c sub mono" style="fill:#444">2</text>
<rect x="304" y="174" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="319" y="191" class="c sub mono" style="fill:#444">0</text>
<text x="224" y="217" class="sub e">P4</text>
<rect x="244" y="200" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="259" y="217" class="c sub mono" style="fill:#444">0</text>
<rect x="274" y="200" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="289" y="217" class="c sub mono" style="fill:#444">1</text>
<rect x="304" y="200" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="319" y="217" class="c sub mono" style="fill:#444">1</text>
<text x="420" y="46" class="b">安全序列 P2 → P4 → P1 → P3</text>
<rect x="420" y="58" width="300" height="44" rx="5" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="432" y="76" class="sub" style="fill:#444">1. P2 需要 (1 0 2) ≤ (1 1 2)</text>
<text x="446" y="94" class="sub">归还后 Available = (4 1 4)</text>
<rect x="420" y="112" width="300" height="44" rx="5" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="432" y="130" class="sub" style="fill:#444">2. P4 需要 (0 1 1) ≤ (4 1 4)</text>
<text x="446" y="148" class="sub">归还后 Available = (6 2 5)</text>
<rect x="420" y="166" width="300" height="44" rx="5" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="432" y="184" class="sub" style="fill:#444">3. P1 需要 (3 2 2) ≤ (6 2 5)</text>
<text x="446" y="202" class="sub">归还后 Available = (8 3 6)</text>
<rect x="420" y="220" width="300" height="44" rx="5" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="432" y="238" class="sub" style="fill:#444">4. P3 需要 (5 2 0) ≤ (8 3 6)</text>
<text x="446" y="256" class="sub">归还后 Available = (9 5 7)</text>
<text x="30" y="290" class="sub">Need = 最大需求 − 已分配。一开始只有 P2 的需求能被满足,P1 缺 R2、P3 缺 R1;</text>
<text x="30" y="308" class="sub">收到新的申请时,先假设分配,再用这个方法检查,找不到安全序列就拒绝。</text>
</svg>

After

Width:  |  Height:  |  Size: 8.9 KiB

@@ -0,0 +1,84 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">单个资源的银行家算法:共 12 个,分配前先检查分配后是否安全</text>
<text x="140" y="58" class="c sub b" style="fill:#444">(a) 当前状态</text>
<text x="52" y="91" class="sub b e" style="fill:#444">A</text>
<rect x="60" y="78" width="104" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="60" y="78" width="39" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="170" y="91" class="sub mono">3 / 8</text>
<text x="52" y="123" class="sub b e" style="fill:#444">B</text>
<rect x="60" y="110" width="52" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="60" y="110" width="26" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="118" y="123" class="sub mono">2 / 4</text>
<text x="52" y="155" class="sub b e" style="fill:#444">C</text>
<rect x="60" y="142" width="117" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="60" y="142" width="52" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="183" y="155" class="sub mono">4 / 9</text>
<rect x="60" y="180" width="80" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="100" y="197" class="c sub" style="fill:#444">Free 3</text>
<text x="60" y="228" class="sub ok">安全:B → A → C</text>
<text x="380" y="58" class="c sub b" style="fill:#444">(b) C 再申请 2 个</text>
<text x="292" y="91" class="sub b e" style="fill:#444">A</text>
<rect x="300" y="78" width="104" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="300" y="78" width="39" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="410" y="91" class="sub mono">3 / 8</text>
<text x="292" y="123" class="sub b e" style="fill:#444">B</text>
<rect x="300" y="110" width="52" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="300" y="110" width="26" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="358" y="123" class="sub mono">2 / 4</text>
<text x="292" y="155" class="sub b e" style="fill:#444">C</text>
<rect x="300" y="142" width="117" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="300" y="142" width="78" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="423" y="155" class="sub mono">6 / 9</text>
<rect x="300" y="180" width="80" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="340" y="197" class="c sub" style="fill:#444">Free 1</text>
<text x="300" y="228" class="sub note">不安全:谁都补不齐,拒绝</text>
<text x="620" y="58" class="c sub b" style="fill:#444">(c) B 再申请 1 个</text>
<text x="532" y="91" class="sub b e" style="fill:#444">A</text>
<rect x="540" y="78" width="104" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="540" y="78" width="39" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="650" y="91" class="sub mono">3 / 8</text>
<text x="532" y="123" class="sub b e" style="fill:#444">B</text>
<rect x="540" y="110" width="52" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="540" y="110" width="39" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="598" y="123" class="sub mono">3 / 4</text>
<text x="532" y="155" class="sub b e" style="fill:#444">C</text>
<rect x="540" y="142" width="117" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="540" y="142" width="52" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="663" y="155" class="sub mono">4 / 9</text>
<rect x="540" y="180" width="80" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="580" y="197" class="c sub" style="fill:#444">Free 2</text>
<text x="540" y="228" class="sub ok">安全:B → A → C,分配</text>
<text x="40" y="270" class="sub">(b) 中 A、B、C 分别还需要 5、2、3 个,Free 只有 1,任何进程都无法运行完,可能发生死锁,所以拒绝这次申请,</text>
<text x="40" y="288" class="sub">让 C 等待;(c) 中 B 只差 1 个,运行完归还 4 个后 Free = 5,A 和 C 也能依次完成,所以可以分配。</text>
</svg>

After

Width:  |  Height:  |  Size: 5.9 KiB

@@ -0,0 +1,60 @@
<svg xmlns="http://www.w3.org/2000/svg" width="720" height="330" viewBox="0 0 720 330">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="360" y="28" class="title">每种资源只有一个:在资源分配图中找环</text>
<circle cx="110" cy="90" r="20" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
<text x="110" y="94" class="c sub b" style="fill:#444">P1</text>
<circle cx="390" cy="90" r="20" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
<text x="390" y="94" class="c sub b" style="fill:#444">P2</text>
<circle cx="250" cy="250" r="20" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
<text x="250" y="254" class="c sub b" style="fill:#444">P3</text>
<circle cx="560" cy="250" r="20" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
<text x="560" y="254" class="c sub b" style="fill:#444">P4</text>
<rect x="231" y="71" width="38" height="38" rx="4" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
<text x="250" y="94" class="c sub b" style="fill:#444">R1</text>
<rect x="461" y="151" width="38" height="38" rx="4" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
<text x="480" y="174" class="c sub b" style="fill:#444">R2</text>
<rect x="91" y="231" width="38" height="38" rx="4" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
<text x="110" y="254" class="c sub b" style="fill:#444">R3</text>
<rect x="621" y="91" width="38" height="38" rx="4" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
<text x="640" y="114" class="c sub b" style="fill:#444">R4</text>
<line x1="131" y1="90" x2="229" y2="90" class="arrow red"/>
<line x1="271" y1="90" x2="369" y2="90" class="arrow red"/>
<line x1="405.696" y1="103.952" x2="464.304" y2="156.048" class="arrow red"/>
<line x1="460.166" y1="176.899" x2="269.834" y2="243.101" class="arrow red"/>
<line x1="229" y1="250" x2="131" y2="250" class="arrow red"/>
<line x1="110" y1="229" x2="110" y2="111" class="arrow red"/>
<line x1="545.151" y1="235.151" x2="494.849" y2="184.849" class="arrow "/>
<line x1="629.581" y1="128.233" x2="570.419" y2="231.767" class="arrow "/>
<text x="360" y="312" class="c sub">资源指向进程表示已分配,进程指向资源表示正在请求。红色的边构成环 P1 → R1 → P2 → R2 → P3 → R3 → P1,</text>
<text x="360" y="328" class="c sub">三个进程互相等待,发生死锁;P4 不在环上,但它请求的 R2 被 P2 持有,也会一直等下去。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.8 KiB

@@ -0,0 +1,105 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">每种资源有多个:逐个找能满足请求的进程</text>
<text x="30" y="50" class="sub">资源 R1、R2、R3</text>
<text x="90" y="79" class="sub b e" style="fill:#444">E 总量</text>
<rect x="98" y="62" width="30" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="113" y="79" class="c sub mono" style="fill:#444">5</text>
<rect x="128" y="62" width="30" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="143" y="79" class="c sub mono" style="fill:#444">3</text>
<rect x="158" y="62" width="30" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="173" y="79" class="c sub mono" style="fill:#444">4</text>
<text x="280" y="79" class="sub b e" style="fill:#444">A 剩余</text>
<rect x="288" y="62" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="303" y="79" class="c sub mono" style="fill:#444">1</text>
<rect x="318" y="62" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="333" y="79" class="c sub mono" style="fill:#444">1</text>
<rect x="348" y="62" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="363" y="79" class="c sub mono" style="fill:#444">1</text>
<text x="119" y="124" class="c sub b" style="fill:#444">C 已分配</text>
<text x="54" y="149" class="sub e">P1</text>
<rect x="74" y="132" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="89" y="149" class="c sub mono" style="fill:#444">1</text>
<rect x="104" y="132" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="119" y="149" class="c sub mono" style="fill:#444">1</text>
<rect x="134" y="132" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="149" y="149" class="c sub mono" style="fill:#444">0</text>
<text x="54" y="175" class="sub e">P2</text>
<rect x="74" y="158" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="89" y="175" class="c sub mono" style="fill:#444">2</text>
<rect x="104" y="158" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="119" y="175" class="c sub mono" style="fill:#444">0</text>
<rect x="134" y="158" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="149" y="175" class="c sub mono" style="fill:#444">1</text>
<text x="54" y="201" class="sub e">P3</text>
<rect x="74" y="184" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="89" y="201" class="c sub mono" style="fill:#444">1</text>
<rect x="104" y="184" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="119" y="201" class="c sub mono" style="fill:#444">1</text>
<rect x="134" y="184" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="149" y="201" class="c sub mono" style="fill:#444">2</text>
<text x="289" y="124" class="c sub b" style="fill:#444">R 还在请求</text>
<text x="224" y="149" class="sub e">P1</text>
<rect x="244" y="132" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="259" y="149" class="c sub mono" style="fill:#444">2</text>
<rect x="274" y="132" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="289" y="149" class="c sub mono" style="fill:#444">0</text>
<rect x="304" y="132" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="319" y="149" class="c sub mono" style="fill:#444">1</text>
<text x="224" y="175" class="sub e">P2</text>
<rect x="244" y="158" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="259" y="175" class="c sub mono" style="fill:#444">1</text>
<rect x="274" y="158" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="289" y="175" class="c sub mono" style="fill:#444">1</text>
<rect x="304" y="158" width="30" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="319" y="175" class="c sub mono" style="fill:#444">0</text>
<text x="224" y="201" class="sub e">P3</text>
<rect x="244" y="184" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="259" y="201" class="c sub mono" style="fill:#444">1</text>
<rect x="274" y="184" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="289" y="201" class="c sub mono" style="fill:#444">2</text>
<rect x="304" y="184" width="30" height="26" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="319" y="201" class="c sub mono" style="fill:#444">1</text>
<text x="420" y="50" class="b">按算法依次标记</text>
<rect x="420" y="62" width="320" height="58" rx="5" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="432" y="80" class="sub" style="fill:#444">1. P2 请求 (1 1 0) ≤ A,可以运行</text>
<text x="446" y="100" class="sub">结束后归还 (2 0 1),A = (3 1 2)</text>
<rect x="420" y="132" width="320" height="58" rx="5" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="432" y="150" class="sub" style="fill:#444">2. P1 请求 (2 0 1) ≤ A,可以运行</text>
<text x="446" y="170" class="sub">结束后归还 (1 1 0),A = (4 2 2)</text>
<rect x="420" y="202" width="320" height="58" rx="5" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="432" y="220" class="sub" style="fill:#444">3. P3 请求 (1 2 1) ≤ A,可以运行</text>
<text x="446" y="240" class="sub">结束后归还 (1 1 2),A = (5 3 4)</text>
<text x="30" y="286" class="sub">开始时 A 只满足 P2 的请求;P2 结束后归还资源,P1、P3 才依次得到满足。</text>
<text x="30" y="304" class="sub">所有进程都能被标记,没有死锁;如果某一步找不到能满足的进程,剩下没被标记的进程就处于死锁中。</text>
</svg>

After

Width:  |  Height:  |  Size: 7.6 KiB

+85
View File
@@ -0,0 +1,85 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">安全状态:共 10 个资源,存在一个能让所有进程运行完的顺序</text>
<text x="100" y="58" class="c sub b" style="fill:#444">(a) 初始</text>
<text x="52" y="91" class="sub b e" style="fill:#444">A</text>
<rect x="60" y="78" width="91" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="60" y="78" width="26" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="157" y="91" class="sub mono">2 / 7</text>
<text x="52" y="123" class="sub b e" style="fill:#444">B</text>
<rect x="60" y="110" width="65" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="60" y="110" width="39" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="131" y="123" class="sub mono">3 / 5</text>
<text x="52" y="155" class="sub b e" style="fill:#444">C</text>
<rect x="60" y="142" width="52" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="60" y="142" width="26" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="118" y="155" class="sub mono">2 / 4</text>
<rect x="60" y="180" width="80" height="26" rx="3" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="100" y="197" class="c sub" style="fill:#444">Free 3</text>
<text x="280" y="58" class="c sub b" style="fill:#444">(b) B 再拿 2 个,运行结束</text>
<text x="232" y="91" class="sub b e" style="fill:#444">A</text>
<rect x="240" y="78" width="91" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="240" y="78" width="26" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="337" y="91" class="sub mono">2 / 7</text>
<text x="232" y="123" class="sub b e" style="fill:#BBB">B</text>
<text x="240" y="123" class="sub" style="fill:#BBB">已完成</text>
<text x="232" y="155" class="sub b e" style="fill:#444">C</text>
<rect x="240" y="142" width="52" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="240" y="142" width="26" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="298" y="155" class="sub mono">2 / 4</text>
<rect x="240" y="180" width="80" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="280" y="197" class="c sub" style="fill:#444">Free 6</text>
<text x="460" y="58" class="c sub b" style="fill:#444">(c) A 再拿 5 个,运行结束</text>
<text x="412" y="91" class="sub b e" style="fill:#BBB">A</text>
<text x="420" y="91" class="sub" style="fill:#BBB">已完成</text>
<text x="412" y="123" class="sub b e" style="fill:#BBB">B</text>
<text x="420" y="123" class="sub" style="fill:#BBB">已完成</text>
<text x="412" y="155" class="sub b e" style="fill:#444">C</text>
<rect x="420" y="142" width="52" height="18" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<rect x="420" y="142" width="26" height="18" rx="3" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1"/>
<text x="478" y="155" class="sub mono">2 / 4</text>
<rect x="420" y="180" width="80" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="460" y="197" class="c sub" style="fill:#444">Free 8</text>
<text x="640" y="58" class="c sub b" style="fill:#444">(d) C 再拿 2 个,运行结束</text>
<text x="592" y="91" class="sub b e" style="fill:#BBB">A</text>
<text x="600" y="91" class="sub" style="fill:#BBB">已完成</text>
<text x="592" y="123" class="sub b e" style="fill:#BBB">B</text>
<text x="600" y="123" class="sub" style="fill:#BBB">已完成</text>
<text x="592" y="155" class="sub b e" style="fill:#BBB">C</text>
<text x="600" y="155" class="sub" style="fill:#BBB">已完成</text>
<rect x="600" y="180" width="80" height="26" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="640" y="197" class="c sub" style="fill:#444">Free 10</text>
<text x="40" y="238" class="sub">条形的长度是最大需求,浅蓝部分是已持有的数量。(a) 中 Free = 10 − 2 − 3 − 2 = 3,只够 B 补齐,</text>
<text x="40" y="256" class="sub">B 运行完归还 5 个后 Free = 6,又够 A 补齐 5 个,依此类推。</text>
<text x="40" y="280" class="sub">安全不代表一定按这个顺序执行,只说明不管进程怎样申请,系统都能找到一条出路。</text>
</svg>

After

Width:  |  Height:  |  Size: 5.9 KiB

+66
View File
@@ -0,0 +1,66 @@
<svg xmlns="http://www.w3.org/2000/svg" width="740" height="330" viewBox="0 0 740 330">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="370" y="28" class="title">死锁:两个线程反向转账</text>
<circle cx="150" cy="110" r="20" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
<text x="150" y="114" class="c sub b" style="fill:#444">T1</text>
<circle cx="150" cy="250" r="20" fill="#C6ECE9" stroke="#7DBCB7" stroke-width="1.2"/>
<text x="150" y="254" class="c sub b" style="fill:#444">T2</text>
<rect x="311" y="91" width="38" height="38" rx="4" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
<text x="330" y="114" class="c sub b" style="fill:#444">alice</text>
<rect x="311" y="231" width="38" height="38" rx="4" fill="#C6E4FB" stroke="#7FB2D6" stroke-width="1.2"/>
<text x="330" y="254" class="c sub b" style="fill:#444">bob</text>
<line x1="309" y1="110" x2="171" y2="110" class="arrow "/>
<text x="240" y="100" class="c sub">持有</text>
<line x1="309" y1="250" x2="171" y2="250" class="arrow "/>
<text x="240" y="266" class="c sub">持有</text>
<line x1="166.576" y1="122.893" x2="313.424" y2="237.107" class="arrow red"/>
<text x="206" y="166" class="c sub note">等待</text>
<line x1="166.576" y1="237.107" x2="313.424" y2="122.893" class="arrow red"/>
<text x="276" y="166" class="c sub note">等待</text>
<text x="60" y="80" class="sub mono">transfer(alice, bob)</text>
<text x="60" y="296" class="sub mono">transfer(bob, alice)</text>
<text x="240" y="316" class="c sub">圆形是线程,方形是账户上的锁</text>
<text x="420" y="70" class="b">四个条件同时满足:</text>
<rect x="420" y="92" width="300" height="40" rx="5" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="432" y="109" class="sub b" style="fill:#444">互斥</text>
<text x="432" y="125" class="sub">一把锁同一时刻只能被一个线程持有</text>
<rect x="420" y="144" width="300" height="40" rx="5" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="432" y="161" class="sub b" style="fill:#444">占有和等待</text>
<text x="432" y="177" class="sub">T1 持有 alice 的锁,还在等 bob 的锁</text>
<rect x="420" y="196" width="300" height="40" rx="5" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="432" y="213" class="sub b" style="fill:#444">不可抢占</text>
<text x="432" y="229" class="sub">bob 的锁只能由 T2 自己释放</text>
<rect x="420" y="248" width="300" height="40" rx="5" fill="#E6E3FA" stroke="#B8B0DE" stroke-width="1"/>
<text x="432" y="265" class="sub b" style="fill:#444">环路等待</text>
<text x="432" y="281" class="sub">T1 → bob → T2 → alice → T1</text>
</svg>

After

Width:  |  Height:  |  Size: 4.1 KiB