【问题标题】:Segmentation fault when using pthreads使用 pthread 时出现分段错误
【发布时间】:2018-10-03 14:20:28
【问题描述】:

我在使用线程模拟地铁站时遇到分段错误 我的想法是,我有 4 个车站,有 4 条路径和 4 列火车,每列火车通过一条路径到达某个车站,火车按我最初的模拟工作的线程移动,直到我尝试进入 4th线程(“火车”)到我的世界,我开始出现分段错误(核心愚蠢) 我把这些作为我的结构:

struct Path {  
 int Direction; //if equals to 1 it's to the tunnel   0 it's away from the tunnel 
   int size;
   char name[25];  // A or B or C or D
   int Used;
};
 struct train {

   int TrainId;
   int   speed;
   int position;
   int direction;
   struct Path CurPath;

}; 

一些路径初始化器

 struct Path PathA; //Path A inital Info 
    PathA.Direction = 1 ;
    strcpy( PathA.name, "A");
    PathA.size = 20;
    paths[0]=PathA;

还有一些训练初始化器:

   train1->speed = 20;
   train1->CurPath = PathA;
   train1->TrainId = 1;
   train1->direction = 1;
   train1->position = 0;
 pthread_create(&train1Th, NULL, run, (void *)train1);

以及线程的运行方法:

void *run (void  *Train){
    //sleep(1);
    int i;
    struct train *Trains= (struct train*)Train;
    if((*Trains).TrainId == 1)
    i=0;
    if((*Trains).TrainId == 2)
    i=2;
    if((*Trains).TrainId == 3)
    i=0;
    if((*Trains).TrainId == 4)
    i=2;


    while(run){
    sleep(1);
    if((*Trains).TrainId == 1)
    if (i==8) i=0;
    if((*Trains).TrainId == 2)
    if (i==6) i=2;
    if((*Trains).TrainId == 3)
    if (i>8) i=0;
    if((*Trains).TrainId == 4)
    if (i>8) i=0;
    (*Trains).CurPath = paths[i];
    paths[i].Used = 0;
    if((*Trains).CurPath.Used == 1){ puts("Busy waiting");
    sleep(2);}

        printf("\nThe Train is on path : %s,is now at the endpoint with an id of %d", (*Trains).CurPath.name, (*Trains).TrainId);

    paths[i].Used == 1; 
    if((*Trains).TrainId == 1)
    i=i+4;
    if((*Trains).TrainId == 2)
    i=i+2;
    if((*Trains).TrainId == 3)
    i=i+7;
    if((*Trains).TrainId == 4)
    i=i+5;

    }

i 仅用于路径选择 尝试添加新火车时导致分段错误的任何想法? 这是我的整个代码的副本:

    #include <stdio.h>
    #include <string.h>
    #include <pthread.h>
    #include <stdlib.h>
    #include <time.h>
    #include<sys/time.h>

    int semtime =20;
    struct Path {
       int Direction; //if equals to 1 it's to the tunnel   0 it's away from the tunnel 
       int size;
       char name[25];  // A or B or C or D
       int Used;
    };
    struct train {

       int TrainId;
       int   speed;
       int position;
       int direction;
       struct Path CurPath;

    }; 
     struct Path paths[8];
    void *run (void  *Train){
        //sleep(1);
        int i;
        struct train *Trains= (struct train*)Train;
        if((*Trains).TrainId == 1)
        i=0;
        if((*Trains).TrainId == 2)
        i=2;
        if((*Trains).TrainId == 3)
        i=0;
        if((*Trains).TrainId == 4)
        i=2;


        while(run){
        sleep(1);
        if((*Trains).TrainId == 1)
        if (i==8) i=0;
        if((*Trains).TrainId == 2)
        if (i==6) i=2;
        if((*Trains).TrainId == 3)
        if (i>8) i=0;
        if((*Trains).TrainId == 4)
        if (i>8) i=0;
        (*Trains).CurPath = paths[i];
        paths[i].Used = 0;
        if((*Trains).CurPath.Used == 1){ puts("Busy waiting");
        sleep(2);}

            printf("\nThe Train is on path : %s,is now at the endpoint with an id of %d", (*Trains).CurPath.name, (*Trains).TrainId);

        paths[i].Used == 1; 
        if((*Trains).TrainId == 1)
        i=i+4;
        if((*Trains).TrainId == 2)
        i=i+2;
        if((*Trains).TrainId == 3)
        i=i+7;
        if((*Trains).TrainId == 4)
        i=i+5;

        }
        //printf("%02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);

     /*   time_t now = time( NULL);

        struct tm now_tm = *localtime( &now);


        struct tm then_tm = now_tm;
        then_tm.tm_sec += semtime;   // add 50 seconds to the time

        mktime( &then_tm);      // normalize it

        printf( "%s\n", asctime( &now_tm));
        printf( "%s\n", asctime( &then_tm));
    */

            return NULL;

     }

    int main( ) {  
        /* Initilizing Paths */
       struct Path PathA; //Path A inital Info 
        PathA.Direction = 1 ;
        strcpy( PathA.name, "A");
        PathA.size = 20;
        paths[0]=PathA;

       struct Path PathA2; //Path A inital Info The other direction
        PathA.Direction = 0 ;
        strcpy( PathA.name, "A");
        PathA.size = 20;
        paths[1]=PathA2;

       struct Path PathB; //Path B inital Info
        malloc(sizeof(struct Path));
        PathB.Direction = 1 ;
        strcpy( PathB.name, "B");
        PathB.size = 20;
        paths[2]=PathB;

       struct Path PathB2; //Path A inital Info The other direction
        PathB2.Direction = 0 ;
        strcpy( PathB2.name, "B");
        PathB2.size = 20;
        paths[3]=PathB2;

       struct Path PathC; //Path c inital Info
        PathC.Direction = 1 ;
        strcpy( PathC.name, "C");
        PathC.size = 20;
        paths[4]=PathC;

       struct Path PathC2; //Path C inital Info The other direction
        PathC2.Direction = 0 ;
        strcpy( PathC2.name, "C");
        PathC2.size = 20;
        paths[5]=PathC2;

       struct Path PathD; //Path D inital Info
        PathD.Direction = 1 ;
        strcpy( PathD.name, "D");
        PathD.size = 20;
        paths[6]=PathD;

       struct Path PathD2; //Path D inital Info The other direction
        PathD2.Direction = 0 ;
        strcpy( PathD2.name, "D");
        PathD2.size = 20;
        paths[7]=PathD;

       struct train *train1;  
       pthread_t train1Th;

       train1->speed = 20;
       train1->CurPath = PathA;
       train1->TrainId = 1;
       train1->direction = 1;
       train1->position = 0;
     pthread_create(&train1Th, NULL, run, (void *)train1);


       struct train *train2;  
       pthread_t train2Th;

       train2->speed = 20;
       train2->CurPath = PathB;
       train2->TrainId = 2;
       train2->direction = 1;
       train2->position = 0;

     pthread_create(&train2Th, NULL, run, (void *)train2);

     struct train *train3;  
     pthread_t train3Th;


   train3->speed = 20;
   train3->CurPath = PathA;
   train3->TrainId = 3;
   train3->direction = 1;
   train3->position = 0;

 pthread_create(&train3Th, NULL, run, (void *)train3);


     struct train *train4;
        train4->TrainId = 4;


       return 0;
    }

当我尝试访问 TrainId 时发生错误

【问题讨论】:

  • 您能否发布完整的代码,正确缩进并使用Train-&gt; 而不是(*Train).
  • 我发布了完整的代码,但是什么是 Train-> 而不是 (*Train)。为了?
  • 并且代码在三个线程“Trains”上工作得很好,但是在尝试添加第四个线程时出现错误
  • Train-&gt;(*Train.) 含义相同,但前者更具可读性。
  • 这是完整的代码吗? train1、train2、train4 似乎没有指向任何东西;这可以解释你的错。在寻求帮助之前编译您的代码并解决合理的警告可能会很好......

标签: c linux pthreads


【解决方案1】:

正如 cmets 中所说,您尝试将值影响到未分配的内存:

    struct train *train1;
    // you declare a pointer to a structure train, but your struct does not exists in memory

    train1->speed = 20;
    // and then, you initialize an unallocated memory with a value

最简单的可能是使用train结构而不是指向结构的指针(您将避免分配和释放内存的问题)并在启动线程时给出结构的地址:

    struct train train1;

    train1.speed = 20;
    [...]
    pthread_create(&train1Th, NULL, run, (void *)&train1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    相关资源
    最近更新 更多