本次美国代写是操作系统的一个Homework
For this assignment, you are to walk through the sleeping barber problem and fill in the values of each semaphore and queue at every step of the problem. Also, answer all the questions at the end of this document.
Code for our sleeping barbers problem appears below.
//A variant of the Sleeping Barber Problem using 1 binary semaphore and 2 general semaphores
// background details https://en.wikipedia.org/wiki/Sleeping_barber_problem
binary_semaphore AccessToWaitingRoomSeats=1;
general_semaphore BarberReady=0, CustomerReady=0;
int numberOfFreeWaitingRoomSeats;
void Barber() {
while (true) {
semWait(CustomerReady);
semWaitB(AccessToWaitingRoomSeats);
numberOfFreeWaitingRoomSeats += 1;
semSignal(BarberReady);
semSignalB(AccessToWaitingRoomSeats);
CutHair();
}
}
void Customer() {
semWaitB(AccessToWaitingRoomSeats);
if(numberOfFreeWaitingRoomSeats>0)
{
numberOfFreeWaitingRoomSeats –= 1;
semSignal(CustomerReady);
semSignalB(AccessToWaitingRoomSeats);
semWait(BarberReady);
GetHairCut();
}
else
{ //no space, must leave!
semSignalB(AccessToWaitingRoomSeats);
}
}
void main()
{
numberOfFreeWaitingRoomSeats=2;
parbegin(barber, barber, customer, customer, customer, customer);
}
Sleeping Barber Questions:
- How many people / processes were involved with the barbershop? (5 pts)
- How many people were completely done getting a haircut at the end of the code step-through? (5 pts)
- How many people were still waiting to get their haircut? (5pts)
- How many people walked out? Why? (10pts)