Sleeping barber problem

ImprimirCitar
The barber's cutting off the customer's beard.

In computer science, the sleeping barber problem is a timing problem.

The problem involves a barbershop where a barber works who has a single barber chair and several waiting chairs. When there are no customers, the barber sits in a chair and falls asleep. When a new customer arrives, he either wakes the barber or—if the barber is shaving another customer—sits in a chair (or leaves if all the chairs are occupied by waiting customers). The problem is to carry out the activity of the barber without race conditions occurring. The solution involves the use of semaphores and mutex objects to protect the critical section.

A semaphore is a protected variable (or abstract data type) that is the classical method of restricting or allowing access to shared resources (for example, a storage resource) in a multiprocessing environment. They were invented by Edsger Dijkstra and were first used in the THEOS operating system.

In electronics and concurrent programming, the error that occurs in programs or logic circuits that have not been adequately built for simultaneous execution with other processes is known as a race condition.


Implementation

  • The next pseudo-code guarantees synchronization between the barber and the customer, but it can lead to customer inanition. wait() and signal() are functions provided by the traffic light.
  • You need to:
Semáforo barberoListo = 0     // (Mutex, sólo 1 o 0) 
 Semáforo sillasAccesibles = 1 // (Mutex) Cuando sea 1, el número de sillas libres puede aumentar o disminuir
 Semáforo clientes = 0         // Número de clientes en la sala de espera
 int sillasLibres = N          // N es el número total de sillas
  • Barber function (Process/hylo-thread):
 while(true)                   // Ciclo infinito
 {
    wait(clientes)             // Espera la señal de un hilo cliente para despertar.
    wait(sillasAccesibles)     // (Ya está despierto) Espera señal para poder modificar sillasLibres.
    sillasLibres += 1          // Aumenta en uno el número de sillas libres.
    signal(barberoListo)       // El barbero está listo para cortar y manda señal al hilo cliente.
    signal(sillasAccesibles)   // Manda señal para desbloquear el acceso a sillasLibres
    // Aquí el barbero corta el pelo de un cliente (zona de código no crítico).
 }
  • Customer function (Process/hilo-thread):
 wait(sillasAccesibles)       // Espera la señal para poder acceder a sillasLibres.
 if (sillasLibres > 0)        // Si hay alguna silla libre, se sienta en una.
 {
    sillasLibres -= 1         // Decrementando el valor de sillasLibres en 1.
    signal(clientes)          // Manda señal al barbero de que hay un cliente disponible.
    signal(sillasAccesibles)  // Manda señal para desbloquear el acceso a sillasLibres.
    wait(barberoListo)        // El cliente espera a que el barbero esté listo para atenderlo.
    // Se le corta el pelo al cliente.
 }
 else                         // Si no hay sillas libres.
 {  
    signal(sillasAccesibles)  // Manda señal para desbloquear el acceso a sillasLibres.
    // El cliente se va de la barbería y no manda la señal de cliente disponible.
 }

Contenido relacionado

Real time

A real-time system is a digital system that actively interacts with an environment with known dynamics in relation to its inputs, outputs, and time...

Electric capacitor

In electricity and electronics, a capacitor or capacitor is a passive device capable of storing energy by sustaining an electric field. It consists of by a...

Hawking radiation

The Hawking radiation is radiation theoretically produced near the event horizon of a black hole and is entirely due to quantum-type effects. Hawking...
Más resultados...
Tamaño del texto:
Copiar