A semaphore is a variable or abstract data
type that provides a simple but useful abstraction for controlling access by
multiple processes to a common resource in a parallel programming environment.
A useful way to think of a semaphore is as
a record of how many units of a particular resource are available, coupled with
operations to safely (i.e., without race conditions) adjust that record as
units are required or become free, and if necessary wait until a unit of the
resource becomes available. Semaphores are a useful tool in the prevention of
race conditions; however, their use is by no means a guarantee that a program
is free from these problems. Semaphores which allow an arbitrary resource count
are called counting semaphores, while semaphores which are restricted to the
values 0 and 1 (or locked/unlocked, unavailable/available) are called binary
semaphores.
Sleeping barber problem
The problem
The analogy is based upon a hypothetical
barber shop with one barber. The barber has one barber chair and a waiting room
with a number of chairs in it. When the barber finishes cutting a customer's
hair, he dismisses the customer and then goes to the waiting room to see if
there are other customers waiting. If there are, he brings one of them back to
the chair and cuts his hair. If there are no other customers wai
Each customer, when he arrives, looks to
see what the barber is doing. If the barber is sleeping, then the customer
wakes him up and sits in the chair. If the barber is cutting hair, then the
customer goes to the waiting room. If there is a free chair in the waiting
room, the customer sits in it and waits his turn. If there is no free chair,
then the customer leaves. Based on a naïve analysis, the above description
should ensure that the shop functions correctly, with the barber cutting the
hair of anyone who arrives until there are no more customers, and then sleeping
until the next customer arrives. In practice, there are a number of problems
that can occur that are illustrative of general scheduling problems.
The
problems are all related to the fact that the actions by both the barber and
the customer (checking the waiting room, entering the shop, taking a waiting
room chair, etc.) all take an unknown amount of time. For example, a customer
may arrive and observe that the barber is cutting hair, so he goes to the
waiting room. While he is on his way, the barber finishes the haircut he is
doing and goes to check the waiting room. Since there is no one there (the
customer not having arrived yet), he goes back to his chair and sleeps. The
barber is now waiting for a customer and the customer is waiting for the
barber. In another example, two customers may arrive at the same time when
there happens to be a single seat in the waiting room. They observe that the
barber is cutting hair, go to the waiting room, and both attempt to occupy the
single chair.
Solution
Many possible solutions are available. The
key element of each is a mutex, which ensures that only one of the participants
can change state at once. The barber must acquire this mutex exclusion before
checking for customers and release it when he begins either to sleep or cut
hair. A customer must acquire it before entering the shop and release it once
he is sitting in either a waiting room chair or the barber chair. This
eliminates both of the problems mentioned in the previous section. A number of
semaphores are also required to indicate the state of the system. For example,
one might store the number of people in the waiting room.
Implementation
The following Python code guarantees
synchronization between barber and customer and is deadlock free, but may lead
to starvation of a customer. The functions wait() (originally P, for the Dutch
proberen = try) and signal() (originally V, for the Dutch verhogen = raise) are
functions provided by the semaphores.
No comments:
Post a Comment