Question

In: Computer Science

Sleeping Barber Problem where there is only 1 barber in the shop for x clients function...

Sleeping Barber Problem where there is only 1 barber in the shop for x clients

function Customer()

acquire(mutex)

if num_waiting < num_chairs then

num_waiting + 1

release(customer)

release(mutex)

acquire(barber)

Cutmyhair()

else

release(mutex)

end

end

function Barber()

while not breaking

acquire(customer)

acquire(mutex)

num_waiting - 1

release(barber)

release(mutex)

Clipaway()

end while

end

QUESTION: come up with the pseudocode for the clipaway() and cutmyhair() methods so that each client pays for there haircut and the barber gives each client a customized haircut.

Any language.

thank you in advance !!!

Solutions

Expert Solution

The algorithm for the sleeping barber problem in process synchronisation can be completed as follows:

Semaphore Customers = 0; 
Semaphore Barber = 0; 
Mutex Seats = 1; 
int FreeSeats = N; 
  
Barber { 
      while(true) { 
            /* waits for a customer (sleeps). */
            down(Customers); 
  
            /* mutex to protect the number of available seats.*/
            down(Seats); 
  
            /* a chair gets free.*/
            FreeSeats++; 
             
            /* bring customer for haircut.*/
            up(Barber); 
             
            /* release the mutex on the chair.*/
            up(Seats); 
            /* barber is cutting hair.*/
      } 
} 
  
Customer { 
      while(true) { 
            /* protects seats so only 1 customer tries to sit 
               in a chair if that's the case.*/
            down(Seats); //This line should not be here. 
            if(FreeSeats > 0) { 
                   
                  /* sitting down.*/
                  FreeSeats--; 
                   
                  /* notify the barber. */
                  up(Customers); 
                   
                  /* release the lock */
                  up(Seats); 
                   
                  /* wait in the waiting room if barber is busy. */
                  down(Barber); 
                  // customer is having hair cut 
            } else { 
                  /* release the lock */
                  up(Seats); 
                  // customer leaves 
            } 
      } 
} 

Also, here is the running code of the problem in C++.

#define CHAIRS 5 // number of chairs for waiting customers
typedef int semaphore;
semaphore customers = 0; // number of waiting customers
semaphore barbers = 0; // number of barbers waiting for customers
semaphore mutex = 1; // for mutual exclusion
int waiting = 0; // customers are waiting not being haircut

void Barber(void)
{
    while(true)
    {
        down(customers); // go to sleep if number of customers is down to 0
        down(mutex); // acquire access to 'waiting'
        waiting--; //decrement count of waiting customers
        up(barbers); // one barber is now ready to cut hair
        up(mutex); // release 'waiting'
        cut_hair(); //cut hair , non-CS
    }
}

void customer(void)
{
    down(mutex); // enters CS
    if(waiting < CHAIRS)
    {
        waiting++; // increment count of waiting customers
        up(customers); // wake up barber if necessary
        up(mutex); // release access to 'waiting'
        down(barbers); // wait if no free barber
        get_haircut(); // non-CS
    }
    
    else
    {
        up(mutex); // shop is full, do not wait
    }
}

Please upvote if you find the answer helpful.


Related Solutions

Problem 15-9 (Algorithmic) Marty's Barber Shop has one barber. Customers have an arrival rate of 2.1...
Problem 15-9 (Algorithmic) Marty's Barber Shop has one barber. Customers have an arrival rate of 2.1 customers per hour, and haircuts are given with a service rate of 5 per hour. Use the Poisson arrivals and exponential service times model to answer the following questions: What is the probability that no units are in the system? If required, round your answer to four decimal places. P0 = What is the probability that one customer is receiving a haircut and no...
Problem 15-9 (Algorithmic) Marty's Barber Shop has one barber. Customers have an arrival rate of 2.1...
Problem 15-9 (Algorithmic) Marty's Barber Shop has one barber. Customers have an arrival rate of 2.1 customers per hour, and haircuts are given with a service rate of 4 per hour. Use the Poisson arrivals and exponential service times model to answer the following questions: What is the probability that no units are in the system? If required, round your answer to four decimal places. P0 = ????? What is the probability that one customer is receiving a haircut and...
Ned's New Wave Barber Shop specializes in modern unisex haircuts. The only service available at Ned's...
Ned's New Wave Barber Shop specializes in modern unisex haircuts. The only service available at Ned's is a “20-minute” haircut for which the customer is charged $10. The shop has five (5) barbers. (Ned does not work in the shop and, as owner/entrepreneur, he takes no salary.) Each barber is paid an annual salary of $18,000. All equipment including store fixtures and barbering equipment is leased on an annual basis at $4,500 per year. Building space is leased at the...
Define a function ?∶ ℝ→ℝ by ?(?)={?+1,[?] ?? ??? ?−1,[?]?? ???? where [x] is the integer...
Define a function ?∶ ℝ→ℝ by ?(?)={?+1,[?] ?? ??? ?−1,[?]?? ???? where [x] is the integer part function. Is ? injective? (b) Verify if the following function is bijective. If it is bijective, determine its inverse. ?∶ ℝ\{5/4}→ℝ\{9/4} , ?(?)=(9∙?)/(4∙?−5)
For the function f(x) = x(x+1) find the exact formula for f′(x). Use only the definition...
For the function f(x) = x(x+1) find the exact formula for f′(x). Use only the definition of the derivative.
Given that h(x) = x.sinx . Find the root of the function h(x) = 1, where...
Given that h(x) = x.sinx . Find the root of the function h(x) = 1, where x is between [0, 2] using substitution method.
Solve the following differential equation: y''+e^{-y}=0 where y is only a function of x.
Solve the following differential equation: y''+e^{-y}=0 where y is only a function of x.
Find the extreme values of the function and where they occur. y= (x+1)/(x^2+3x+3)
Find the extreme values of the function and where they occur. y= (x+1)/(x^2+3x+3)
1. Use the derivative function, f'(x)f′(x), to determine where the function f(x)=−2x^2+14x−8 is increasing. 2.Use the...
1. Use the derivative function, f'(x)f′(x), to determine where the function f(x)=−2x^2+14x−8 is increasing. 2.Use the derivative function f'(x)f′(x) to determine where the function f(x)=2x^3−27x^2+108x+13 is increasing.   3.Use the derivative function f'(x)f′(x) to determine where the function f(x)=2x^3−27x^2+108x−12 is decreasing. 4.Find each value of the function f(x)=−x^3+12x+9 where the line tangent to the graph is horizontal. x=
A random variable Y is a function of random variable X, where y=x^2 and fx(x)=(x+1)/2 from...
A random variable Y is a function of random variable X, where y=x^2 and fx(x)=(x+1)/2 from -1 to 1 and =0 elsewhere. Determine fy(y). In this problem, there are two x values for every y value, which means x=T^-1(y)= +y^0.5 and -y^0.5. Be sure you account for both of these. Ans: fy(y)=0.5y^-0.5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT