建立管程PC,包括:
二过程:put(item)过程;get(item)过程;
一变量:count>=n时满,<=0时空
初始值:in=out=count=0
Monitor PC{ item buffer[N]; int in out; condition notfull,notempty; int count; public: void put(item x){ if(count>=N)cwait(notfull); buffer[in]=x; in=(in+1)%N; count++; csignal(notempty); } void get(item x){ if(count<=0)cwait(notempty); x=buffer[out]; out=(out+1)%N; count--; csignal(notfull); } } void producer(){ item x; while(1){ //produce PC.put(x); } } void consumer(){ item x; while(1){ PC.get(x); //consume } }