《CSAPP》(第3版)答案(第十二章)(二)

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) {
   
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值