How do I solve the barbershop problem in C using pthreads A

How do I solve the barbershop problem in C, using pthreads?

A barbershop consists of a waiting room with n chairs, and the barber room containing the barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy, but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.

Customer threads should invoke a function named getHairCut.

• If a customer thread arrives when the shop is full, it can invoke balk, which does not return.

• The barber thread should invoke cutHair.

• When the barber invokes cutHair there should be exactly one thread invoking getHairCut concurrently.

Solution

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

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h> // The maximum number of customer threads.
#define MAX_CUSTOMERS 25 // Function prototypes...

void *getHairCut(void *num);
void *cutHair(void *);
void randwait(int secs);

//Define the semaphores.
// waitingRoom Limits the # of customers allowed to enter the waiting room at one time.
sem_t waitingRoom;
// barberChair ensures mutually exclusive access to the barber chair.
sem_t barberChair;
// barberPillow is used to allow the barber to sleep until a customer arrives.
sem_t barberPillow;
// seatBelt is used to make the customer to wait until the barber is done cutting his/her hair.
sem_t seatBelt;
// Flag to stop the barber thread when all customers have been serviced.
int allDone = 0;

int main(int argc, char *argv[])
{
pthread_t btid;
pthread_t tid[MAX_CUSTOMERS];
int i, x, numCustomers, numChairs; int Number[MAX_CUSTOMERS];
printf(\"Maximum number of customers can only be 25. Enter number of customers and chairs.\ \");
scanf(\"%d\",&x);
numCustomers = x;
scanf(\"%d\",&x);
numChairs = x;
if (numCustomers > MAX_CUSTOMERS) {
printf(\"The maximum number of Customers is %d.\ \", MAX_CUSTOMERS);
system(\"PAUSE\");
return 0;
}
printf(\"A solution to the sleeping barber problem using semaphores.\ \");
for (i = 0; i < MAX_CUSTOMERS; i++) {
Number[i] = i;
}
// Initialize the semaphores with initial values...
sem_init(&waitingRoom, 0, numChairs);
sem_init(&barberChair, 0, 1);
sem_init(&barberPillow, 0, 0);
sem_init(&seatBelt, 0, 0);
  
// Create the barber.
pthread_create(&btid, NULL, cutHair, NULL);
  
// Create the customers.
for (i = 0; i < numCustomers; i++) {
pthread_create(&tid[i], NULL, getHairCut, (void *)&Number[i]);
}
// Join each of the threads to wait for them to finish.
for (i = 0; i < numCustomers; i++) {
pthread_join(tid[i],NULL);
}
// When all of the customers are finished, kill the barber thread.
allDone = 1;
sem_post(&barberPillow); // Wake the barber so he will exit.
pthread_join(btid,NULL);
system(\"PAUSE\");
return 0;
}

void *getHairCut(void *number) {
int num = *(int *)number; // Leave for the shop and take some random amount of time to arrive.
printf(\"Customer %d leaving for barber shop.\ \", num);
randwait(5);
printf(\"Customer %d arrived at barber shop.\ \", num); // Wait for space to open up in the waiting room...
sem_wait(&waitingRoom);
printf(\"Customer %d entering waiting room.\ \", num); // Wait for the barber chair to become free.
sem_wait(&barberChair); // The chair is free so give up your spot in the waiting room.
sem_post(&waitingRoom); // Wake up the barber...
printf(\"Customer %d waking the barber.\ \", num);
sem_post(&barberPillow); // Wait for the barber to finish cutting your hair.
sem_wait(&seatBelt); // Give up the chair.
sem_post(&barberChair);
printf(\"Customer %d leaving barber shop.\ \", num);
}

void *cutHair(void *junk)
{
// While there are still customers to be serviced... Our barber is omnicient and can tell if there are customers still on the way to his shop.

while (!allDone) { // Sleep until someone arrives and wakes you..
printf(\"The barber is sleeping\ \");
sem_wait(&barberPillow); // Skip this stuff at the end...
if (!allDone)
{ // Take a random amount of time to cut the customer\'s hair.
printf(\"The barber is cutting hair\ \");
randwait(3);
printf(\"The barber has finished cutting hair.\ \"); // Release the customer when done cutting...
sem_post(&seatBelt);
}
else {
printf(\"The barber is going home for the day.\ \");
}
}
}
  
void randwait(int secs) {
int len = 1; // Generate an arbit number...
sleep(len);
}

====================================================

output:

akshay@akshay-Inspiron-3537:~/Chegg$ gcc barber.c -lpthread
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Maximum number of customers can only be 25. Enter number of customers and chairs.
5
2
A solution to the sleeping barber problem using semaphores.
The barber is sleeping
Customer 3 leaving for barber shop.
Customer 1 leaving for barber shop.
Customer 0 leaving for barber shop.
Customer 2 leaving for barber shop.
Customer 4 leaving for barber shop.
Customer 3 arrived at barber shop.
Customer 3 entering waiting room.
Customer 3 waking the barber.
Customer 2 arrived at barber shop.
Customer 2 entering waiting room.
Customer 4 arrived at barber shop.
Customer 4 entering waiting room.
Customer 0 arrived at barber shop.
The barber is cutting hair
Customer 1 arrived at barber shop.
The barber has finished cutting hair.
The barber is sleeping
Customer 3 leaving barber shop.
Customer 0 entering waiting room.
Customer 2 waking the barber.
The barber is cutting hair
The barber has finished cutting hair.
The barber is sleeping
Customer 2 leaving barber shop.
Customer 4 waking the barber.
Customer 1 entering waiting room.
The barber is cutting hair
The barber has finished cutting hair.
The barber is sleeping
Customer 4 leaving barber shop.
Customer 0 waking the barber.
The barber is cutting hair
The barber has finished cutting hair.
The barber is sleeping
Customer 0 leaving barber shop.
Customer 1 waking the barber.
The barber is cutting hair
The barber has finished cutting hair.
The barber is sleeping
Customer 1 leaving barber shop.
The barber is going home for the day.

How do I solve the barbershop problem in C, using pthreads? A barbershop consists of a waiting room with n chairs, and the barber room containing the barber cha
How do I solve the barbershop problem in C, using pthreads? A barbershop consists of a waiting room with n chairs, and the barber room containing the barber cha
How do I solve the barbershop problem in C, using pthreads? A barbershop consists of a waiting room with n chairs, and the barber room containing the barber cha

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site