Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. (15 points) A barbershop is designed so that there is a private room containi

ID: 3596984 • Letter: 5

Question

5. (15 points) A barbershop is designed so that there is a private room containing the barber chair and an adjoining waiting room with a sliding door that contains k chairs. If the barber is busy, then the door to the private room is closed and arriving customers sit in one of the available chairs and waits for his/her turn. If no chair is available, the customer leaves the shop without an haircut. If there is no customer to be served, the barber goes to sleep in the barber chair with the door to the waiting room open. If the barber is asleep, a newly arrived customer wakes the barber and obtains a haircut. Use semaphores and wait/signal operations to synchronize the barber and customer processes else

Explanation / Answer

Solution:

//There are two threads, Customer_Thread is created when Customer enter to the Barbershop.
//Barber thread is for Barber.


set chair = k

Customer_thread()
{
if(chair_is_available())
{
semaphore_lock(chair);         //for chair allocation
{
semaphore_lock(&door);    //wait for door unlock
semaphore_unlock(chair);    //free the waiting chair
signal(barber);            //now awake the barber
wait(customer);              //wait , until the cutting is done
}
}
else
{
semaphore_unlock( chair);
return;
}

Barber_thread()
{
while(1)
{
wait(doc);        //sleep barber
serving the customer
signal(customer);      //signal to go
semaphore_unlock (&door);
}

}

Please, please upvote and ask your doubts in the comments.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote