Linux实验全纪录之 理发师问题

#include   
#include   
#include   
#include   
#include   
#include   
#include   
#define CHAIRS 5  
sem_t customers; 
sem_t barbers;  
pthread_mutex_t mutex; 
int waiting = 0; 
void *barber(void *arg);  
void *customer(void *num);  
void cut_hair(void);  
double timediff(struct timeval i,struct timeval j);  
void seed_random(void);  
double flat(void);  
double normal(void);  
double bursty(void);  
int main()  
   {  
       int i;  
       seed_random();  
       pthread_t barber_t,customer_t;  
       int error;  
       error=pthread_create(&barber_t,NULL,barber,NULL);//创建理发师线程  
       if(error!=0) 
       {  
        	printf("pthread_create is not created...\n");  
            return -1;  
       }  
       while(1) 
       {  
          usleep(30000);//等待时间如果小于理发师理发时间则会出现等待者过多,否则不会出现等待者过多的现象  
          error=pthread_create(&customer_t,NULL,customer,NULL);//创建顾客线程  
          if(error!=0) 
          {  
             printf("pthread_create is not created...\n");  
             return -1;  
          }  
       }  
    }  
double timediff(struct timeval now,struct timeval earlier)  
    {  
       if(now.tv_sec == earlier.tv_sec)  
          return (now.tv_usec - earlier.tv_usec)/1000000.0;  
       else  
          return (1000000*(now.tv_sec - earlier.tv_sec) + now.tv_usec - earlier.tv_usec)/1000000.0;  
    }  
void *barber(void *arg)  
    {  
    	while(1)  
        {  
          sem_wait(&customers);//顾客信号量-1  
          pthread_mutex_lock(&mutex);  
          waiting = waiting -1;  
          sem_post(&barbers);//  
          pthread_mutex_unlock(&mutex);  
          cut_hair();//理发  
        }  
    }  
void cut_hair(void)  
{  
    printf("  Barber:I am cutting the customer's hair...\n");  
    usleep(100000);//理发时间  
    printf("  Barber:done.\n");  
}  
void *customer(void *num)  
{  
    pthread_mutex_lock(&mutex);  
    if(waiting> 16);  
    xsub1[2] = (ushort)(getpid());  
    seed48(xsub1);  
}  
double flat()  
{  
    return drand48()/5;  
} 
double normal()
{
	return sin(M_PI*drand48())/M_PI/2.006999;
}

double bursty()
{
	return (sin(M_PI+drand48()*M_PI)+1.)/3.591;
}


编译时 加上-lpthread -lm

你可能感兴趣的:(Linux实验)