首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用线程时的分割错误

使用线程时的分割错误
EN

Stack Overflow用户
提问于 2018-04-23 13:45:48
回答 1查看 1.6K关注 0票数 1

当我用线程模拟一个地铁站的时候,我遇到了一个分割错误,我的想法是,我有4个车站,4条路径,4列火车,每列火车通过一条路径到达某个车站,而列车通过一条我最初模拟的线程运行,直到我试图进入我的世界的第4线程(“列车”),在我的世界中,我开始得到分割故障(核心哑巴),我得到了这些作为我的结构:

代码语言:javascript
复制
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;

}; 

一些路径初始化器

代码语言:javascript
复制
 struct Path PathA; //Path A inital Info 
    PathA.Direction = 1 ;
    strcpy( PathA.name, "A");
    PathA.size = 20;
    paths[0]=PathA;

一些训练初始化器也是:

代码语言:javascript
复制
   train1->speed = 20;
   train1->CurPath = PathA;
   train1->TrainId = 1;
   train1->direction = 1;
   train1->position = 0;
 pthread_create(&train1Th, NULL, run, (void *)train1);

以及线程的run方法:

代码语言:javascript
复制
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;

    }

在哪里,我只是为路径选择了什么想法,导致分段故障时,试图添加一个新的列车?下面是我整个代码的副本:

代码语言:javascript
复制
    #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时,会发生错误

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-23 15:14:48

正如注释中所述,您试图影响未分配内存的值:

代码语言:javascript
复制
    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

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

代码语言:javascript
复制
    struct train train1;

    train1.speed = 20;
    [...]
    pthread_create(&train1Th, NULL, run, (void *)&train1);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49982749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档