首页 文章

pthread段错误发生在哪里?

提问于
浏览
-5

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

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));
}

edit: 我检查过以确保它适合初始数组,并发现最高ID仅为3000;

1 回答

  • 1
    • 你使用的 strtok() 错了:
    line = strtok(buffer, NULL);
    

    应该

    line = strtok(NULL, delim);
    

    应该同样修复另一个错误 .

    • text_lines 的元素未初始化:
    text_lines = malloc(6000 * sizeof(char*));
    

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

相关问题