* cut hair for worktime seconds (really just call sleep()) */ * generate random number, worktime, from 1-4 seconds for length of haircut. * signal to client that barber is ready to cut their hair (sem_barber) */ * increment number of chairs available */ * wait for mutex to access chair count (chair_mutex) */ * wait for a client to become available (sem_client) */ Here is a skeleton for the barber function: The haircuts will take different amounts of time (randomly distributed). The barber thread will wait for a client, give a haircut and repeat. All the other threads will be terminated when the parent thread exits (using exit() but not pthread_exit()). Once the main program has created all the threads, it will sleep for the number of seconds specified on the command line.įinally, when the main program wakes back up, it should exit. You will need to write a barber and a client function for the threads to begin execution in. The function calls are pthread_attr_init(), pthread_create(), pthread_join(), and pthread_exit(). When creating the threads, you can use the pthread functions detailed in figure 4.9 on page 161. The sem_barber semaphore should be initialized to 0 since the barber is not ready for a customer initially.
The sem_client semaphore should be initialized to 0 since there are no clients initially ready. The chairs_mutex should be initialized to 1 since no process is in its critical section initially (meaning that the first process to call wait() will succeed). The num_chairs variable should be initialized to the value given on the command line (the number of chairs total = n). Name your counter numchairs and your semaphores chairs_mutex, sem_client, and sem_barber. The maximum amount of time a client can wait between attempts to get a haircut.Įssentially, we need to keep track of how many chairs are available (using a chair count and a mutex to protect it), how many clients are ready and waiting (counting semaphore), and whether the barber is ready (mutex semaphore). The number of chairs in the waiting room.Ĥ. The length of time the program should run.ģ. Your main program will spawn off one barber thread and x client threads, which will shares resources (chairs) using semaphores for synchronization.ġ. If there are no chairs available when a client arrives, he leaves. If the barber is busy, and there is a chair available, the client sits and waits. If a client comes in, and the barber is asleep, the client wakes the barber and gets a haircut. If there are no clients in his shop, the barber goes to sleep. There are x clients and one barber in the system. The sleeping barber synchronization problem is this: a barbershop consists of a waiting room with n-1 chairs, and a barber room with one chair (for a total of n chairs). In which case I'd have this complexity? By several barbers going to the waiting room to see if there is anyone there and then taking (more than one barber) a single customer? Would I need an extra semaphore here?ġ sem_wait() locks the semaphore.Write a program which implements a solution to the sleeping barber problem I went on Wikipedia to see if I could find something about it, but the only thing that I found there was thisĪ multiple sleeping barbers problem has the additional complexity of coordinating several barbers among the waiting customers.Īnd I couldn't figure this out by myself. 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) I have made a implementation of this using the following basic-idea (pseudo-code, only using sem_wait and sem_post 1 for smooth reading) Semaphore Customers = 0 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.