-
大小: 2KB文件類型: .c金幣: 1下載: 0 次發(fā)布日期: 2021-01-08
- 語言: 其他
- 標(biāo)簽: 操作系統(tǒng)??第一個(gè)??
資源簡(jiǎn)介
理發(fā)師問題的描述:一個(gè)理發(fā)店接待室有n張椅子,工作室有1張椅子;沒有顧客時(shí),理發(fā)師睡覺;第一個(gè)顧客來到時(shí),必須將理發(fā)師喚醒;顧客來時(shí)如果還有空座的話,他就坐在一個(gè)座位上等待;如果顧客來時(shí)沒有空座位了,他就離開,不理發(fā)了;當(dāng)理發(fā)師處理完所有顧客,而又沒有新顧客來時(shí),他又開始睡覺。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define??maxn??5
sem_t?mutexcustomersbarbers;
int?waiting=0;
int?chair[maxn];
void?*?barber(void?*arg){
sem_wait(&customers);
sem_wait(&mutex);
waiting--;
int?inext;
for(i=0;i<5;i++){
if(chair[i]){
next=chair[i];
chair[i]=0;
break;
}
}
printf(“The?barber?is?cutting?%dth?customer‘s?hair.\n“next);
usleep(10000);
sem_post(&mutex);
sem_post(&barbers);
}
void?*?customer(void?*arg){
sem_wait(&mutex);
if(waiting waiting++;
int?i;
for(i=0;i<5;i++){
if(!chair[i]){
chair[i]=1;
break;
}
}
printf(“**************************************************\n“);
printf(“The?customer?comes?and?sits?at?%dth?chai
評(píng)論
共有 條評(píng)論