In: Computer Science
produce the pseudo code for the Barber routine Clipaway() and the Customer routine Cutmyhair() such that each customer is given a customized haircut and each customer pays for the Barber’s service.
The Sleeping Barber Problem This works for a shop that holds a total of N customers with one barber. If a customer cannot enter the shop, the customer will be forced into the street. Start the Barber before the customers.
Shared variables:
const int num_chairs = N; // number of patrons allowed in shop at a single instant
semaphore customer = 0, // customer semaphore
barber = 0, // barber’s semaphore
mutex = 1; // for mutual exclusion to a shared area
int num_waiting = 0; // # of customers that are waiting
Customer( )
{
// Customer begins by trying to get into the shop. acquire(mutex); // I am trying for a chair in the waiting area but must get into the
// critical section . .
if (num_waiting < num_chairs)
{ // If a chair is available.
num_waiting++; // I am a (one more) waiting customer.
release(customer); // Hey Barber! I want my hair cut! Wake up!
release(mutex); // Release my hold on mutual exclusion.
acquire(barber); // I will wait until the barber is ready for me.
Cutmyhair( ); // Showtime . . . its makeover magic!
}
else
{
release(mutex); // Release my hold on mutual exclusion – no chairs – I’m
// going home!
}
}
Barber( )
{
While (TRUE)
{
acquire(customer); // Catch up on my sleep if no customers are waiting.
// Who dare wake me? Oh, this is my job!
acquire(mutex); // Mutual exclusion to grab the next customer . . .
num_waiting--; // . . . who won't be waiting anymore
release(barber); // . . . as soon as I wake the poor soul up.
release(mutex); // I am feeling very creative – hmmm, what is in style today?
Clipaway( ); // This is where I do my best work, ooops – ah nothing a hat won’t
// fix.
}
}
--------------------------------------------------------------------------------
Please write in c++
#define WAITING_CHAIRS 16
#define NUMBER_OF_BARBERS 4
struct
waitingChair {
int
allotedBarber;
Semaphore
*customerWait;
waitingChair()
{
//
No barber is alloted initially.
allottedBarber
= -1;
customerWait
=
new
Semaphore(0);
}
};
struct
cuttingChair {
// customer's head to
cut hair.
void
*customerHead;
Semaphore
*startHairCut;
Semaphore
*endHairCut;
cuttingChair()
{
customerHead
= NULL;
startHairCut
=
new
Semaphore(0);
endHairCut
=
new
Semaphore(0);
}
};
class
SleepingBarbers {
public
:
SleepingBarbers();
~SleepingBarbers();
void
customerRoutine();
void
barberRoutine(
IN
int
barberId
);
private
:
queue
m_emptyChairs;
queue
m_occupiedChairs;
cuttingChair
m_baberChairs[NUMBER_OF_BARBERS];
Mutex
*m_emptyChairMutex;
ConditionMutex
*m_occupiedChairCondMutex;
};
SleepingBarbers::SleepingBarbers()
{
for
(
int
i=0; i<WAITING_CHAIRS; i++)
{
m_emptyChairs.push(
new
waitingChair());
}
m_emptyChairMutex
=
new
Mutex();
m_occupiedChairCondMutex
=
new
ConditionMutex();
}
void
SleepingBarbers::customerRoutine()
{
m_emptyChairMutex->lock();
//
Check if there is empty chair available.
if
(m_emptyChairs.empty()) {
//
Leave the shop, before leaving unlock the mutex.
m_emptyChairMutex->unlock();
return
;
}
//
Grab an empty chair.
waitingChair
*myChair = m_emptyChairs.front();
m_emptyChairs.pop();
m_emptyChairMutex->unlock();
// Announce barbers
that you have grabbed a chair.
m_occupiedChairCondMutex->lock();
m_occupiedChairs.push(myChair);
//
wakeup sleeping barber (if any).
m_occupiedChairCondMutex->
signal
();
m_occupiedChairCondMutex->unlock();
// Go to sleep, till
one barber is assigned to you.
myChair->customerWait->wait();
// A barber is ready
to cut your hair.
// Find out the
barber assigned to you.
int
myBarber = myChair->allottedBarber;
// Empty your
occupied chair.
m_emptyChairMutex->lock();
myChair->allottedBarber
= -1;
m_emptyChairs.push(myChair);
m_emptyChairMutex->unlock();
// Make your head
available for the barber.
m_baberChairs[myBarber].customerHead
= makeYourHeadAvilable();
// Request the barber
to start hair cut.
m_baberChairs[myBarber].startHairCut->post();
// Wait till the
barber is done with your hair cut.
m_baberChairs[myBarber].endHairCut->wait();
}
// m threads are initialized to run this
routine.
void
SleepingBarbers::barberRoutine(
IN
int
barberId
)
{
while
(
true
) {
m_occupiedChairCondMutex->lock();
//
Sleep till customer is available for haircut.
while
(m_occupiedChairs.empty()) {
m_occupiedChairCondMutex->wait();
}
waitingChair
*nextCustomer = m_occupiedChairs.front();
m_occupiedChairs.pop();
m_occupiedChairCondMutex->unlock();
//
Inform customer that you are ready to cut hair.
nextCustomer->allottedBarber
= barberId;
nextCustomer->customerWait->post();
//
Wait till customer is ready for hair cut.
m_baberChairs[barberId].startHairCut->wait();
//
Perform hair cut.
performHarCut(m_baberChairs[barberId].customerHead);
//
Inform the customer hair cut is done.
m_baberChairs[barberId].endHairCut->post();
}
}