使用管程解决 Void producer 生产者消费者 Char x 问题 While true Void consumer( [produce(x) Char x Append(x); While true ttake(x); Void main( consume (parbegin(producer, consumer);; Jy
使用管程解决 生产者/消费者 问题 Void producer() Char x {while true {produce (x); Append(x); }} Void consumer() Char x {while true {take(x); consume (x); }} Void main() {parbegin(producer,consumer);}
管程的问题 如果 signa不在进程未尾 需要两次进程切换 必须确保 signal之后立刻执行唤醒的进程 不能有新进程进入 Notify (x) 唤醒等待队列的头个进程 原进程继续执行
管程的问题 • 如果csignal不在进程末尾 – 需要两次进程切换 • 必须确保csignal之后立刻执行唤醒的进程 – 不能有新进程进入 • Cnotify(x) – 唤醒等待队列的头个进程 – 原进程继续执行
有界缓冲区管程代码 void append( char x) While count==n wait(notfull) Buffer[nestin]=X; Nextin=(nextin+1 )%n; Count++, void take(char x) Notify(notempty):) iwhile count==0 wait(notempt X= Butfernextout] Nextout-= nextout +1)%n Count-- Notify(notfull)
有界缓冲区管程代码 void append(char x) {while count==n cwait(notfull); Buffer[nextin] =x; Nextin=(nextin+1)%n; Count++; Cnotify(notempty); } void take(char x) {while count==0 cwait(notempty); x = Buffer[nextout]; Nextout=(nextout+1)%n; Count--; Cnotify(notfull); }
Notify的优点 唤醒进程执行前再次测试-whie 给每个条件变量设置监视计时器 Broadcast原语 不知道该唤醒几个进程 不知道该唤醒哪个进程 识别错误发出的信号
Cnotify的优点 • 唤醒进程执行前再次测试-while • 给每个条件变量设置监视计时器 • Cbroadcast原语 – 不知道该唤醒几个进程 – 不知道该唤醒哪个进程 • 识别错误发出的信号