信号量与PV操作全解.pptVIP

  • 9
  • 0
  • 约7.71千字
  • 约 32页
  • 2017-01-28 发布于湖北
  • 举报
信号量与PV操作全解

item B[k]; semaphore empty; empty=k; //可以使用的空缓冲区数 semaphore full; full=0; //缓冲区内可以使用的产品数 int in=0; //放入缓冲区指针 int out=0; //取出缓冲区指针? cobegin process producer ( ){ while(true) { produce( ); P(empty); append to B[in]; in=(in+1)%k; V(full); } } coend ? * process consumer ( ){ while(true) { P(full); take( ) from B[out]; out=(out+1)%k; V(empty); consume( ); } } item B[k]; semaphore empty; empty=k; //可以使用的空缓冲区数 semaphore full; full=0; //缓冲区内可以使用的产品数 semaphore mutex; mutex=1; //互斥信号量 int in=0; //放入缓冲区指针 int out=0; //取出缓冲区指针? cobegin process producer_i ( ){ while(true) { produce( ); P(empty); P(mutex); append to B[in]; in=(in+1)%k; V(mutex); V(full); } } coend ? * process consumer_j ( ){ while(true) { P(full); P(mutex); take( ) from B[out]; out=(out+1)%k; V(mutex); V(empty); consume( ); } } 有两组并发进程:读者和写者,共享一个文件F,要求: 允许多个读者同时执行读操作 任一写者在完成写操作之前不允许其它读者或写者工作 写者执行写操作前,应让已有的写者和读者全部退出 * int readcount=0;

文档评论(0)

1亿VIP精品文档

相关文档