息。针对下述两种情况:
(1)缓冲区是环形的,最多可容纳n个信息;
(2)缓冲区是无穷大的。
试分别回答下列问题:
(1)输入、输出两组进程读 / 写缓冲区需要什么条件?
(2)用P,V操作写出输入、输出两组进程的同步算法,并给出信号量含义及初值。
// 信号量及说明 typedef semaphore int; // 整型信号量 semaphore mutex = 1; // 互斥信号量mutex semaphore empty = n; // 空缓冲区初始为n semaphore full = 0; // 满缓冲区初始为n int in = 0; //输入指针 int out = 0; //输出指针 // Input while (true) { P(empty); P(mutex); 向buffer(in) 写入; in = (in + 1) % n; V(mutex); V(full); } // Output while(true) { P(full); P(mutex); 从bufffer(out)读信息; out = (out + 1) % n; V(mutex); V(empty); }