Question

In: Computer Science

produce the pseudo code for the Barber routine Clipaway() and the Customer routine Cutmyhair() such that...

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++

Solutions

Expert Solution

#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();

    }

}


Related Solutions

A customer in a grocery store is purchasing three items. Write the pseudo code that will:...
A customer in a grocery store is purchasing three items. Write the pseudo code that will: • Ask the user to enter the name of the first item purchased. Then ask the user to enter the cost of the first item purchased. Make your program user friendly. If the user says the first item purchased is milk, then ask: “What is the cost of milk.” [This should work no matter what item is entered by the user. I might buy...
1. Convert the following pseudo code routine into MIPS assembler language: - Set register $t0 =...
1. Convert the following pseudo code routine into MIPS assembler language: - Set register $t0 = 1 - In four separate instructions add 2, then 3, then 4, then 5 into register $t0 $t0 = $t0 + 2 + 3 + 4 + 5 - Copy the result from register $t0 into register $t1 2. Convert the following into MIPS assembler language: - Set register $t0 = 0 - Initialize the register $t1 = 10 - Use register $t2 as...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
    It takes a barber 15 minutes to serve one customer.   a. What is...
    It takes a barber 15 minutes to serve one customer.   a. What is the capacity of the barber expressed in customers per hour?   customers per hour         b. Assuming the demand for the barber is 2 customers per hour, what is the flow rate?   customers per hour         c. Assuming the demand for the barber is 2 customers per hour, what is the utilization?   percent      ...
In pseudo-code, design an algorithm for finding baking a cake.
In pseudo-code, design an algorithm for finding baking a cake.
can someone translate this pseudo code to actual c++ code while (not the end of the...
can someone translate this pseudo code to actual c++ code while (not the end of the input) If the next input is a number read it and push it on the stack else If the next input is an operator, read it pop 2 operands off of the stack apply the operator push the result onto the stack When you reach the end of the input: if there is one number on the stack, print it else error
Assume that customer arrivals at a barber shop are random and independent of one another, and...
Assume that customer arrivals at a barber shop are random and independent of one another, and the number of customer arrivals at a barber shop and the time until the next customer arrives is independent. (a) In city A, on average, 3 customers arrive at a barber shop every hour. Using an appropriate probability distribution, (i) find the probability that at least 5 customers arrive at a barber shop every hour. (ii) A sample of 25 barber shops in city...
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Given a BST and a sum, write pseudo code to determine if the tree has a...
Given a BST and a sum, write pseudo code to determine if the tree has a root- to-leaf path such that adding up all the values along the path equals the given sum. Given the below BST and sum = 49, the array is (8, 4, 10, 1, 0, 3, 9, 15, 16). Return true, as there exist a root-to-leaf path 8− > 10− > 15− > 16 which sum is 49.
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT