2012-08-28 43 views
1

我寫的代碼睡理髮問題,它似乎在尋找怪異...... 的代碼如下..睡覺理髮師給人僵局

#include<stdlib.h> 
#include<stdio.h> 
#include<pthread.h> 

#define MAX_C 10 
int a[MAX_C], head=0, tail=0, tmp, tb, tc, count=1; 
pthread_mutex_t B; 
double time_slot[]={0.125,0.5,0.75,1.00,1.25,1.50,1.75,2.00}; 

void wait(int a) 
{ 
    clock_t g=clock(); 
    while(((float)(clock()-g))/CLOCKS_PER_SEC != time_slot[a]); 
} 

void barber() 
{ 
    printf("barber started\n"); 
    while(1) { 
     tmp=0; 
     while(head==tail) { 
      printf("b\n"); 
     } 
     tb=rand()%8; 
     printf("tail:%d\n", tail); 
     tail=(tail+1)%MAX_C; 
     wait(tb); 
    } 
} 

void customer() 
{  
    printf("customers started\n"); 
    while(1) { 
     wait(rand()%8); 
     while((head+1)%MAX_C == tail) { 
      printf("c\n"); 
     } 
     a[head]=count-1; 
     printf("head:%d\n", head); 
     head=(head+1)%MAX_C; 
    } 
} 

int main(int argc, char* argv[]) 
{ 
    pthread_t b,c; 

    pthread_mutex_init(&B, NULL); 
    pthread_create(&c, NULL, (void*)&customer, NULL); 
    pthread_create(&b, NULL, (void*)&barber, NULL); 

    pthread_join(b, NULL); 
    pthread_join(c, NULL); 
    exit(0); 
} 

的問題是,當緩衝區已滿。理髮師正在等待顧客......但顧客根本沒有執行任務!!(它既沒有等待也沒有填充緩衝器)......因爲顧客在循環未執行的同時...

+0

你的線程函數原型應該是'無效*函數(無效*)' – mathematician1975

+0

這也不能成爲僵局的比如你從來沒有真正鎖定互斥的任何地方,我可以看到 – mathematician1975

+0

它是否輸入'customer'?即執行第一個'printf'語句嗎?你是否嘗試過在調試器中運行它,在'customer'中放置一個斷點,並通過它來檢查發生了什麼? –

回答

0

As Daneil Fischer說...有一個錯誤while(((float)(clock()-g))/CLOCKS_PER_SEC != time_slot[a]);我應該用

替換它

while(((float)(clock()-g))/CLOCKS_PER_SEC <= time_slot[a]);

,但它仍然奇怪,這種「丟失」的時鐘值是發生在整個緩衝區填滿後,才....

0

你必須在信號燈的問題,即執行者正在使用CPU

請看看您是否使用信號量非常好。 不要忘了的#include

#include <unistd.h> 
#include <stdlib.h> 

#include <pthread.h> 
#include <semaphore.h> 

// The maximum number of customer threads. 
#define MAX_CUSTOMERS 25 
</b> 

void *customer(void *number) { 
    int num = *(int *)number; 

    // Leave for the shop and take some random amount of 
    // time to arrive. 
    printf("Customer %d leaving for barber shop.\n", num); 
    randwait(5); 
    printf("Customer %d arrived at barber shop.\n", num); 

    // Wait for space to open up in the waiting room... 
    sem_wait(&waitingRoom); 
    printf("Customer %d entering waiting room.\n", num); 

    // Wait for the barber chair to become free. 
    sem_wait(&barberChair); 

    // The chair is free so give up your spot in the 
    // waiting room. 
    sem_post(&waitingRoom); 

    // Wake up the barber... 
    printf("Customer %d waking the barber.\n", num); 
    sem_post(&barberPillow); 

    // Wait for the barber to finish cutting your hair. 
    sem_wait(&seatBelt); 

    // Give up the chair. 
    sem_post(&barberChair); 
    printf("Customer %d leaving barber shop.\n", num); 
} 

void *barber(void *junk) { 
    // While there are still customers to be serviced... 
    // Our barber is omnicient and can tell if there are 
    // customers still on the way to his shop. 
    while (!allDone) { 

    // Sleep until someone arrives and wakes you.. 
    printf("The barber is sleeping\n"); 
    sem_wait(&barberPillow); 

    // Skip this stuff at the end... 
    if (!allDone) { 

     // Take a random amount of time to cut the 
     // customer's hair. 
     printf("The barber is cutting hair\n"); 
     randwait(3); 
     printf("The barber has finished cutting hair.\n"); 

     // Release the customer when done cutting... 
     sem_post(&seatBelt); 
    } 
    else { 
     printf("The barber is going home for the day.\n"); 
    } 
    } 
}