Consider four processes A, B, C and D, sharing a same buffer. Process A inputs one object into the b

Consider four processes A, B, C and D, sharing a same buffer. Process A inputs one object into the buffer each time, and it will not input the next object until that all process B, C and D have accessed the object once.

(1) How many semaphores will be needed for correct communication among process A, B, C and D?

(2) Try to solve the above scene’s synchronization and mutex using P, V operations.

这是一道华为面试题,在这里我把自己的思路写一下,供大家参考。

(1)设置4个信号量,mutexB=mutexC=mutexD=1;int s=0;//s=1表示A已经生产

(2)process A:

while(true){

Wait(mutexB);

Wait(mutexC);

Wait(mutexD);

S=0;

//produce item

S=1;

signal(mutexB);

signal(mutexC);

signal(mutexD);

}

 

Process B:

While(true){

Wait(mutexB);

If(s==1){

//consume item;

}

Signal(mutexB);

}//进程C、D跟B同理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值