ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > CLASS (2016-2017-1)-62903-06102-1 > 2014329600005_胡悦 >
Homework_04 Version 0
👤 Author: by 2014329600005valerie 2016-12-23 18:22:47
6.11 The Sleeping-Barber Problem. A barbershop consists of a waiting room with n chairs and a barber room with one barber 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 the customer leaves the shop. If the barber is busy but the chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the the customer.

semaphore CutChair = 0, MutexChair = 0;

int number = 0;

//共享变量, CutChair制约理发师, MutexChair制约顾客, number椅子数量

Barber:

do {

wait(CutChair);//有人剪,无人睡

Service();

signal(MutexChair);//进人剪,无人睡

} while (TRUE);

Customer i:

{

wait(MutexChair);//有空等,无空走

if(number==n-1)

{

signal(MutexChair);

exit();

}

Else

{

number = number +1;

signal(CutChair);

signal(MutexChair);

Service();

wait(MutexChair);

number = number -1;

signal(MutexChair);

exit();

}

} while (TRUE);

Please login to reply. Login

Reversion History

Loading...
No reversions found.