Chapter 31 Signal Quantity
第 31 章 信号量
1. 信号量的定义
1.1. 初始化
#include <semaphore.h>
sem_t s;
sem_init(&s, 0, 1);1.2. 交互
int sem_wait(sem_t *s)
{
// decrement the value of semaphore s by one
// wait if value of semaphore s is negative
}
int sem_post(sem_t *s)
{
// increment the value of semaphore s by one
// if there are one or more threads waiting, wake one
}2. 二值信号量(锁)
2.1. 使用
2.2. 两个线程使用同一个信号量
3. 信号量用作条件变量
4. 生产者/消费者(有界缓冲区)问题
4.1. 第一次尝试
4.2. 增加互斥
4.3. 避免死锁
4.4. 可行的方案
5. 读者 - 写者锁
6. 哲学家就餐问题
6.1. 有问题的方案
6.2. 破除依赖
7. 如何实现信号量
8. 作业(编码作业)
8.1. fork-join
8.2. 一个双子进程的等待
8.3. 8.2 拓展到 多子进程
8.4. reader-writer problem
8.5. reader-writer problem nostarve
8.6. mutex nostarve
Last updated