主要内容:
同步与同步机制
信号量及其操作
信号量的应用
哲学家进餐问题
生产者-消费者问题
读者-写者问题
理发师问题 ;3.1.1 同步与同步机制 ;; int k;
typedef anyitem item; //item类型
nextp, nextc: item;
item buffer[k];
int in=0, out=0, counter=0; ; process producer(void){
while(TRUE){
{produce an item in nextp;}
if(counter==k) sleep(producer);
buffer[in]=nextp;
in=(in+1) % k;
counter++;
if(counter==1) wakeup(consumer);
}
}; process consumer(void){
while(TRUE){
if(counter==0) sleep(consumer);
nextc=buffer[out];
out=(out+1) % k;
counter--;
if(counter==k-1) wakeup(producer);
{consume the item in nextc;}
}
};生产者和消费者单独运行都是正确
原创力文档

文档评论(0)