Sei sulla pagina 1di 8

SLEEPING BARBER

PROBLEM

A CLASSICINTER-PROCESS COMMUNICATION
&SYNCHRONIZATIONPROBLEM
BETWEEN MULTIPLEOPERATING
SYSTEMPROCESSES

HYPOTHETICAL BARBER SHOP CONDITIONS


Barber shop - one barber; many customers.
A waiting room - with N chairs.
One customer is served at a time.
Barber,
Barber sleeps when no customers are waiting.
Customer leaves if shop is full.

THE SLEEPING BARBER PROBLEM


(CONTD)
When barber finishes cutting a customer's hair, he checks if
there are any waiting customers:
Any waiting customers : barber cuts his hair.
No customers waiting : barber sleeps.

When each customer arrives, he checks what the barber is


doing:
Barber sleeping : customer wakes him up.
Barber cutting hair: customer goes to waiting room.
A free chair : customer sits in it.
No free chair : customer leaves.
4

PROBLEMS
Both barber and customer waits for each other :
o customer arrives - barber is busy
o Customer on his way to waiting room, barber finishes hair cut.
o Barber goes to sleep, Customer waits.

Customers competing for the same chair:


o Two customers single chair.
o Both attempt to occupy the single chair.

SOLUTION
Mutex :
Ensures that only one of the participants can change
state at once.
The barber must acquire this mutex 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 chair or the
barber chair.
6

PSEUDOCODE

THANK YOU..

Potrebbero piacerti anche