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

POSIX Synchronization In the Sleeping-Barber problem, a barbershop consists of a

ID: 3583472 • Letter: P

Question

POSIX Synchronization In the Sleeping-Barber problem, a barbershop consists of a waiting room with n chairs and one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and the barber is asleep, the customer wakes up the barber. If the barber is busy but chairs are available, then the customer sits in FCFS free chairs. If all chairs are occupied, then the customer is blocked.

Using POSIX semaphores, write a C program to synchronize the operations of the barber and the customers. Your program should display all barbershop operations. Note: waiting room size n should be initialized by user on the command line.

please avoid hand writing and use coputer text to solve the problem.

Explanation / Answer

# define chairs 10 // chairs for the waiting customers
typedef int semaphore; // use this one for the imagination
semaphore customer = 0; // number of the customers waiting for the service
semaphore barber = 0; // number of the barbers waiting for the customers
semaphore mutex = 1; // for the mutual exclusion purpose
int wait = 0; //customers waiting for a haircutting
void barbers(void)
while (TRUE)
{
down(&customer); //go to sleep if the number of the customers are zero
down(&mutex); //acquire the access to waiting
wait = wait -1 ; //decrement the count of the waiting customers
up(&barber); //one barber is ready for the cut haircutting
up(&mutex); //release the waiting
cutting_hair(); //this one is for the out of the critical region for hair cutting
}
}

void customers(void)
{
down(&mutex); //enter into the critical region
if (wait < chairs) //if there are no free chairs then leave the saloon
{
wait = wait +1; //increment the count of the waiting customers
up(&customer); //wake up the barber if the necessary up(&mutex);
// release the access to the waiting
down(&barber); //go to the sleep if number of free barbers are
zero get_haircutting(); //be seated & serviced
}
else
{
up (&mutex); // saloon shop is full do no wait for hair cut
}
}

///*** Thank You ***///

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