While an interdependent process could be influenced by concurrent processes, a standalone process remains impervious to the activities of other processes. Even though it may be tempting to believe that autonomously functioning processes will perform exceptionally well, there are various scenarios where collaborative behavior can enhance computational efficiency, simplicity, and adaptability. Processes have the capability to engage with each other and synchronize their tasks through a method known as inter-process communication (IPC). The interaction between these processes can be viewed as a form of collaboration.
Problem:
The problem is based on a fictitious barbershop with just one barber, which is a problem. There is a barbershop with one barber, one chair for the barber, and n seats to wait for clients to sit in the chair, if any.
- If there isn't a client, the barber will fall asleep on his own chair.
- He needs to rouse the barber when a client shows up.
- The remaining customers can either wait if there are empty seats in the waiting area or they can leave if there are no empty chairs when there are numerous clients and the barber is cutting a customer's hair.
Solution:
Three synchronization primitives are employed in the resolution of this problem. One semaphore keeps track of the total customers in the waiting zone, excluding the customer in the barber chair since they are not considered waiting. The second mutex ensures the critical section is accessed mutually exclusively, while the binary semaphore for the barber status indicates whether the barber is available or busy. A counter maintained by the client monitors the number of customers presently waiting in the designated area, prompting the next customer to leave the barbershop once the count matches the available seating capacity.
The barber procedure initiates upon the barber's morning arrival, prompting him to set the semaphore for clients to block as it starts at 0. Subsequently, the barber proceeds to rest until the first client appears.
Code:
Semaphore Customers = 0;
Semaphore Barber = 0;
Mutex Seats = 1;
int FreeSeats = N;
Barber {
while(true) {
/* waits for a customer (sleeps). */
down(Customers);
/* mutex to protect the number of available seats.*/
down(Seats);
/* a chair gets free.*/
FreeSeats++;
/* bring customer for haircut.*/
up(Barber);
/* release the mutex on the chair.*/
up(Seats);
/* barber is cutting hair.*/
}
}
Customer {
while(true) {
/* protects seats so only 1 customer tries to sit
in a chair if that's the case.*/
down(Seats); //This line should not be here.
if(FreeSeats > 0) {
/* sitting down.*/
FreeSeats--;
/* notify the barber. */
up(Customers);
/* release the lock */
up(Seats);
/* wait in the waiting room if barber is busy. */
down(Barber);
// customer is having hair cut
} else {
/* release the lock */
up(Seats);
// customer leaves
}
}
}
Analysis:
When the barber begins work, the barber routine is executed, and he verifies if any customers are waiting. Select a customer for a haircut and lock the customer's semaphore if someone is present. If no customers are in line for a haircut, the barber enters a sleep state.
The client unlocks the mutex, prompting the barber to awaken, while the waiting count is incremented when a chair becomes free. Subsequently, the barber proceeds to access the critical section, acquires the mutex, and initiates the haircut.
The customer leaves upon completion of the haircut. Subsequently, the barber checks for any additional clients in the waiting area. If there are none, the barber will then close for the day.