【问题标题】:where is the pthread segfault happening?pthread 段错误发生在哪里?
【发布时间】:2025-11-25 11:00:02
【问题描述】:

在我的程序中,我提供了一个包含文本文件的目录。每个文本文件包含数百行,格式如下

Username,Password,BloodType,Domain,Number

然后我为目录中的每个文件创建一个线程,它将这些行合并排序(按编号)到数组 char* text_lines[6000];

我不知道为什么会出现分段错误,因为每次运行都会得到不同的输出。

这是我的代码:

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

void store_line(char* line);
void* my_merge_sort(void* file);

char** text_lines;

int main(int argc, char* argv[])
{

    if(argc != 2)
    {
        fprintf(stderr, "usage: ./coolsort <directory>\n");
    }
    else
    {
        text_lines = malloc(6000 * sizeof(char*));
        DIR* the_directory;
        int filecount = 0;
        struct dirent* directory_files[50];
        if((the_directory = opendir(argv[1])) != NULL)
        {
            //make a list of the files in the directory
            while((directory_files[filecount++] = readdir(the_directory))) ;
            filecount--;

            //<<<DEBUGGING INFO>
            int i;
            fprintf(stderr,"there are %i files in %s:\n", filecount, argv[1]);
            for(i = 0; i < filecount; i++)
            {
                fprintf(stderr, "%s\n",directory_files[i]->d_name);
            }
            char cwd[512];
            chdir(argv[1]);
            getcwd(cwd, sizeof(cwd));
            fprintf(stderr, "the CWD is:  %s\n", cwd);
            //<DEBUGGING INFO>>>

            //lets start some threads
            pthread_t threads[filecount-2];
            int x = 0;
            for(i = 0; i < (filecount); i++ )
            {
                if (!strcmp (directory_files[i]->d_name, "."))
                    continue;
                if (!strcmp (directory_files[i]->d_name, ".."))    
                    continue;
                pthread_create(&threads[x++], NULL, my_merge_sort, (void*)directory_files[i]->d_name);
            }
            //do stuff here

            //
        }
        else
        {
            fprintf(stderr, "Failed to open directory: %s\n", argv[1]);
        }
    }
}

void* my_merge_sort(void* file)
{
    fprintf(stderr, "We got into the function!\n");
    FILE* fp = fopen(file, "r");
    char* buffer;
    char* line;
    char delim[2] = "\n";
    int numbytes;

    //minimize I/O's by reading the entire file into memory;
    fseek(fp, 0L, SEEK_END);
    numbytes = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
    buffer = (char*)calloc(numbytes, sizeof(char));
    fread(buffer, sizeof(char), numbytes, fp);
    fclose(fp); 

    //now read the buffer by '\n' delimiters
    line = strtok(buffer, delim);
    fprintf(stderr, "Heres the while loop\n");
    while(line != NULL)
    {
        store_line(line);
        line = strtok(buffer, NULL);
    }
    free(buffer);
}

void store_line(char* line)
{   
    //extract the ID.no, which is the fifth comma-seperated-token. 
    char delim[] = ",";
    char* buff;
    int id;
    int i;
    strtok(line, delim);
    for(i = 0; i < 3; i++)
    {
        strtok(line, NULL);
    }
    buff = strtok(line, NULL);
    id = atoi(buff);

    //copy the line to text_lines[id]
    memcpy(text_lines[id], line, strlen(line));
}

编辑:我检查确定它是否适合初始数组,发现最高ID只有3000;

【问题讨论】:

    标签: c segmentation-fault pthreads mergesort


    【解决方案1】:
    1. 你使用strtok()是错误的:

      line = strtok(buffer, NULL);
      

      应该是

      line = strtok(NULL, delim);
      

      另一个错误应该类似地修复。

    2. text_lines 的元素未初始化:

      text_lines = malloc(6000 * sizeof(char*));
      

      这分配了 6000 个指向 char 的指针,但这些指针都没有被初始化。

    【讨论】:

      最近更新 更多