Open In App
Related Articles

FIFO Barbershop in Process synchronization

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Overview :
In process synchronization, there exists a Sleeping Barber problem, as discussed here. But in the above solution, there is no guarantee that the customers are served in the order they arrive in the barbershop, i.e. there is no guarantee that the customers enter a FIFO manner(First-in First-out). In the solution discussed above.

  • Up to n customers can arrive, then signal customer and wait for the barber to be idle or awake.
  • Once the barber is idle or awaken, any of the customers may proceed.

Pre-requisite –
Sleeping Barber problem in Process Synchronization

Problem Statement : 
We want a solution, in which the customers are served in order of their arrival inside the shop.

Solution –
To maintain the order of the customers, we should implement a data structure, known as Queue, which follows the First-in First-out principle. Instead of using a single Semaphore for customers, a list of Semaphore will be used. From now onward, we will refer a customer as a thread. So for threads, a list of Semaphore will be used, named as a queue. The intuition behind this approach is that:

  1. As each thread enters the barbershop, it creates a thread and puts it in the queue. 
  2. Instead of waiting for the barber to be idle, each thread waits on its own semaphore.
  3. Whenever a barber is either idle or awaken, he removes the thread from the queue, and signals it to proceed to the chair.

Note –
The barber has to get the mutex to access the queue. Hence, the mutex will use some Semaphore to control the thread’s access to the queue, and the barber processes the queue.

Algorithm for the FIFO Barbershop Problem :

Semaphore customer = 0;
Semaphore mutex = 1;
Semaphore barberDone = 0;
Semaphore customerDone = 0;
customer = 0;
Queue queue;

/* Number of chairs available */
n = 4

Customer
{
/* Protects the seat, to mark the present customer */
    Semaphore self = 0;
    
    mutex.wait();
    
    if(customer == n){
    mutex.signal();
          
/* Invoke the barber */
    customer += 1;          
            
/* Adds the thread inside the queue */
    queue.push(self);
    
    mutex.signal();
    customer.signal();
    self.wait();
    
/* gets the hair cut */
    customerDone.signal();
    barberDone.wait();
    
/* Decrements the number of thread by one */
    mutex . wait ();
    customers -= 1;
    mutex . signal ();
}


Barber{
/* To store the current thread*/
    Semaphore curr;
    
    customer.wait();
    mutex.wait();
    
/* Gets the current thread*/
    curr = queue.pop();
    mutex.signal();
    
    curr.signal();
    
    /* gets the hair cut */
    customerDone.waitl();
    barberDone.signal();

}

Note –
The self and curr are nothing but are semaphores referring to the current thread.

 

A FIFO (First-In-First-Out) barbershop is a classic problem in process synchronization that simulates a barbershop with one barber and multiple customers. In this problem, there is a waiting room with a fixed number of chairs and a barber chair. Customers arrive and either sit in an empty chair in the waiting room or leave if there are no chairs available. If the barber is free, he takes the next customer in the waiting room and begins cutting their hair. If there are no customers in the waiting room, the barber goes to sleep in his chair. When the haircut is finished, the customer leaves the barbershop.

The problem with this scenario is that the barber and the customers are competing for access to the shared resources of the waiting room and the barber chair. If a customer arrives when the waiting room is full, they must leave and come back later, which can lead to dissatisfaction.

  1. To implement this scenario in process synchronization, we can use several synchronization tools such as semaphores, mutex locks, and condition variables. One possible solution to the FIFO barbershop problem is as follows:
  2. Initialize two semaphores: chairs and barber. The chairs semaphore is initialized to the number of chairs in the waiting room, and the barber semaphore is initialized to 0, indicating that the barber is initially sleeping.
  3. Initialize a mutex lock to protect the critical section, which is the access to the waiting room.
  4. When a customer arrives, they acquire the mutex lock to enter the critical section. If there are no chairs available in the waiting room (the chairs semaphore is 0), the customer releases the lock and leaves the barbershop. Otherwise, the customer decrements the chairs semaphore and releases the lock, indicating that they have taken a seat in the waiting room.
  5. When the barber wakes up, he acquires the mutex lock and enters the critical section. If there are no customers in the waiting room (the chairs semaphore is equal to the number of chairs), the barber releases the lock and goes back to sleep. Otherwise, the barber increments the barber semaphore to indicate that he is ready to cut hair, and he releases the lock.
  6. When a customer’s turn arrives, they acquire the barber semaphore to enter the critical section. The barber then cuts the customer’s hair and releases the semaphore.
  7. When the haircut is finished, the customer releases the chairs semaphore to indicate that they are leaving the waiting room. If there are customers waiting, the barber will immediately take the next customer and begin cutting their hair. Otherwise, the barber releases the mutex lock and goes back to sleep.

This solution ensures that customers are served in the order in which they arrived, and it avoids deadlocks and race conditions that can arise when multiple processes are competing for shared resources.


Last Updated : 24 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads