本文介绍了睡觉的理发师问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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);
}
}
这是流程之间通信的典型问题。我在上面的代码中不理解函数sleep()。有人可以告诉我详细的含义。谢谢你,圣诞快乐。 :)
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. :)
推荐答案
这篇关于睡觉的理发师问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!