However, now that I'll implement this problem with multiple barbers, my head got stuck. Sem_wait(Barber) // wait in the waiting room if barber is busy Sem_post(accessSeats) // release the lock Sem_wait(accessSeats) // protects seats so only 1 thread tries to sit in a chair if that's the case Sem_post(accessSeats) // release the mutex on the chair Sem_post(Barber) // bring customer for haircut Sem_wait(accessSeats) // mutex to protect the number of available seats Sem_wait(Customers) // waits for a customer (sleeps) (although the actual code is in C, I wrote the following pseudo-code without much care for the syntax for sake of problem-understading, only using sem_wait and sem_post 1 for smoother reading) Semaphore Customers = 0 I have made a implementation of this using the following basic-idea When another customer arrives, he then must wake up the barber. If, then, there is no one in the waiting room, the barber goes to sleep. If there is someone, he cuts his hair (symbolizing some processing) and the costumer leaves. It's based on the premise that a barber (a thread) sleeps when there are no customers (each customer is a thread) in the waiting room (which is a semaphore). The Sleeping Barber Problem is a classical synchronization problem that many of you may be familiar with or at least heard of.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |