Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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
分段错误:11在使用pthread库的C多线程程序中_C_Pthreads - Fatal编程技术网

分段错误:11在使用pthread库的C多线程程序中

分段错误:11在使用pthread库的C多线程程序中,c,pthreads,C,Pthreads,我需要使用pthreadclibrary制作一个程序来处理线程。任务的内容是:有一条走廊,有一条单行道可以双向通行,有两种类型的员工将穿过走廊到达走廊的对面。第一类员工从左向右,第二类员工从右向左。在这条走廊里,有一个服务员,当没有人经过时,他会清理这条走廊。清洁走廊时,两边的员工都会等待走廊结束,否则,如果走廊被占用,员工会说他无法清洁,并睡2秒钟。 当我编译这个程序时,GCC编译器返回5个警告,但没有错误。当我运行程序时,一个分段错误立即出现:11 这是更新的程序代码: #include &

我需要使用pthreadclibrary制作一个程序来处理线程。任务的内容是:有一条走廊,有一条单行道可以双向通行,有两种类型的员工将穿过走廊到达走廊的对面。第一类员工从左向右,第二类员工从右向左。在这条走廊里,有一个服务员,当没有人经过时,他会清理这条走廊。清洁走廊时,两边的员工都会等待走廊结束,否则,如果走廊被占用,员工会说他无法清洁,并睡2秒钟。 当我编译这个程序时,GCC编译器返回5个警告,但没有错误。当我运行程序时,一个分段错误立即出现:11

这是更新的程序代码:

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


//global variables 
int c1,c2; //counters for imp1, imp2
pthread_mutex_t m1, m2;
sem_t corridor; 
double clean = 0.5;

//implementation thread attendant
void* attendant(void* arg){
    sleep(1); //rest 1 seconds
    if (c1==0 && c2==0){
        sem_wait(&corridor);
        printf("I'm starting to clean up\n");
        sleep(clean);
        printf ("I finished cleaning\n");
        sem_post(&corridor);
        }else{
           printf ("I can't clean, the corridor is busy\n");
        }
    return NULL;
}


//thread employee type 1
void* emp1(void *arg){
    printf("I'm the number %d of em1\n", c1);
    pthread_mutex_lock(&m1); //beginning critical section
    c1++;                    //it increases to signal the presence of a thread of the same type that wants to enter the corridor
    if (c1 == 1){            //the thread is the only one in the corridor. Can pass
        printf ("I am the first of my group emp1\n");
        sem_wait(&corridor);  //takes possession of the corridor
    }
    pthread_mutex_unlock(&m1); //allows other threads of the same type to pass in the corridor since it was the first in his group. End of critical section

    // invents "passage" function

    pthread_mutex_lock(&m1);  //beginning of the critical section. Once crossed the corridor, the variable c1 is modified. A mutex is used to avoid inconsistency
    c1--;
    if(c1 == 0){
        printf ("I am the last of my group emp1\n");
        sem_post(&corridor);
    } //if c1 == 0, it is the last thread imp1 and releases the corridor
    pthread_mutex_unlock(&m1); //end critical section
    return NULL;
}


//thread employee type 2 
void* emp2(void *arg){
    printf("I'm the number %d of emp2\n", c2);
    pthread_mutex_lock(&m2); //beginning critical section
    c2++;                    //it increases to signal the presence of a thread of the same type that wants to enter the corridor
    if (c2 == 1){            // the thread is the only one in the corridor. Can pass
        printf ("I am the first of my group emp2\n");
        sem_wait(&corridor);  //takes possession of the corridor
    }
    pthread_mutex_unlock(&m2); //allows other threads of the same type to pass in the corridor since it was the first in his group. End of critical sectionritica

    // invents "passage" function

    pthread_mutex_lock(&m2);  //beginning of the critical section. Once crossed the corridor, the variable c1 is modified. A mutex is used to avoid inconsistency
    c2--;
    if(c2 == 0){
        printf ("I am the last of my group emp2\n");
        sem_post(&corridor);
    }//if c1 == 0, it is the last thread imp1 and releases the corridor
    pthread_mutex_unlock(&m2); //end critical section
    return NULL;
}


int main(int argc, char const *argv[]) {
    //pthread_t emp1, emp2, attendant;
    pthread_t idt;
    int r; //var random to create thread emp1 or emp2
    int i; //index 

    //variable initialization
    c1 = c2 = 0;
    pthread_mutex_init(&m1, NULL); 
    pthread_mutex_init(&m2, NULL);
    sem_init(&corridor,0,1); 

    pthread_create(&idt,NULL,attendant,NULL); 
    while(i<40){
        sleep(1);
        r = rand()%2;
        if(r==0){
            printf("Employee creation 1\n");
            pthread_create(&idt,NULL,emp1,NULL);
        }else{
            printf("Employee creation 2\n");
            pthread_create(&idt,NULL,emp2,NULL);
        }
        i++;
    pthread_join(idt, NULL);
    }
    return 0;
}
pthread_t emp1,emp2,助理;对具有相同名称的函数进行阴影处理,以便pthread_创建&idt,NULL,attendant,NULL;将变量attendant的未初始化值传递给pthread_create作为函数指针,而不是函数void*attendantvoid*arg

删除这些变量,您根本不使用它们

你也忘了包括在内

这将使您的程序编译:


此外,您的主函数不会等待线程完成,您应该使用pthread_join等待线程。

请修复警告,然后查看是否解决了问题。警告应该被视为错误。调试101:作为初学者,千万不要忽视警告。你因为睡眠而错过了unistd.h。在main中,不需要将emp1、emp2设置为pthread\u t类型的attendent。最后,在pthread_create中,将回调作为引用传递&。可能还有更多的问题,但只有我能看到的问题。不要在问题标题中手动标记答案[已解决]。已经清楚地表明,这个问题有一个公认的答案。