分段错误 p_thread 可能存在竞争条件

2024-03-11

问题:我创建了子线程 TIDS 的链接列表,并希望在继续主线程之前等待所有子线程 TIDS 完成执行。基本上我有目录遍历(目录由给定的成员指定struct)。每次我看到一个目录或文件时,我都会创建一个新线程并将其放入threadID进入链表。但是,当我遍历链表并调用pthread_join我遇到分段错误(核心转储) - 我不明白为什么。我相信可能与比赛条件有关,但我不确定。当我删除pthread_join我不再出现段错误,但是我无法完全遍历目录中的目录。

Program:

void *handy(void *arguments)
{
    static pthread_t threadA;
    threadA=pthread_self();
    handarg *thestruct=(handarg*)arguments; 
    DIR *dp=opendir(thestruct->directory);  
    DIR *outExist=opendir(thestruct->outputdirectory);
    if(outExist==NULL)
    {
        if((thestruct->outputdirectory = ""))
        {
            ;
        }
        else
        {
            printf("The outputdirectory doesnt exist, program terminated\n");
            exit(1);
        }
    }

    struct dirent *entry;  // has two important members (d-type)-tells me if its a directory (d-name) tells me current file name
    if(dp==NULL)
    {
        printf("directory doesnt exist: %s\n", thestruct->directory);
        exit(1);
    }
    NODE *head=NULL; //linked list
    NODE *ptr=(NODE*)malloc(sizeof(NODE));//node POINTER
    int count;

    while((entry=readdir(dp))!=NULL)
    {
        if(strcmp(entry->d_name,"..")==0||strcmp(entry->d_name,".")==0)
        {
            continue;
        }

        if((entry->d_type==DT_DIR))
        {
            unsigned int const sz1 = strlen(thestruct->directory);
            unsigned int const sz2 = strlen(entry->d_name);

            char *dnewname=(char *)malloc((sz1+sz2+2)*sizeof(char));

            memcpy(dnewname,thestruct->directory,sz1);
            memcpy(dnewname+sz1,"/",sizeof(char));
            memcpy(dnewname+sz1+1,entry->d_name,sz2);
            dnewname[strlen(dnewname)]='\0';

            pthread_t tid;

            handarg *current=(handarg*)malloc(sizeof(handarg));//malloc
            current->directory=dnewname;
            current->outputdirectory=thestruct->outputdirectory;
            current->column=thestruct->column;
            pthread_create(&tid,NULL,&handy,current);

            threadA=pthread_self();
            printf("The Filepath is: %s, The Parent TID = %ld, THE peer TID = %ld\n",dnewname, threadA, tid);
            NODE *newNode=(NODE*)malloc(sizeof(NODE));
            newNode->peerTid=tid;
            newNode->next=NULL;
            if(head==NULL)
            {
                head=newNode;
                ptr=newNode;
                //printf("THE TID I PUT INTO THIS NODE IS FUCK YOU FUCK YOU FUCK YOU %ld\n",ptr->peerTid);
            }
            else
            {
                ptr->next=newNode;
                ptr=newNode;
                //printf("THE TID I PUT INTO THIS NODE IS YAMAKA %ld\n",ptr->peerTid);

            }
            //and call wait on each of tids  for loop only exit if last tid is waiting on 
        }

        if((entry->d_type==DT_REG))
        {
            char *filename = entry->d_name;
            int len = strlen(filename);
            const char *last_four = &filename[len-4]; //need to account for if filename is less than 4 characters   
            if(strcmp(last_four,".csv")==0)
            {
                char *dnewname = (char*)malloc(sizeof(char)*(strlen(thestruct->directory)+strlen(entry->d_name)+2));
                char *slash = (char*)malloc(sizeof(char));
                strcpy(dnewname, thestruct->directory);
                strcpy(slash, "/");
                strcat(dnewname, slash);
                strcat(dnewname, entry->d_name);
                dnewname[strlen(dnewname)]='\0';

                pthread_t tid;
                handarg *current=(handarg*)malloc(sizeof(handarg));
                current->directory=dnewname;
                current->outputdirectory=thestruct->outputdirectory;
                current->column=thestruct->column;
                pthread_create(&tid,NULL,&handleFile,current);
                //count++;
                threadA=pthread_self();
                printf("The Filepath is: %s, The Parent TID = %ld, THE peer TID = %ld\n",dnewname, threadA, tid);
                NODE *newNode=(NODE*)malloc(sizeof(NODE));
                newNode->peerTid=tid;
                newNode->next=NULL;
                if(head==NULL)
                {
                    head=newNode;
                    ptr=newNode;

                //printf("THE TID I PUT INTO THIS NODE IS %ld\n",ptr->peerTid);
                }
                else
                {
                    ptr->next=newNode;
                    ptr=newNode;
                    //printf("THE TID I PUT INTO THIS NODE IS %ld\n",ptr->peerTid);
                }
            }
        }   
    }

    printf("the thread produced %d threads\n", count);
    if(head==NULL)      
    {
        printf("no child threads\n");
    }
    ptr=head;
    printf("\nthese are the items from the linkedlist\n\n");
    while (ptr!=NULL)
    {
        printf("%ld\n",ptr->peerTid);           
        ptr=ptr->next;
    }
    printf("\n");
    ptr=head;
    while (ptr!=NULL)
    {
        threadA=pthread_self();
        printf("THE PEER THREAD %ld --------I AM THE THREAD %ld\n",ptr->peerTid,threadA);
        pthread_join(ptr->peerTid,NULL);
        printf("got here");
        ptr=ptr->next;
    }
    printf("\n");
    //return NULL;
}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

分段错误 p_thread 可能存在竞争条件 的相关文章

随机推荐

  • git 添加 .给我总线错误(核心转储)

    我正在使用 ubuntu 并尝试将我的代码放在 github 上 但是当我尝试使用 git add 时 命令时 我收到错误 总线错误 核心转储 joannah joannah Inspiron N5040 Macerdo git init
  • 图 Api - 401 未经授权

    刚刚开始使用 graph office API 哇 这是一个雷区 只是想知道是否有人可以给我任何建议 当我尝试联系时收到 401https graph microsoft com beta me files https graph micr
  • 如何将资产文件夹中的文件路径传递给文件(字符串路径)? [复制]

    这个问题在这里已经有答案了 可能的重复 Android 如何确定资产中特定文件的绝对路径 https stackoverflow com questions 4744169 android how to determine the abso
  • 角度引导模态掩码形式

    我正在尝试在范围内获得角度形式来验证验证等 基本情况 假设我有以下 HTML
  • 如何控制C printf %e中'e'后面的指数位数?

    我想控制C中 e 后面的指数位数printf e 例如 Cprintf e result 2 35e 03 但我想要2 35e 003 我需要3位指数 我该如何使用printf Code include
  • ASP.NET:如何更快地加载页面

    我们用 ASP NET 编写了 Portal 但它有很多 JavaScript 我们的页面加载速度很慢 在某些页面中 页面大小为 1 5 mb 减少或压缩页面大小以使其更快的最佳方法是什么 谢谢 几件事 最小化您的 javascript 和
  • Azure Functions V2 中的 OpenAPI(又名 Swagger)

    我正在创建一个 V2 函数应用程序 并希望使用 Swagger Open API 来处理文档 但 Azure V2 函数门户尚不支持它 关于如何在 VSTS 中使用 Swagger 和 V2 函数在每个构建上创建文档 有什么建议吗 TL D
  • 展平嵌套数组。 (爪哇)

    我正在努力创建正确的逻辑来展平数组 我本质上想为嵌套数组中的每个子项复制父行 嵌套数组的数量可能会有所不同 我一直在创建 Java 列表 因为我发现它们很容易使用 但对任何解决方案都开放 这个问题的本质是我从一些嵌套的 JSON 开始 我想
  • 在不知道结构的情况下使用python读取二进制文件

    我有一个包含 8000 个粒子位置的二进制文件 我知道每个粒子值应该看起来像 24 6151 我不知道我的程序给出的值的精度 我猜它是双精度 但是当我尝试使用以下代码读取文件时 In with open results0epsilon en
  • Symfony2 简单文件上传编辑,无实体

    请帮助我 因为我不敢相信自己的眼睛 我拒绝使用某些第三方插件进行文件上传 并拒绝为文件 文档创建单独的实体 我只想在 Zend Laravel 等中进行简单的文件上传 我有一个发票表 最后一列名称为 附件 我想在此处存储其清理后的名称 例如
  • 误报选项不会出现在项目中

    我在 Ubuntu 12 01 机器上使用 Sonarqube 我使用 Sonar Runner 和 Jenkins 插件来分析我的代码 当我对新项目进行分析时 问题就出现了 例如 我已经保存了 Sonar A 和 B 项目 如果我对该项目
  • 理解 Dymola 错误消息时遇到问题

    谁能告诉我 代数环 的含义 以及我应该如何通过添加 预 运算符来应对这种情况 我真的没看懂 Error Failed to generate code for an algebraic loop involving when equatio
  • 如何动态计算HTML页面表格中每一列的总计?

    我基本上会有一个表格 其中包含一周中的几天 标题行交叉 第 1 栏 周日 第 2 栏 周一等 每个单元格将输入工作时间 即8 最后一行 我希望每个单元格在将数据输入到每个单元格后动态计算其列中其上方单元格的总数 理想情况下 应在将光标移动到
  • 每个外键都有索引?

    每个外键上的索引是否都会优化查询 通常 在外键上放置索引被认为是良好的做法 这样做是因为在将 FK 表链接到包含键定义的表时 它有助于提高联接性能 这不会神奇地使您的整个查询得到优化 但它肯定有助于提高 FK 与其主键对应部分之间的连接性能
  • 如何检测 GHC 默认生成 32 位还是 64 位代码?

    我的里面有以下内容makefile https github com bsl GLFW b blob master Makefile GLFW FLAG m32 O2 Iglfw include Iglfw lib Iglfw lib co
  • jwt 令牌过期后如何注销

    我正在开发一个网络应用程序 使用node js and vue js 我正在使用进行身份验证和维护会话jwt and passport js using passport jwtstrategy 我已经完成了从创建 jwt 到保护路由的所有
  • 如何在 Python 中对编辑距离超过 80% 的单词进行分组

    假设我有一个清单 person name zakesh oldman LLC bikash goldman LLC zikash rakesh 我正在尝试以这种方式对列表进行分组 以便编辑距离 https en wikipedia org
  • 在远程计算机上运行命令

    我想使用 C 在远程计算机上的命令提示符中运行命令 根据此链接如何在远程计算机上执行命令 https stackoverflow com questions 428276 how to execute a command in a remo
  • 我需要调用[super viewDidUnload]吗?

    我见过一些Apple调用的例子 super viewDidUnload 也有一些没有 我读过一篇文章 几个月前所以我不记得网址 说打电话 super viewDidUnload 是不必要的 但除此之外没有解释 是否有明确的理由为什么或为什么
  • 分段错误 p_thread 可能存在竞争条件

    问题 我创建了子线程 TIDS 的链接列表 并希望在继续主线程之前等待所有子线程 TIDS 完成执行 基本上我有目录遍历 目录由给定的成员指定struct 每次我看到一个目录或文件时 我都会创建一个新线程并将其放入threadID进入链表