Friday 5 February 2016

Barber Shop Example Using Mutex and Semaphore in OS | pthread_create Example (C)

Barber Shop Example Using Mutex and Semaphore in OS | pthread_create Example (C)

Description:    Consider a situation of a hair salon where there is a barber room containing the barber chair and a waiting room for customers comprising of n chairs. The barber goes to sleep if there is no customer in the salon. If a customer arrives the salon and all chairs are occupied, then the customer leaves the salon. But the customer sits in one of the free chairs if the barber is busy and chairs are available. If the barber is asleep, the customer wakes up the barber.

The following information is provided:
  •  Customer threads should invoke a function named getHairCut.
  • If a customer thread arrives when the salon is full, it can invoke balk, which does not return.
  • Barber threads should invoke cutHair.
  • When the barber invokes cutHair, getHairCut should be invoked by exactly on thread concurrently.



Code in C

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

#define NO_OF_CHAIRS 5

sem_t arrives;
sem_t cutting;
sem_t leaves;

pthread_mutex_t mutex;
pthread_mutex_t c_mutex;


int count=0;
bool isCutting=false;


void cut_Hair(){
   sem_wait(&cutting);
   isCutting=true;
   int s_t=rand()%6;
   printf("\t -->Cutting (Time required:%d secs.)\n",s_t);
   sleep(s_t);
  
   sem_post(&cutting);
   sem_post(&leaves);
   isCutting=false;
   sleep(1);
}

void getHairCut(int no){
   printf("CUSTOMER#%d is having the cutting\n",no);
  
   sem_wait(&leaves);
   sem_post(&cutting);
   printf("CUSTOMER#%d has got his hair cut and left.\n\n",no);
  
}

void Balk(){
   Balk();  
}

void *Customer(void *param){
   int no,s_t;
   no=(int*)param;
   s_t=rand()%8;
   sleep(s_t);
   pthread_mutex_lock(&c_mutex);
   printf("CUSTOMER#%d Arrived\n",no);
   count++;
   if(count+1>NO_OF_CHAIRS){
      printf("No Seat Available for CUSTOMER#%d. Customer left.\n",no);
      Balk();  
   }
   else if(!isCutting)
      printf("CUSTOMER#%d straight-forward went to Barber's room without waiting.\n",no);
  
   else{
      pthread_mutex_lock(&mutex);
     
      printf("Barber is Busy. CUSTOMER#%d went to waiting room.\n",no);
      sem_wait(&cutting);
      count--;
      pthread_mutex_unlock(&mutex);
   }
   pthread_mutex_unlock(&c_mutex);
   sem_post(&arrives);

   getHairCut(no);

  
}

void *Barber(void *param){
   int s_t;
   while(true){
      if(count==0)
          printf("\t -->Sleeping\n\n");
      sem_wait(&arrives);
     
      cut_Hair();
   }
}


int main(int argc,char *argv[]){
  
   sem_init(&arrives, 2, 0);
   sem_init(&leaves, 20, 0);
   sem_init(&cutting, 2, 1);

   pthread_mutex_init(&mutex,NULL);
   pthread_mutex_init(&c_mutex,NULL);

   int i,sleep_time,no_of_cust_threads;
  
   sleep_time=atoi(argv[1]);
   no_of_cust_threads=atoi(argv[2]);
  
   pthread_t threadID;
   pthread_attr_t attributes;
   pthread_attr_init (&attributes);

   pthread_create(&threadID,&attributes,Barber,NULL);
     
   for(i=0;i<no_of_cust_threads;i++)
      pthread_create(&threadID,&attributes,Customer,i+1);
     
   sleep(sleep_time);
   exit(0);
   return 0;
}



No comments

Post a Comment

Recent Posts