多线程找不到Segmentation fault错误原因

来源:互联网  时间:2016/7/13 1:24:09

关于网友提出的“ 多线程找不到Segmentation fault错误原因”问题疑问,本网通过在网上对“ 多线程找不到Segmentation fault错误原因”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 多线程找不到Segmentation fault错误原因
描述:

多线程

本人写的用多线程读一个文件夹里所有文件,但是报Segmentation fault:11 实在是不知道哪里有错误,麻烦各位大牛帮忙看一下
#include 
#include 
#include 
#include 
#include 
#define MAX_SIZE 100
#define TRUE 1
//Global Variables
int threads_number; //Threads numbers
int files_number; //Files numbers
char *dir_name; //dir name
pthread_mutex_t mutex; //mutex lock
int file_map[MAX_SIZE];
//Function
int count_of_files(DIR *dir);
void read_file(void *thread_id);
//Main Function
int main(int argc, char *argv[])
{
DIR *dir; //directory stream
FILE *file; //file stream
struct dirent *ent; // directory entry structure
char *line = NULL; // pointer to 
size_t len = 1000; //the length of bytes getline will allocate
size_t read;
       
char full_filename[256]; //will hold the entire file name to read
// check the arguments
if(argc != 3)
{
printf("The number of arguments should be Three.\n");
return -1;
}
dir_name = argv[1];
threads_number =  atoi(argv[2]);
//Check parameters legal
if((dir = opendir(argv[1])) == NULL){
printf("The dir %s cannot be opened or exist.\n", argv[1]);
return -1;
}
/*----------------Start-------------------*/
printf("\nDirectory: %s\n", dir_name);
printf("Expected threads number: %d\n", threads_number);
files_number = count_of_files(dir) - 1;
printf("File number to be read: %d\n\n", files_number);
closedir(dir);
//Create and init mutex
pthread_mutex_init(&mutex, NULL);
//Create thread arrays
pthread_t threads[threads_number];
pthread_attr_t attr;
int ret;
//Set default attribute of threads
pthread_attr_init(&attr);
    
    int i;
    
    //Create threads
    printf("Program start......\n\n");
    
    for(i = 0; i < MAX_SIZE; i++){
     file_map[i] = 0;
    }
    for(i = 0; i < threads_number; i++){
     printf("Create thread %d\n", i);
     ret = pthread_create(&threads[i], &attr, read_file, (void *)i);
     if(ret){
     printf("Thread %d is created fail. Error %d", i, ret);
     exit(-1);
     }
    }
    
    for(i = 0; i < threads_number; i++){
     pthread_join(threads[i], NULL);
    }
    
    //Delete mutex   
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
    return 0;
}
int count_of_files(DIR *dir){
int count = 0;
struct dirent *ent;
while((ent = readdir(dir)) != NULL){
if(ent->d_type == DT_REG)
count++;
}
return count;
}
void read_file(void *thread_id){
DIR *dir; //directory stream
FILE *file; //file stream
struct dirent *ent; // directory entry structure
char file_path[256];
size_t read;
char *line = NULL; // pointer to 
size_t len = 1000; //the length of bytes getline will allocate
int tid = (long)thread_id;
//Each threads processes files numbers
int duty_files = files_number / threads_number; 
//Divide files into several parts
int dividing_point = tid * duty_files; 
printf("Thread %d processes %d files. Dividing_point is %d\n", tid, duty_files, dividing_point);
/* print all the files and directories within directory */
if((dir = opendir(dir_name) != NULL)){
int i = dividing_point + 1;
while (TRUE) 
{
int last_one = tid == threads_number - 1; //the last thread process last file
if(last_one && i > files_number)
break;
else if(!last_one && i > dividing_point + duty_files){
break;
}
// generate full path of file
snprintf(file_path, sizeof file_path, "./%s%d.txt\0", dir_name, i);
// open the file
file = fopen(file_path, "r");
// file was not able to be open
if (file == NULL) {
printf("Not found file %s\n", file_path);
}
else{
if(file_map[i] == 1)
continue;
pthread_mutex_lock(&mutex);
file_map[i] = 1;
// Print out each line in the file
while ((read = getline(&line, &len, file)) != -1){
     printf("Retrieved line of length %d:\n", read);
     printf("%s\n", line);
    }
fclose(file);
printf("file %d tid %d\n", i, tid);
pthread_mutex_unlock(&mutex);
}
i++;
}
}
// Close the directory structure
closedir (dir);
pthread_exit(NULL);
}

上一篇使用openssl的AES_encrypt加解密出问题
下一篇求助哈希表的编译出错
明星图片
相关文章
《 多线程找不到Segmentation fault错误原因》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)