Sleeping Barber Problem in Process Synchronization

0

 


The Sleeping Barber Problem is a classic problem in computer science that is used to illustrate the principles of process synchronization and semaphores. The problem can be defined as follows: a barber has a barber shop with a waiting room that can hold n chairs. Customers enter the shop and wait in the waiting room if all the chairs are occupied. If a customer arrives and the barber is not busy, they will be immediately served. If the barber is busy, they will wait in the waiting room until a chair becomes available. The barber will continue to serve customers until all the customers have left the waiting room. The barber then takes a nap until a customer enters the shop.

The Sleeping Barber Problem can be solved using semaphores. A semaphore is a variable that is used to control access to a shared resource. In this case, the semaphores are used to control access to the barber shop and the waiting room. When a customer enters the shop, they first request access to the semaphore for the waiting room. If there is an available chair, they are granted access and can sit down. If all the chairs are occupied, the customer will wait until a chair becomes available.

When the barber is not busy, they will request access to the semaphore for the waiting room. If there is a customer waiting, they will be granted access and will serve the customer. If there are no customers waiting, the barber will release the semaphore and take a nap.

The Sleeping Barber Problem can also be solved using monitors. A monitor is a synchronized data structure that provides a way to control access to shared resources. In this case, the monitor is used to control access to the barber shop and the waiting room. When a customer enters the shop, they first request access to the monitor. If there is an available chair, they are granted access and can sit down. If all the chairs are occupied, the customer will wait until a chair becomes available.

When the barber is not busy, they will request access to the monitor. If there is a customer waiting, they will be granted access and will serve the customer. If there are no customers waiting, the barber will release the monitor and take a nap.

In conclusion, the Sleeping Barber Problem is a classic problem in computer science that is used to illustrate the principles of process synchronization and semaphores. The problem can be solved using semaphores or monitors, each with its own advantages and disadvantages. The semaphore approach is more flexible, as it allows multiple processes to access the barber shop and the waiting room at the same time, but it requires more complex logic to ensure that deadlocks are avoided. The monitor approach is more restrictive, as it only allows one process to access the barber shop and the waiting room at a time, but it is also easier to implement and ensures that deadlocks are avoided. Ultimately, the choice of which approach to use will depend on the specific requirements and constraints of the problem being solved.



#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>

#define CHAIRS 5
#define CUSTOMERS 10

sem_t customer;
sem_t barber;
sem_t accessSeats;
int waiting = 0;

void *barberThread(void *arg)
{
    while(1)
    {
        sem_wait(&customer);
        sem_wait(&accessSeats);
        waiting--;
        sem_post(&barber);
        sem_post(&accessSeats);

        printf("Barber is cutting hair.\n");
        sleep(5);
    }
}

void *customerThread(void *arg)
{
    while(1)
    {
        sem_wait(&accessSeats);
        if(waiting < CHAIRS)
        {
            waiting++;
            printf("Customer entering the shop.\n");
            sem_post(&customer);
            sem_post(&accessSeats);
            sem_wait(&barber);

            printf("Customer is getting a hair cut.\n");
        }
        else
        {
            sem_post(&accessSeats);
            printf("Customer leaving the shop.\n");
        }
        sleep(1);
    }
}

int main()
{
    pthread_t barberId;
    pthread_t customerId[CUSTOMERS];
    int i;

    sem_init(&customer, 0, 0);
    sem_init(&barber, 0, 0);
    sem_init(&accessSeats, 0, 1);

    pthread_create(&barberId, NULL, barberThread, NULL);
    for(i = 0; i < CUSTOMERS; i++)
    {
        pthread_create(&customerId[i], NULL, customerThread, NULL);
    }

    pthread_join(barberId, NULL);
    for(i = 0; i < CUSTOMERS; i++)
    {
        pthread_join(customerId[i], NULL);
    }

    sem_destroy(&customer);
    sem_destroy(&barber);
    sem_destroy(&accessSeats);

    return 0;
}


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !