【问题标题】:about synchronization while implementing the sleeping barber with multi-thread关于多线程实现睡眠理发师时的同步
【发布时间】:2012-12-22 16:08:45
【问题描述】:

您好,我正在实现一个称为睡眠理发师问题的多线程程序。问题是有一个理发店有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",但是主线程首先开始运行生产者线程,然后当计数器等于0时它打印"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

我将不胜感激每一个回复,无论如何谢谢

编辑:第一个代码无法正常工作,抱歉,我更新了真正的代码。

【问题讨论】:

    标签: c multithreading synchronization pthreads posix


    【解决方案1】:

    您不能假设线程将执行的顺序。 这是出乎意料的行为,它不是确定性的。 如果您需要确保一个线程先于另一个线程启动。使用互斥体或信号量。

    【讨论】:

    • 是的,你是对的,但不应该是输出打印首先“理发师正在睡觉”,因为理发师线程是在生产者线程之前创建的?
    • 没有。这正是我写的。你可以运行 profram 100 次,你会发现有时理发师会先启动,有时则不会
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多