Enhanced versions of hairdressers

Source: Internet
Author: User
Enhanced versions of hairdressers

2. Barber problem: a barber shop has an entrance and an exit. The barber shop has a seat for five customers.
Area, 4 single sofa, 3 hairdressers and their special hair tools, and a cashier. New customers sit on the couch and wait.
Waiting; when there is no empty sofa, you can wait in the station area; when the station area is full, you can only wait outside the entrance. Hairdressers can engage in management
Three activities are available: sending, receiving, and resting. Barber Shop activities meet the following conditions:
1) The barber at rest is sitting in his own lounge chair and will not occupy the customer's sofa;
2) hairdressers who handle resting conditions can cut hair for customers who have waited for the longest time on the sofa;
3) The length of the haircut is determined by the barber;
4) customers with the longest waiting time in the standing area can sit on their free hairdressers;
5) at most one barber can be at a cashier at any time.
Use the semaphores or processes to implement hairdressers and customer processes.
Principle:
(1) Customer process:
First, check whether the seat area is full (stand_capacity). If it is full, choose to leave. Otherwise, enter the seat area, that is, enter
Barber shop. Wait for the sofa space in the seat area. If the sofa is full, the waiting queue is blocked,
Wait until there is a blank space. The customer with the longest waiting time in the standing area leaves the standing area (stand_capacity ). Sit in Sand
Send up, wait for the Chair (barber_chair), if the Chair is full, enter the blocking wait queue until it appears
Vacant Space, waiting for the longest customer to leave the sofa on the sofa ). Sit down and release
Prepare the signal (customer_ready) and obtain the number of the barber (0 ~ 1 ). Waiting for the barber
Finished [barber_number]. Pay (payment) before leaving the chair and wait for the receipt
(Receipt), leave the chair (leave_barberchair ). Finally, leave the barber shop.
Note the following points:
A) first, there are several mutually exclusive processes, including: Entering the seat area, entering the sofa, and entering the chair.
And payment.
B) Use barber_chair to ensure that only one customer can be added to a chair. However, this is not enough because
Baber_chair cannot guarantee that a customer will not sit in the chair before leaving the chair,
Therefore, the semaphore leave_barberchair is added to allow the customer to release the signal and get a haircut after leaving the chair.
After receiving the signal, the engineer releases barber_chair and waits for the next customer.
C) In the process of haircutting, you must ensure that you have finished the haircut before you can make the following payment and leave the chair.
. This mechanism is implemented through the customer process to obtain the number of hairdressers for his haircut. In this way, when
When the Barber Of this number releases the corresponding finished signal, the customer will have a haircut.
D) hairdressers use mutex semaphores to ensure that each of them performs only one operation (haircut or receipt) at the same time ).
E) in order to ensure that the customer can make payment immediately after the haircut is completed, the barber of the customer's haircut should be handled.
After sending the mail, you can immediately go to the cashier to collect the money instead of serving the next customer. In the pseudo code, the following mechanism is used to implement
Now: the customer sends a payment signal before releasing the signal of leaving the chair. In this way, the barber cannot get the customer's
The signal that leaves the reading chair cannot enter the next cycle to serve the next customer, but can only enter the receiving desk
Operation. It is not until the customer receives the receipt that the signal of leaving the chair is released, and the barber is allowed to release the chair.
The signal of the Chair enables the next waiting customer to sit in the chair.
(2) barber Process
First, the number of the barber is pushed into the queue for the customer to extract. Wait for the customer to sit in the chair (semaphore
Customer_ready), start the haircut, and release the finished signal after the haircut ends ). Waiting for customers
Leave the lounge chair (leave_barberchair) (during which you will go to the cashier for collection activities) and release the free letter
Number (barber_chair), waiting for the next customer to sit up.
(3) Cash Process
Wait for the customer to pay (payment), execute the collection operation, the collection operation ends, and the payment receipt (receipt ).
Semaphore summary table:
Semaphores wait Signal
Stand_capacity the customer is waiting to enter the barber shop and the customer leaves the seat area.
Sofa customers are waiting to sit on the couch. Customers are leaving the couch.
Barber_chair customer waits for the empty chair barber to release the empty chair
The customer_ready barber waits until a customer sits
To chair
The customer sat down in the chair and gave the barber
Send signal
Mutex waits for the barber to be idle and performs a haircut or
Collection operations
The barber performs a haircut or the receipt ends,
Enter idle status
Mutex1 enters or leaves the queue waiting for the completion of the queue or exit, releasing the signal
Finished: the customer waits for the corresponding number of hairdressers.
End of sending
The barber ends his hair and releases the signal.
Leave_barberchair the barber waits for the customer to leave the chair. The customer has paid and received the receipt and left.
Rocking Chair release Signal
Payment cashier waits for the customer to pay for the customer and sends a signal
The receipt customer waits for the cashier to receive the receipt, issues the receipt, and ends the cashier's receipt,
Release Signal
Pseudo code:
Semaphore stand_capacity = 5;
Semaphore sofa = 4;
Semaphore barber_chair = 3;
Semaphore customer_ready = 0;
Semaphore mutex = 3;
Semaphore mutex1 = 1;
Semaphore finished [3] = {0, 0 };
Semaphore leave_barberchair = 0;
Semaphore payment = 0;
Semaphore receept = 0;
Void customer ()
{
Int barber_number;
Wait (stand_capacity); // wait to enter the barber shop
Enter_room (); // enter the barber shop
Wait (SOFA); // wait for the sofa
Leave_stand_section (); // leave the seat area
Signal (stand_capacity );
Sit_on_sofa (); // sit on the sofa
Wait (barber_chair); // wait for the lounge chair
Get_up_sofa (); // exit the sofa
Signal (SOFA );
Wait (mutex1 );
Sit_on_barberchair (); // sit down in the chair
Signal (customer_ready );
Barber_number = dequeue (); // get the barber number
Signal (mutex1 );
Wait (finished [barber_number]); // wait until the haircut ends.
Pay (); // payment
Signal (payment); // payment
Wait (receipt); // wait for the receipt
Get_up_barberchair (); // leave the chair
Signal (leave_barberchair); // sends the exit Signal
Exit_shop (); // leave the barber shop
}
Void Barber (int I)
{
While (true)
{
Wait (mutex1 );
Enqueue (I); // Add the number of the barber to the queue
Signal (mutex1 );
Wait (customer_ready); // wait for the customer to prepare
Wait (mutex );
Cut_hair (); // haircut
Signal (mutex );
Signal (finished); // end the haircut
Wait (leave_barberchair); // wait for the customer to exit the ticket
Signal (barber_chair); // release the barber_chair Signal
}
}
Void cash () // cash register
{
While (true)
{
Wait (payment); // wait for the customer to pay
Wait (mutex); // atomic operation
Get_pay (); // accept payment
Give_receipt (); // give the customer a receipt
Signal (mutex );
Signal (receept); // After the cashier is complete, release the signal
}
}
Analysis:
During the analysis of the problem, a number of problems occurred. The problem was concealed and serious only after relevant materials were read.
Mainly include:
(1) In the customer process, if the payment is performed after the leave_barberchair signal is released
It is easy to cause no cashier to collect the money for the customer because the barber who haircut the customer receives
After the leave_barberchair signal is sent, the barber_chair signal is released. Another customer sat down in the chair,
The barber may have a haircut for the other customer, but has not paid for the customer who just finished the haircut. To solve this problem,
The payment is completed before the leave_barberchair signal is released. In this way, the barber cannot access
In the next round, the customer can only collect money from the cashier.
(2) Ben Algorithm The barber number is assigned to the customer. When the customer sits in a chair, the number of the barber is also obtained,
In this way, when the barber's hair ends, the signal is released, and the customer only receives the hair ending signal from the barber who haircut it.
Payment and other operations. To avoid such errors, if only one finished message is used
If the number is used, it is easy to see other hairdressers release the finished signal after the haircut is completed.
The customer rushed to make the payment, but the customer who sent the manager was blocked on the chair. Of course, you can also compile for the customer
Ask the barber to get the number of the customer who has his haircut, but this will limit the number of customers, because finished []
Arrays cannot be infinite. The barber number requires only three elements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.