2014-03-25 43 views
-5

在我的程序中,我提供了一个包含文本文件的目录。每一个文本文件包含几百行的格式如下pthread segfault在哪里发生?

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;

回答

1
  1. 您使用的strtok()是错误的:

    line = strtok(buffer, NULL); 
    

    应该

    line = strtok(NULL, delim); 
    

    另一个错误,应同样固定。

  2. text_lines的元素是未初始化:

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

    这种分配6000个指针char,但这些指针被初始化。