[Typical operating system problems] Analysis of sleep barber Problems

Source: Internet
Author: User

2. Barber problem: a barber shop has an entrance and an exit. In the barber shop, there is a standing area where five customers can stand, four single-person sofas, three hairdressers and their specialized hairdressing tools, and a cashier. New customers are waiting on the sofa. When there is no sofa, they can wait in the seat area. when the seat area is full, they can only wait outside the entrance. A hairdresser can engage in three activities: haircut, cashier and rest. 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 the barber shop. Wait for the sofa space (semaphore sofa) in the seat area. If the sofa is full, it will enter the blocking waiting queue until there is a blank space, customers with the longest waiting time in the standing area leave the standing area (stand_capacity ). Sit down on the sofa and wait for the barber_chair. If the chair is full, enter the blocked waiting queue until there is a vacant space. On the sofa, the customer with the longest waiting time will leave the sofa ). Sit down in the chair and release the prepared signal (customer_ready) to get the number of the barber (0 ~ 1 ). Wait until the barber's haircut ends (finished [barber_number]). Pay (payment) before leaving the chair, wait for the receipt (receipt), and leave the chair (leave_barberchair ). Finally, leave the barber shop.
Note the following points:
A) first, there are several mutex processing areas, including: Entering the seat area, entering the sofa, entering the chair, and paying.
B) Use barber_chair to ensure that only one customer can be added to a chair. However, this is not enough because the baber_chair alone cannot guarantee that another customer will not sit in the chair before leaving the chair. Therefore, the semaphore leave_barberchair is added to let the customer leave the chair, this signal is released, and the barber receives this signal before releasing barber_chair to wait for the next customer.
C) In the process of haircutting, you must make sure 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 the barber who gave his haircut. In this way, when the Barber Of this number releases the corresponding finished [I] signal, the customer has 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. The pseudo code is implemented by the following mechanism: that is, the customer sends a payment signal before releasing the signal from the rocking chair. In this way, the barber cannot receive the signal from the customer to leave the chair. Instead, he cannot enter the next cycle to serve the next customer, but he can only go to the receiving desk to collect the money. It is not until the customer receives the receipt that the signal of leaving the chair is released, and the barber is asked to release the signal of the chair, so that the next waiting customer can 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 (semaphores customer_ready) and start the haircut. After the haircut ends, the finished [I] signal is released. Wait for the customer to leave the cashier's chair (leave_barberchair), release the free signal (barber_chair), and wait 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 in the reading chair and the customer sits in the reading chair. It sends a signal to the barber. mutex waits for the barber to be idle and performs the haircut or receiving operation. The barber performs the haircut or receiving ends, in idle state, mutex1 enters the queue or leaves the queue waiting for or leaves to end. The release signal is finished [I]. The customer waits for the corresponding number to end the barber's haircut; the barber's haircut to end, release signal leave_barberchair barber waits for the customer to leave the chair. After the payment is completed, the customer receives the receipt and leaves the chair. Release signal: Payment cashier waits for the customer to pay for the customer, send a signal. The customer waits for the cashier to receive the receipt. The cashier ends the receipt, issues the receipt, and releases the 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 [I]); // end of 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 consulted, including:
(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 payment for the customer because: after receiving the leave_barberchair signal, the barber who sent the haircut to the customer releases the barber_chair signal. When another customer sat down in the chair, the barber may have a haircut for the other customer, the payment is not collected for the customer who just sent the payment. To solve this problem, you must complete the payment before releasing the leave_barberchair signal. In this way, the barber cannot go into the next round to serve other customers, but can only collect money from the cashier.
(2) This algorithm is used to number a barber. When a customer sits in a chair and obtains the number of the barber, when the barber ends the haircut, the signal is released, payment and other operations can only be performed if the customer receives a haircut ending signal from the barber who has a haircut for him. To avoid this problem, if only one finished semaphore is used, it is easy for other hairdressers to release the finished signal, the customer with a haircut is rushed to make the payment, but the manager's customer is congested on the chair. Of course, the number can also be assigned to the customer so that the barber can obtain the number of the customer for his haircut, but this will limit the number of customers, because the finished [] array 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.