Wednesday, December 14, 2005

2. Sleeping Barber - traces

This is a follow-on from the post above.

Here some details about the program supplied by Tanenbaum, as well as the modification I made.

I'll put the executions, but you can find the programs in the next post.

The program unmodified - SleepingBarber.java.
In this example there are three barbers, five chairs, and the output is exactly as you would like.
A sample output:
Barber 0 is cutting hair
Customer 0 is getting his hair cut
Barber 1 is cutting hair
Customer 1 is getting his hair cut
Barber 2 is cutting hair
Customer 2 is getting his hair cut
Barber 0 is cutting hair
Customer 3 is getting his hair cut
Barber 1 is cutting hair
Customer 4 is getting his hair cut
Barber 2 is cutting hair
Customer 5 is getting his hair cut
Barber 0 is cutting hair
Customer 6 is getting his hair cut
and so on.

Now the program with the only change being that the customers delay before executing down(barber) - SleepingBarberD.java.
Sample output in which the lack of synchronization between customer and client is clear. What is less clear is that very early there are 12 customers in the shop but only 5 chairs and 3 barber's chairs - overcrowding! This would be a problem if the barbers were web server processes, particularly because the excess of customers may be unbounded:
Barber 0 is cutting hair
Barber 1 is cutting hair
Barber 2 is cutting hair
Barber 0 is cutting hair
Barber 1 is cutting hair
Barber 2 is cutting hair
Barber 0 is cutting hair
Barber 1 is cutting hair
Barber 2 is cutting hair
Barber 0 is cutting hair
Barber 1 is cutting hair
Barber 2 is cutting hair
Customer 0 is getting his hair cut
Barber 0 is cutting hair
Customer 1 is getting his hair cut
Barber 1 is cutting hair
Customer 2 is getting his hair cut
Barber 2 is cutting hair
Customer 3 is getting his hair cut
Customer 4 is getting his hair cut
Customer 5 is getting his hair cut
Customer 6 is getting his hair cut
Customer 7 is getting his hair cut
Barber 0 is cutting hair
Customer 8 is getting his hair cut
Barber 1 is cutting hair
Customer 9 is getting his hair cut
Barber 2 is cutting hair
Customer 10 is getting his hair cut

Labels:

0 Comments:

Post a Comment

<< Home