Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.75/5 (4 votes)
See more:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
#include<sys/ipc.h>
#include<semaphore.h>

#define N 5

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 argc,char *argv[])
{
	pthread_t id1,id2;
	int status=0;
	end_time=time(NULL)+20;/*Barber Shop Hours is 20s*/

	/*Semaphore initialization*/
	sem_init(&mutex,0,1);
	sem_init(&customers,0,0);
	sem_init(&barbers,0,1);

	/*Barber_thread initialization*/
	status=pthread_create(&id1,NULL,(void *)barber,NULL);
	if(status!=0)
		perror("create barbers is failure!\n");
	/*Customer_thread initialization*/
	status=pthread_create(&id2,NULL,(void *)customer,NULL);
	if(status!=0)
		perror("create customers is failure!\n");

	/*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.\n",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\n",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);
	}
}

This is a classic problem of communication between processes. I do not understand the function, sleep(), in the above code. Could someone can tell me the meaning in detail.Thank you and Merry Christmas. :)
Posted
Updated 25-Dec-09 1:14am
v2
Comments
ahmedliver 27-Dec-11 8:06am    
r there a solution for barber problem using monitors in linux implementation

the sleep just worked as the inactive time for the program that it do nothing during given miliseconds of time
 
Share this answer
 
I think that just halts processing for the specified number of milliseconds (or seconds?). So, "sleep(1)" would mean that when that line is processed, your program will do absolutely nothing for 1 millisecond. However, depending on the system that code is run on, it may not be 1 millisecond. For example, my computer seems to only be able to perform operations to a precision of about 15 milliseconds. So, "sleep(1)" might act more like "sleep(15)".
 
Share this answer
 
Comments
Dalek Dave 23-Jul-10 9:26am    
Timing on threads can be a bugger, also where to put the interupts.
In addition to what aspdotnetdev said, when one thread is sleeping that allows another to execute. If the barber thread didn't sleep and give up the processor then the customer thread might never get a chance to do anything. With multi-core processors being more prevalent this might be less of a problem but that wasn't always the case.
 
Share this answer
 
Comments
[no name] 20-Apr-11 11:01am    
Right, and in addition to that: A sleeping thread does not only voluntarily yield the CPU, but also may help to avoid or resolve locks between threads.
i think sleep is not necessary. it's just to simulate the real world. sleep(3) mean that barber is cutting hair.

in additon, sleep is a waitting way without occupation cpu. the way using while is opposite.
 
Share this answer
 
Comments
Richard MacCutchan 19-May-13 3:33am    
Why are you posting answers to a question that is more than three years old?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900