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...
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 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...
Use pseudo code to plan a programming solution for the following: A GUI interface to ensure...
Use pseudo code to plan a programming solution for the following: A GUI interface to ensure a user is old enough to play a game. See additional resources above for additional resources. Properly formatted prompts to input name, address, phone number, and age. Instructions to ensure that the information is displayed back to the user. Also, the user must be 21. So, include instructions to display an appropriate message about whether or not the user can continue. Instructions that will...
Submit a document with methods for an automobile class, and pseudo code indicating functionality of each...
Submit a document with methods for an automobile class, and pseudo code indicating functionality of each method. Example: public String RemoveVehicle(String autoMake, String autoModel, String autoColor, int autoYear) If values entered match values stored in private variables remove vehicle information else return message indicating mismatch
Develop the pseudo code for, desk check and test the following programs. Allow the user to...
Develop the pseudo code for, desk check and test the following programs. Allow the user to provide the number of elements, and initialise the array according to their input. Use functions where appropriate to ensure modularity. 1. Find the average of a list of numbers 2. Find the smallest element in a list of numbers
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT