資源簡(jiǎn)介
一個(gè)理發(fā)店接待室有n張椅子,工作室有1張椅子;沒有顧客時(shí),理發(fā)師睡覺;第一個(gè)顧客來到時(shí),必須將理發(fā)師喚醒;顧客來時(shí)如果還有空座的話,他就坐在一個(gè)座位上等待;如果顧客來時(shí)沒有空座位了,他就離開,不理發(fā)了;當(dāng)理發(fā)師處理完所有顧客,而又沒有新顧客來時(shí),他又開始睡覺。
代碼片段和文件信息
#include
#include
#include
#define?chair?5
#define?max?1000
pthread_mutex_t?the_mutex;
pthread_cond_t?condbcondc;
int?w=0;
void?*?barber(void?*?arg)
{
??for(int?i=0;i ??{
pthread_mutex_lock(&the_mutex);//互斥使用緩沖區(qū)
while(w==0)
pthread_cond_wait(&condb&the_mutex);
w--;
printf(“is?cutting?hairthe?number?of?consumer?is:%d\n“w);
pthread_cond_signal(&condc);//喚醒消費(fèi)者
pthread_mutex_unlock(&the_mutex);//釋放緩沖區(qū)
????sleep(2);
??}
}
void?*?consumer(void?*?arg)
{
??for(int?i=0;i ??{
pthread_mutex_lock(&the_mutex);//互斥使用緩沖區(qū)
if(w>=
評(píng)論
共有 條評(píng)論