学习
- void exchange(BiTree BT)
- {
- BiTree r,p;
- BiTree stack[100];
- int tp=0;
- stack[0]=BT;
- while(tp >= 0)
- {
- p=stack[tp];
- tp=tp-1;
- if(p!=NULL)
- {
- r=p-> lchild;
- p-> lchild=p-> rchild;
- BT-> rchild=r;
- stack[tp+1]=p-> lchild;
- tp=tp+1;
- stack[tp+1]=p-> rchild;
- tp=tp+1;
- }
- }
- }
A,B,C,D四个进程,A向buf里面写数据,B,C,D向buf里面读数据,
当A写完,且B,C,D都读一次后,A才能再写。用P,V操作实现。
semaphore empty = n
semaphore full;
semaphore mutex =1;
semaphore b = 1;
semaphore c = 1 ;
semaphore d = 1;
A (){
while(true){
p(empty);
p(b);
p(c);
p(d);
p(mutex);
write();
v(mutex);
v(full);
}
}
B (){
while(true){
p(full);
p(mutex);
write();
v(mutex);
v(empty);
v(b);
}
}
C(){
while(true){
p(full);
p(mutex);
write();
v(mutex);
v(empty);
v(c);
}
}
D (){
while(true){
p(full);
p(mutex);
write();
v(mutex);
v(empty);
v(d);
}
}
浙公网安备 33010602011771号