CSAPP》(第3版)答案(第十二章)(二)
P28
没有影响
- 1
thread 1 | thread 2 |
---|---|
P(s) | P(s) |
P(t) | P(t) |
V(s) | V(s) |
V(t) | V(t) |
+
|
|
+ +--------------------+
V(t)| | |
| | |
+ | unsafe region t |
| | |
| | |
+ +---------+---------+ |
V(s)| | | | |
| | | | |
+ | | | |
| | | | |
| | | | |
+ | +---------+----------+
P(t)| | |
| | unsafe region s |
+ | |
| | |
| | |
+ +-------------------+
P(s)|
|
+
|
|
+----+----+----+----+----+----+----+----+----+ thread 1
P(s) P(t) V(s) V(t)
- 2
thread 1 | thread 2 |
---|---|
P(s) | P(s) |
P(t) | P(t) |
V(s) | V(t) |
V(t) | V(s) |
+
|
|
+ +-------------------+
V(s)| | |
| | |
+ | |
| | |
| | |
+ | +---------+----------+
V(t)| | | | |
| | | | unsafe |
+ | | | region |
| | | | t |
| | | | |
+ | +---------+----------+
P(t)| | |
| | |
+ | unsafe region s |
| | |
| | |
+ +-------------------+
P(s)|
|
+
|
|
+----+----+----+----+----+----+----+----+----+ thread 1
P(s) P(t) V(s) V(t)
- 3
thread 1 | thread 2 |
---|---|
P(s) | P(s) |
P(t) | P(t) |
V(t) | V(s) |
V(s) | V(t) |
+
|
|
+ +---------+
V(t)| | unsafe |
| | region |
+ | t |
| | |
| | |
+ +---------+---------+----------+
V(s)| | | | |
| | | | |
+ | | | |
| | | | |
| | | | |
+ | +---------+ |
P(t)| | |
| | |
+ | unsafe region s |
| | |
| | |
+ +------------------------------+
P(s)|
|
+
|
|
+----+----+----+----+----+----+----+----+----+ thread 1
P(s) P(t) V(t) V(s)
- 4
thread 1 | thread 2 |
---|---|
P(s) | P(s) |
P(t) | P(t) |
V(t) | V(t) |
V(s) | V(s) |
+
|
|
+ +------------------------------+
V(s)| | |
| | |
+ | |
| | |
| | |
+ | +----------+ |
V(t)| | | unsafe | |
| | | region | |
+ | | t | |
| | | | |
| | | | |
+ | +----------+ |
P(t)| | |
| | |
+ | unsafe region s |
| | |
| | |
+ +------------------------------+
P(s)|
|
+
|
|
+----+----+----+----+----+----+----+----+----+ thread 1
P(s) P(t) V(t) V(s)
P29
不会死锁
如下图所示:
+
|
|
+ +----------+
V(c)| | |
| | |
+ | |
| | |
| | |
+ +----------+ | |
V(b)| | | | |
| | | | |
+ | | | |
| | | | |
| | | | |
+ +----------+ | |
P(b)| | |
| | |
+ | |
| | |
| | |
+ +----------+
P(c)|
|
+
|
|
+----+----+----+----+----+----+----+----+----+ thread 1
P(b) V(b) P(c) V(c)
P30
- A
thread | pairs of mutexes |
---|---|
1 | a&b a&c |
2 | b&c |
3 | a&b |
- B
2&3 - C
在每个线程中遵守P(a), P(b), P©的顺序。
P31
#include <stdio.h>
#include "csapp.h"
sigjmp_buf buf;
void sigchild_handler(int sig) {
siglongjmp(buf, 1);
}
char *tfgets(char *s, int size, FILE *stream) {
if (Fork() == 0) {