0% found this document useful (0 votes)
30 views3 pages

7.sleeping Barber Problem

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

7.sleeping Barber Problem

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

7) Simulate sleeping barber problem

Aim: To simulate sleeping barber problem

Description:

The Sleeping Barber Problem is a classic synchronization problem that demonstrates how multiple
threads (customers and a barber) can interact in a shared resource (barber shop with chairs). In this
problem, the barber sleeps until a customer arrives and then serves the customer. The number of chairs
in the waiting room determines how many customers can wait.

In this program:

1. We use three semaphores: customers to signal the presence of customers, barber to signal that
the barber is ready to cut hair, and mutex for mutual exclusion.

2. The barber_thread simulates the barber. It waits for customers to arrive, cuts hair, and sleeps
for a while to simulate hair cutting.

3. The customer_thread simulates customers arriving at the barber shop. If there's a chair
available, the customer takes a seat and waits for the barber. If all chairs are occupied, the
customer leaves.

4. We create and join threads for the barber and customers, and use semaphores to control access
to shared resources.

Program:

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

#define NUM_CUSTOMERS 10
#define NUM_CHAIRS 5

sem_t customers;
sem_t barber;
sem_t mutex;
int waiting = 0;

void *barber_thread(void *arg) {


while (1) {
sem_wait(&customers);
sem_wait(&mutex);
waiting--;
sem_post(&barber);
sem_post(&mutex);
printf("Barber is cutting hair.\n");
sleep(1); // Simulate hair cutting
}
pthread_exit(NULL);
}

void *customer_thread(void *arg) {


int id = *(int *)arg;
sleep(1); // Simulate customer arriving
sem_wait(&mutex);
if (waiting < NUM_CHAIRS) {
waiting++;
printf("Customer %d takes a seat.\n", id);
sem_post(&customers);
sem_post(&mutex);
sem_wait(&barber);
printf("Customer %d is getting a haircut.\n", id);
} else {
sem_post(&mutex);
printf("Customer %d leaves, no chairs available.\n", id);
}
pthread_exit(NULL);
}

int main() {
pthread_t barber_t, customers_t[NUM_CUSTOMERS];
int customer_ids[NUM_CUSTOMERS];

sem_init(&customers, 0, 0);
sem_init(&barber, 0, 0);
sem_init(&mutex, 0, 1);

pthread_create(&barber_t, NULL, barber_thread, NULL);

for (int i = 0; i < NUM_CUSTOMERS; i++) {


customer_ids[i] = i + 1;
pthread_create(&customers_t[i], NULL, customer_thread, &customer_ids[i]);
}

pthread_join(barber_t, NULL);
for (int i = 0; i < NUM_CUSTOMERS; i++) {
pthread_join(customers_t[i], NULL);
}

sem_destroy(&customers);
sem_destroy(&barber);
sem_destroy(&mutex);

return 0;
}
Output:
Customer 1 takes a seat.
Barber is cutting hair.
Customer 8 takes a seat.
Customer 4 takes a seat.
Customer 2 takes a seat.
Customer 1 is getting a haircut.
Customer 5 takes a seat.
Customer 10 takes a seat.
Customer 7 leaves, no chairs available.
Customer 6 leaves, no chairs available.
Customer 9 leaves, no chairs available.
Customer 3 leaves, no chairs available.
Barber is cutting hair.
Customer 8 is getting a haircut.
Barber is cutting hair.
Customer 4 is getting a haircut.
Barber is cutting hair.
Customer 2 is getting a haircut.
Barber is cutting hair.
Customer 5 is getting a haircut.
Barber is cutting hair.
Customer 10 is getting a haircut.
Result:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy