##### The Barbershop Problem

The Barbershop

Assume that one barber, one barber chair and ‘n’ number of waiting chairs in the barber shop. The barber can sleep on the barber chair when there is customer in the shop. Now when a customer arrives and barber is sleeping, then he wakes him up and sit in the chair. If barber is already doing work then customer sits in the waiting chair, if waiting chair is available otherwise customer leaves.

 Shared Data: Semaphore customers = 0 ; Semaphore barbers = 0 ; Semaphore mutex = 1 ; Int waiting = 0 ; Barber Process: Barber () { While (true) { Wait (customers) ; Wait (mutex) ; waiting - - ; Signal (barbers) ; Signal (mutex) ; Cut_hair () ; } } Customer Process: Customer () { Wait (mutex) ; If (waiting < chairs) { Waiting + + ; Signal (customers) ; Signal (mutex) ; Wait (barbers) ; Get_haircut () ; } Else { Signal (mutex) ;} }

Solutions Based on Programming Language

Monitor is a programming language construct that supports synchronized access to data.  Monitors have advantages over semaphores of making parallel programming easier and less error prone. But some languages do not support monitors, e.g., Java, Visual Basic, C# and Ada.

There is another solution of Producer Consumer problem using Monitor instead of Semaphores.

 Shared Data: Item buffer[n] ; Int count = 0 ; Condition variable full, empty ; Producer Process: Producer () { While (count = = n) {wait(emp)} ; // Produce item ; Count ++ ; Signal (full) ; } Consumer Process: Consumer () { While (count = = 0) {wait(full)} ; // Remove item ; Count - - ; Signal (empty) }