1、一个生产者、一个消费者共享一个缓冲区app
int B; semaphore empty; //能够使用的空缓冲区数 semaphore full; //缓冲区内能够使用的产品数 empty=1; //缓冲区内容许放入一件产品 full=0; //缓冲区内没有产品 process producer() { while(true){ produce( ); P(empty); //申请空盘子 append( ) to B; V(full); // 放 } } process consumer() { while(true) { P(full); //消费 take( ) from B; V(empty); consume( ); } }
2、一个生产者、一个消费者共享多个缓冲区spa
3、多个生产者、多个消费者共享多个缓冲区指针
item B[k]; semaphore empty; empty=k; semaphore full; full=0; semaphore mutex=1; //互斥信号量 int in=0; //放入缓冲区指针 int out=0; //取出缓冲区指针? process producer_i ( ){ while(true) { produce( ); P(empty); //申请临界区资源 P(mutex); //确保惟一操做 append to B[in]; in=(in+1)%k; V(mutex); V(full); } } process consumer_j () { while(true) { P(full);//申请临界区资源 P(mutex);//确保惟一操做 take( ) from B[out]; out=(out+1)%k; V(mutex); V(empty); consume( ); }