分段错误 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 可能存在竞争条件 的相关文章