i need the code in C please ,,,, POSIX Synchronization In the Sleeping-Barber pr
ID: 3583313 • Letter: I
Question
i need the code in C please ,,,,POSIX Synchronization
In the Sleeping-Barber problem, a barbershop consists of a waiting room with n chairs and one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and the barber is asleep, the customer wakes up the barber. If the barber is busy but chairs are available, then the customer sits in FCFS free chairs. If all chairs are occupied, then the customer is blocked. Using POSIX semaphores, write a program to synchronize the operations of the barber and the customers. Your program should display all barbershop operations. Note: waiting room size n should be initialized by user on the command line.
i need the code in C please ,,,,
POSIX Synchronization
In the Sleeping-Barber problem, a barbershop consists of a waiting room with n chairs and one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and the barber is asleep, the customer wakes up the barber. If the barber is busy but chairs are available, then the customer sits in FCFS free chairs. If all chairs are occupied, then the customer is blocked. Using POSIX semaphores, write a program to synchronize the operations of the barber and the customers. Your program should display all barbershop operations. Note: waiting room size n should be initialized by user on the command line.
i need the code in C please ,,,,
POSIX Synchronization
In the Sleeping-Barber problem, a barbershop consists of a waiting room with n chairs and one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and the barber is asleep, the customer wakes up the barber. If the barber is busy but chairs are available, then the customer sits in FCFS free chairs. If all chairs are occupied, then the customer is blocked. Using POSIX semaphores, write a program to synchronize the operations of the barber and the customers. Your program should display all barbershop operations. Note: waiting room size n should be initialized by user on the command line.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/ipc.h>
#include <semaphore.h>
time_t end_time;/*end time*/
sem_t mutex,customers,barbers;/*Three semaphors*/
int count=0;/*The number of customers waiting for haircuts*/
void barber(void *arg);
void customer(void *arg);
int main()
{
int n;
printf("Enter waiting room size: ");
scanf("%d", &n);
pthread_t id1,id2;
int condition=0;
end_time=time(NULL)+10; //Barber Shop Hours is 10s (lets say)
/*Semaphore initialization*/
sem_init(&mutex,0,1);
sem_init(&customers,0,0);
sem_init(&barbers,0,1);
/*Barber_thread initialization*/
condition=pthread_create(&id1,NULL,(void *)barber,NULL);
if(condition!=0)
perror("create barbers is failure! ");
/*Customer_thread initialization*/
condition=pthread_create(&id2,NULL,(void *)customer,NULL);
if(condition!=0)
perror("create customers is failure! ");
/*Customer_thread first blocked*/
pthread_join(id2,NULL);
pthread_join(id1,NULL);
exit(0);
}
void barber(void *arg)/*Barber Process*/
{
while(time(NULL)<end_time || count>0)
{
sem_wait(&customers);/*P(customers)*/
sem_wait(&mutex);/*P(mutex)*/
count--;
printf("Barber:cut hair,count is:%d. ",count);
sem_post(&mutex);/*V(mutex)*/
sem_post(&barbers);/*V(barbers)*/
sleep(3);
}
}
void customer(void *arg)/*Customers Process*/
{
while(time(NULL)<end_time)
{
sem_wait(&mutex);/*P(mutex)*/
if(count<N)
{
count++;
printf("Customer:add count,count is:%d ",count);
sem_post(&mutex);/*V(mutex)*/
sem_post(&customer);/*V(customers)*/
sem_wait(&barbers);/*P(barbers)*/
}
else
/*V(mutex)*/
/*If the number is full of customers,just put the mutex lock let go*/
sem_post(&mutex);
sleep(1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.