操作系统–经典进程同步与互斥问题
我以下所写的算法都是基于我所学习的书籍《计算机操作系统–第4版》的第84页 所写
仅供参考 如有错误 烦请指出
1.生产者消费者算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| semaphore mutex = 1; semaphore empty = n; semaphore full = 0;
void 生产者() { while(true) { ---生产产品--- P(empty); P(mutex); ---放入缓冲区--- V(mutex); V(full); } }
void 消费者() { while(true) { P(full); P(mutex); ---拿出产品--- V(mutex); V(empty); ---使用产品--- } }
|
2.读者写者问题
① 读者优先
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| semaphore Rmutex = 1; semaphore Wmutex = 1; int Rcount = 0;
void 读者() { while(true) { P(Rmutex); if(Rcount == 0) P(Wmutex); Rcount++; V(Rmutex); ---读操作--- P(Rmutex); Rcount--; if(Rcount == 0) V(Wmutex); V(Rmutex); } }
void 写者() { while(true) { P(Wmutex); ---写操作--- V(Wmutex); } }
|
②读写公平(先来先服务)
这个算法是在读者优先的基础上进行修改得来的
(增加一个互斥变量mutex 实现读写公平 实现了先来先服务)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| semaphore Rmutex = 1; semaphore Wmutex = 1; semaphore mutex = 1; int Rcount = 0;
void 读者() { while(true) { P(mutex); P(Rmutex); if(Rcount == 0) P(Wmutex); Rcount++; V(Rmutex); V(mutex;) ---读操作--- P(Rmutex); Rcount--; if(Rcount == 0) V(Wmutex); V(Rmutex); } }
void 写者() { while(true) { P(mutex); P(Wmutex); ---写操作--- V(Wmutex); V(mutex); } }
|
③写者优先
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| semaphore read = 1; semaphore mutex = 1; semaphore RC = 1; semaphore WC = 1; int Wcount = 0; int Rcount = 0;
void 读者() { while(true) { P(read); P(RC); if(Rcount == 0) P(mutex); Rcount++; V(RC); V(read); ---读操作--- P(RC); Rcount--; if(Rcount == 0) V(mutex); V(RC); } }
void 写者() { while(true) { P(WC); if(Wcount == 0) P(read); Wcount++; V(WC); P(mutex); ---写操作--- V(mutex); P(WC); Wcount--; if(Wcount == 0) V(read); V(WC); } }
|
3.哲学家进餐问题
①课本上的算法(会发生死锁)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| semaphore chopstick[5] = {1,1,1,1,1};
void 哲学家() { while(true) { P(chopstick[i]); P(chopstick[i + 1]); ---进餐--- V(chopstick[i]); V(chopstick[i + 1]); ---思考--- } }
|
②改进后(只允许一个人吃饭)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| semaphore chopstick[5] = {1,1,1,1,1}; semaphore mutex = 1;
void 哲学家() { while(true) { P(mutex); P(chopstick[i]); P(chopstick[i + 1]); V(mutex); ---进餐--- V(chopstick[i]); V(chopstick[i + 1]); ---思考--- } }
|
4.打瞌睡的理发师问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #define CHAIRS = 5; semaphore customer = 0; semaphore barners = 0; semaphore mutex = 1; int waiting = 0;
void 理发师() { P(customer); P(mutex); waiting--; V(barners); V(mutex); cut_hair(); }
void 顾客() { P(mutex); if(waiting < CHAIRS) { waiting++; V(customers); V(mutex); P(barners); get_haircut(); } else { V(mutex); } }
|
5.补充:103页 售票大厅
购票者的工作过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| semaphore empty = 200; semaphore wait = 0; semaphore mutex = 1;
void wait() { P(empty); V(wait); }
void buy() { P(wait); P(mutex); ---买票--- V(mutex); V(empty); }
|