【问题标题】:sleeping barber giving deadlock睡觉的理发师给僵局
【发布时间】:2012-08-28 12:53:08
【问题描述】:

我编写了睡眠理发师问题的代码,看起来很奇怪...... 代码如下..

#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);
}

问题在于,当缓冲区已满时……理发师正在等待客户……但客户根本没有执行!(既没有等待也没有填充缓冲区)……因为客户while 循环没有执行...

【问题讨论】:

  • 你的线程函数原型应该是void* function (void*)
  • 这不可能是死锁,因为你从来没有在我能看到的任何地方真正锁定你的互斥锁
  • 它是否输入customer? IE。第一个printf 语句是否被执行?您是否尝试过在调试器中运行它,在customer 中放置一个断点并单步执行以查看发生了什么?
  • 您正在修改 2 个未同步的全局变量,这些变量用于控制循环逻辑。您的代码无法以有意义的方式进行预测。当您更改 headtail 时,您或许应该考虑使用互斥锁来保护您的代码。当然,除非我在这里完全误解了某些东西......
  • @nitish712 (非常)不太可能发生,但是如果while 错过了正确的clock() 值,它将循环直到它换行(如果clock_t 是一个整数类型,如果@ 是世界末日987654331@ 是浮点类型)。使用&lt; 只是更具防御性。

标签: c multithreading deadlock


【解决方案1】:

正如 Daneil Fischer 所说...while(((float)(clock()-g))/CLOCKS_PER_SEC != time_slot[a]); 有一个错误,我应该用

替换它

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

但奇怪的是,时钟值的这种“丢失”仅在整个缓冲区被填满后才发生......

【讨论】:

    【解决方案2】:

    执行器使用 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");
        }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多