5 Haziran 2012 Salı

Uyuyan berber sorunu


Bilgisayar mühendisliğinde, uyuyan berber sorunu işletim sistemleri konusunda klasik bir sorundur. Berber ve müşteriler prosesler olarak düşünülebilir.

Gerçekleme

  • Aşağıdaki sözde kod berber ve müşteriler için ölümcül kilitlenme olmayan bir çözümdür ancak açlığa yol açabilir.
Semaphore Customers = 0
Semaphore Barber = 0
Semaphore accessSeats = 1 # mutex
int NumberOfFreeSeats = N # total number of seats

def Barber():
while true: # Run in an infinite loop.
P(Customers) # Try to acquire a customer - if none is available, go to sleep.
P(accessSeats) # I have been awakened - modify the number of available seats.
NumberOfFreeSeats++ # One chair gets free.
V(Barber) # I am ready to cut.
V(accessSeats) # Don't need the lock on the chairs anymore.
# (Cut hair here.)

def Customer():
while true: # Run in an infinite loop.
P(accessSeats) # Try to get access to the chairs.
if NumberOfFreeSeats > 0: # If there are any free seats:
NumberOfFreeSeats-- # sit down on a chair
V(Customers) # notify the barber, who's waiting until there is a customer
V(accessSeats) # don't need to lock the chairs anymore
P(Barber) # now it's this customer's turn, but wait if the barber is busy.
# (Have hair cut here.)
else: # otherwise, there are no free seats; tough luck --
V(accessSeats) # but don't forget to release the lock on the seats!
# (Leave without a haircut.)

0 Yorum:

Yorum Gönder

Kaydol: Kayıt Yorumları [Atom]

<< Ana Sayfa