#include
#include
#include"unistd.h"
//#ifndef "unistd.h" #define "unistd.h" #include #include #endif /* _UNISTD_H */
//#include"pthread.h"
#include"semaphore.h"
#include"fcntl.h"
#include "errno.h"
#define N 5
//引入3个信号量:mutex,customers,barbers;
sem_t mutex,customers,barber;
int waiting =0;
int chair [5];
void *barbers();
void *customer();
int main(int argc,char *argv[])
//定义10个信号量和一个理发师信号量
pthread_t Customer_id[10],Barbers_id;
int i;
sem_init(&mutex,0,1); //init mutex semaphore to 1
sem_init (&barber,0,1);
sem_init(&customers,0,0);
//init semaphore customers to 0
//creat barber pthread
void *barber()
{int i;
int next;
//wait(customers),如果没有顾客,理发师休息
sem_wait(&customers)
sem_wait (&mutex)
//wait(mutex)
waiting--;
//the number of waiting reduce one
for(i=0,i |