2012-12-22 117 views
1

您好我正在實施一個多線程程序,這就是所謂的睡理髮師問題。問題是有一個理髮店有N把椅子,如果沒有任何顧客理髮師會入睡,每當顧客進店時,理髮師都會醒來,並開始切斷顧客的頭髮。我使用條件變量而不是信號量。事情是我不能有我期望的輸出。關於同步,同時實現多線程熟睡理髮師

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

    #define TRUE 1 

    pthread_mutex_t mutex; 
    pthread_cond_t fill; 

    int counter; 

    int* buffer; 
    int size; 

    void* producer(void*); 
    void* consumer(void*); 

    void initialize_variables(); 
    void insert(void); 
    void remove_item(void); 

    int main(int argc, char *argv[]) { 

     int random_num; 
     int num=0; 
     pthread_t barber; 

     srand(time(NULL)); 

     if(argc!=2){ 
      printf("Not enough parameters\n"); 
      exit(0); 
     } 

     size=atoi(argv[1]); 

     initialize_variables(); 

     pthread_create(&barber,NULL,consumer,NULL); 

     while(TRUE){ 

      pthread_t customer; 
      pthread_create(&customer,NULL,producer,NULL); 

     } 

     printf("Exit the program\n"); 
     return 0; 
    } 

    void initialize_variables(){ 

     int i; 

     if(! (buffer=(int*)malloc(size*sizeof(int)))){ 
      printf("Error while allocating memory for buffer\n"); 
      exit(1); 
     } 

     for(i=0; i<size; i++) 
      buffer[i]=0; 

     pthread_mutex_init(&mutex,NULL); 

     pthread_cond_init(&empty, NULL); 
     pthread_cond_init(&fill, NULL); 

     counter = 0; 
    } 

    void* producer(void* arg){ 

    pthread_mutex_lock(&mutex); 

     while(counter==size) 
      pthread_cond_wait(&empty,&mutex); 

     printf("One Customer has arrived and sit on the #%d chair\n", counter); 
     insert(); 
     sleep(1); 
     pthread_cond_signal(&fill); 

    pthread_mutex_unlock(&mutex); 

    pthread_exit(NULL); 
} 

void* consumer(void* arg){ 

    while(TRUE){ 

     pthread_mutex_lock(&mutex); 

      while(counter==0){ 
       printf("Barber is sleeping now\n"); 
       pthread_cond_wait(&fill,&mutex); 
      } 

      remove_item(); 
      sleep(1); 
      pthread_cond_broadcast(&empty); 

     pthread_mutex_unlock(&mutex); 

    } 
} 

void insert(void){ 

    buffer[counter]=1; 
    counter++; 
} 

void remove_item(void){ 

    buffer[counter]=0; 
    printf("The barber is started to cut the hair of the customer at chair #%d\n", counter); 
    counter--; 
} 

而且,當我開始輸出程序應打印"Barber is sleeping now"第一,但主線程,然後再開始運行生產線時,計數器等於零它打印"Barber is sleeping now"你能幫助我,爲什麼順序錯誤開始?另一個問題是主線程填充整個緩衝區並開始倒計時(這指的是理髮師在店鋪滿了之後將開始他的工作,這裏是輸出的一個例子,它會讓你明白,並假設緩衝區大小爲10

One Customer has arrived and sit on the #0 chair 
One Customer has arrived and sit on the #1 chair 
One Customer has arrived and sit on the #2 chair 
One Customer has arrived and sit on the #3 chair 
One Customer has arrived and sit on the #4 chair 
One Customer has arrived and sit on the #5 chair 
One Customer has arrived and sit on the #6 chair 
One Customer has arrived and sit on the #7 chair 
One Customer has arrived and sit on the #8 chair 
One Customer has arrived and sit on the #9 chair 
The barber is started to cut the hair of the customer at chair #10 
The barber is started to cut the hair of the customer at chair #9 
The barber is started to cut the hair of the customer at chair #8 
The barber is started to cut the hair of the customer at chair #7 
The barber is started to cut the hair of the customer at chair #6 
The barber is started to cut the hair of the customer at chair #5 
The barber is started to cut the hair of the customer at chair #4 
The barber is started to cut the hair of the customer at chair #3 
The barber is started to cut the hair of the customer at chair #2 
The barber is started to cut the hair of the customer at chair #1 
Barber is sleeping now 
One Customer has arrived and sit on the #0 chair 

我可以理解爲每一個答覆,並感謝反正

編輯:第一代碼後不能正常工作抱歉的是,我與真正的一個更新的

回答

1

你對於哪些線程將執行的順序,不能假定任何東西。 這是意想不到的行爲,我t不確定。 如果您需要確保一個線程先於另一個線程啓動。使用互斥量或信號量。

+0

是的,你是對的,但不應該是輸出打印首先「理髮師現在正在睡覺」因爲理髮線程在生產者線程之前創建? – quartaela

+0

不,這正是我寫的。你可以運行profram 100次,看到有時理髮師會先開始,有時它不會 – stdcall

+0

好吧,然後我明白了。感謝您的有用信息。 – quartaela