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

Segmentation fault: 11 in a program of multithread in C using pthread library

我需要使用 pthread C 库编写一个程序来处理线程。任务的内容是这样的:有一条走廊,只有一条单车道,有两个方向,有两种员工会穿过走廊到达对面。有从左到右的类型 1 员工和从右到左的类型 2 员工。在这条走廊里,有一个服务员在没有人经过的时候打扫这条走廊。清扫走廊时,双方员工等清扫完毕,否则走廊有人,员工说不能清扫,睡2秒。 当我编译这个程序时,GCC 编译器 returns 5 警告但没有错误。当我运行程序时,立即出现Segmentation fault: 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, attendant; 隐藏同名函数,因此 pthread_create(&idt,NULL,attendant,NULL); 将变量 attendant 的未初始化值作为函数指针而不是函数 pthread_create 传递给 pthread_create =14=].

删除变量,您根本没有使用它们。

你也忘了#include <unistd.h>

这将使您的程序编译:https://ideone.com/3CIb4J

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