【操作系统】【信号量】理发师问题

本文探讨了操作系统中的经典问题——睡眠理发师问题。当理发店的n个椅子都已坐满顾客时,新顾客会离开;如果有空位,顾客会坐下等待;若理发师在睡觉,顾客会唤醒他。程序通过信号量协调理发师和顾客的行为,确保资源有效利用和避免死锁。
摘要由CSDN通过智能技术生成

The Sleeping-Barber Problem. A barbershopconsists of a waiting room with n chairs and the barber room containing thebarber chair. If there are no customers to be served, the barber goes to sleep.If a customer enters the barbershop and all chairs are occupied, then thecustomer leaves the shop. If the barber is busy but chairs are available, thenthe customer sits in one of the free chairs. If the barber is asleep, thecustomer wakes up the barber. Write a program to coordinate the barber and the customers.


Int wait =0;

Int chairs=n;

Int babers=0;

Semaphore customs=0, babers=0, mutex=1;

 

Baber(){
         while(true);

         P(customers);//服务一个顾客

         P(mutex);//进入临界区

         Waiting:=waiting-1;//等待顾客数-1

         V(babers);//理发师临界区开放

         V(mutex);//开放临界区

         Cut_hair();//理发

}

 

Customer(){

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值