Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Linux
#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. Smile | :)
Posted 25-Dec-09 2:13am
GD_1992490
Edited 25-Dec-09 2:14am
v2
Comments
ahmedliver at 27-Dec-11 8:06am
   
r there a solution for barber problem using monitors in linux implementation
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

the sleep just worked as the inactive time for the program that it do nothing during given miliseconds of time
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)".
  Permalink  
Comments
Dalek Dave at 23-Jul-10 9:26am
   
Timing on threads can be a bugger, also where to put the interupts.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
CDP1802 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Comments
Richard MacCutchan at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100