Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. Sleeping barber This is a potentially useful (if convoluted) example to exercise your understanding of how to use mutexes and conditional variables. It is

image text in transcribedimage text in transcribed

3. Sleeping barber This is a potentially useful (if convoluted) example to exercise your understanding of how to use mutexes and conditional variables. It is a well-known concurrency problem. The writeup and solution are due to Mike Dahlin (who used to be on the faculty at The University of Texas at Austin). He asked this question on a midterm in 2002, Work through the problem on your own; we will post the solution next week. A shop has a barber, a barber chair, and a waiting room with NCHAIRS chairs. If there are no customers present, the barber sits in the chair and falls asleep. When a customer arrives, the customer wakes the sleeping barber. If an additional customer arrives while the barber is cutting hair, the customer sits in a waiting room chair if one is available. If no chairs are available, the customer leaves the shop. When the barber finishes cutting a customer's hair, the barber tells the customer to leave; then, if there are any customers in the waiting room, the barber announces that the next customer can sit down. Customers in the waiting room get their hair cut in FIFO order. The barber shop can be modeled as 2 shared objects: A BarberChair, with the methods napInChair(), wakeBarber(), sitInChair(), cutHair(), and tell CustomerDone(). The BarberChair must have a state variable with the following states: EMPTY, BARBER_IN_CHAIR, LONG_HAIR_CUSTOMER_IN_CHAIR, SHORT_HAIR_CUSTOMER_IN_CHAIR. Note that neither a customer or barber should sit down until the previous customer is out of the chair (state -- EMPTY). Note that cutHair() must not return until the customer is sitting in the chair (LONG_HAIR_CUSTOMER_IN_CHAIR). o Note that a customer should not get out of the chair (that is, return from sitInChair()) until the customer's hair is cut (SHORT_HAIR_CUSTOMER_IN_CHAIR). The barber should get in the chair (BARBER_IN_CHAIR) only if no customers are waiting. o You may need additional state variables. A WaitingRoom, with the methods enter and callNextCustomer(). o enter() returns WR_FULL if the waiting room is full or (immediately or eventually) returns MY_TURN when it is the caller's turn to get their hair cut o callNextCustomer() returns WR_BUSY OR WR_EMPTY depending on if there is a customer in the waiting room or not. Customers are served in FIFO order. Thus, each customer thread executes the code: Customer (WaitingRoom *wr, BarberChair *bc) { status - wr->enter(); if (status -- WR_FULL) { return; } bc->wakeBarber(); bc->sitInChair(); // Wait for chair to be EMPTY // Make state LONG_HAIR_CUSTOMER_IN_CHAIR // wait until SHORT_HAIR_CUSTOMER_IN_CHAIR // then make chair EMPTY and return return; } The barber thread executes the code: Barber (Waiting Room *wr, Barberchair *bc) { while (1) { // A barber's work is never done status = wr->callNextCustomer(); if (status == WR_EMPTY) { bc->napInChair(); // Set state to BARBER_IN_CHAIR; return with state EMPTY } bc->cutHair(); // Block until LONG_HAIR_CUSTOMER_IN_CHAIR; // Return with SHORT_HAIR_CUSTOMER_IN_CHAIR bc->tellCustomerDone(); // Return when EMPTY } } Write the code for the waiting Room class and the Barberchair class. Use locks and condition variables for synchronization. Follow the coding standards specified in Mike Dahlin's Coding Standards for Threads Programming, which you will also follow in Lab 3. Hints and requirement reminders): remember to start by asking for each method "when can thread wait?" and writing down a synchronization variable for each such situation. List the member variables of class WaitingRoom including their type, their name, and their initial value. Then write the methods for Waiting Room. List the member variables of class Barberchair including their type, their name, and their initial value. Then write the methods for BarberChair

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

6. Describe turning points in intercultural friendships.

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago