0% found this document useful (0 votes)
5 views

Sem Exer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Sem Exer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

The Sleeping Barber

Customers who need a haircut enter the waiting room. If the


room is full, the customer comes back later. If the barber is
busy but there is a vacant chair the customer takes a seat. If
the waiting room is empty and the barber is daydreaming the
customer sits in the barber chair and wakes the barber.
When the barber finishes cutting a customer’s hair, the
barber fetches another customer from the waiting room. If
the waiting room is empty the barber daydreams.

D. Gachet
The Sleeping Barber
Semaphore mutex=1;
semáforo para exclusion mutua
Semaphore customers=0, cutting=0, barber=0;
clientes, si se esta cortando el pelo o no, si está dormido o no
int waiting=0, numChairs= ..;

process Customer[i=1..M]
process SleepingBarber
while (true) {
while (true) {
wait(mutex);
ver si hay sitio wait(customers);no hay clientes, me duermo
if (waiting<numChairs){
wait(mutex);
waiting++; al haber un customer más, wait aumenta quito la ocupacion de una silla porque voy a
waiting--;atender a un cliente
signal(customers);hay un customer más
signal(barber);
signal(mutex);
signal(mutex);
wait(barber); ahora me toca que me llegue la vez
wait(cutting);
signal(cutting);
cuando sea mi turno, ya me podrá cortar el pelo }
}
end SleepingBarber;
else up(mutex);
} si todas las sillas estaban ocupadas, no puedo entrar

end Customer;

D. Gachet
The Sleeping Barber

This example illustrates the use of semaphores for


both mutual exclusion and condition synchronization.
The customers and barber use semaphores to control
each other’s flow through the barber shop. This
interaction is an example of a client-server
relationship. The customer and barber rendezvous to
interact: each waits at a certain point for the other
to arrive.

D. Gachet
Preguntas del tema 2 del examen

Exercises with semaphores

El semaforo se inicializa a 0.
B se bloquea, hace el wait, si B hace el signal dsps de opA()

D. Gache0
Exercises with semaphores

D. Gachet
Exercises with semaphores

D. Gachet
Exercises with semaphores

D. Gachet
Exercises with semaphores
Exercises with semaphores

D. Gachet
Exercises with semaphores

D. Gachet
Exercises with semaphores

D. Gachet
Exercises with semaphores

D. Gachet
Exercises with semaphores

D. Gache0
Exercises with semaphores

Which are the possible values for X ?


Send() == signal()

D. Gachet

You might also like