Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 睡眠理发师_C_Multithreading_Deadlock - Fatal编程技术网

C 睡眠理发师

C 睡眠理发师,c,multithreading,deadlock,C,Multithreading,Deadlock,我写了睡眠理发师问题的代码,看起来很奇怪。。。 代码如下 #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.

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

#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);
}
#包括
#包括
#包括
#定义最大值为10
INTA[MAX_C],头部=0,尾部=0,tmp,tb,tc,计数=1;
pthread_mutex_t B;
双时隙[]={0.125,0.5,0.75,1.00,1.25,1.50,1.75,2.00};
无效等待(INTA)
{
时钟=时钟();
而(((float)(clock()-g))/CLOCKS_peru__second!=时隙[a]);
}
空理发师()
{
printf(“理发师开始\n”);
而(1){
tmp=0;
while(head==tail){
printf(“b\n”);
}
tb=rand()%8;
printf(“尾部:%d\n”,尾部);
尾=(尾+1)%MAX_C;
等待(tb);
}
}
作废客户()
{       
printf(“已启动的客户”);
而(1){
等待(rand()%8);
而((头部+1)%MAX_C==尾部){
printf(“c\n”);
}
a[头]=计数-1;
printf(“头:%d\n”,头);
水头=(水头+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);
出口(0);
}

问题是当缓冲区已满时。。。理发师正在等顾客。。。但是客户根本没有执行!!(它既不等待也不填充缓冲区)。。。因为loop没有执行时客户…

正如Daneil Fischer所说。。。
while(((float)(clock()-g))/CLOCKS_PER_second!=时隙[a])出错我应该将其替换为


while((float)(clock()-g))/CLOCKS\u PER\u secs执行器正在使用CPU的信号量有问题

请查看您是否很好地使用了信号灯。 别忘了包括在内

#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");
    }
    }
}
#包括
#包括
#包括
#包括
//客户线程的最大数目。
#定义最大客户数25
作废*客户(作废*编号){
int num=*(int*)编号;
//到商店去,随便拿一些
//到时间了。
printf(“客户%d前往理发店。\n”,num);
randwait(5);
printf(“客户%d到达理发店。\n”,num);
//等待等候室的空间打开。。。
sem_等候室(和等候室);
printf(“客户%d进入等候室。\n”,num);
//等待理发椅释放。
sem_wait(理发椅);
//椅子是空的,所以放弃你在椅子上的位置吧
//候诊室。
sem_邮局(和候机室);
//叫醒理发师。。。
printf(“客户%d唤醒理发师。\n”,num);
sem_柱(和枕头);
//等理发师给你剪完头发。
sem_等待(和安全带);
//把椅子让开。
sem_柱(和理发椅);
printf(“客户%d离开理发店。\n”,num);
}
空*理发师(空*垃圾){
//虽然仍有客户需要服务。。。
//我们的理发师无所不知,能分辨出是否有
//顾客们还在去他的商店的路上。
而(!全部完成){
//睡到有人来叫醒你。。
printf(“理发师正在睡觉”\n”);
sem_wait(理发枕头);
//在结尾跳过这些东西。。。
如果(!全部完成){
//花一段随机的时间来削减成本
//顾客的头发。
printf(“理发师正在理发”;
randwait(3);
printf(“理发师已剪完头发。\n”);
//完成切割后释放客户。。。
sem_柱和安全带;
}
否则{
printf(“理发师今天要回家了。\n”);
}
}
}

你的线程函数原型应该是
void*函数(void*)
这也不能是死锁,因为你从来没有将互斥锁锁定在我能看到的任何地方。它是否进入
customer
了?也就是说,第一条
printf
语句是否被执行了?你有没有试过在调试器中运行它,在
customer
中设置断点,然后逐步查看发生了什么OU在不同步的情况下修改2个全局变量,这些变量用于控制循环逻辑。您的代码不可能以有意义的方式进行预测。在更改<代码>头<代码>和<代码>尾部>代码>时,您应该考虑互斥体保护代码。除非我完全误解了这里的某些内容。当然…@nitish712(非常)不太可能发生,但是如果
while
错过了正确的
clock()
值,它将循环直到结束(如果
clock\t
是整数类型,如果
clock\t
是浮点类型,则为世界末日)。使用