【问题标题】:Segmentation fault multithreading with file open文件打开时的分段错误多线程
【发布时间】:2016-07-13 11:54:33
【问题描述】:

我创建了一个程序来获取目录中的所有文件,查找各个校验和,然后使用多线程查找总校验和。

我收到一个分段错误,所以我运行 gdb 并看到错误出现在 open() 所在的第 60 行。在研究了 SO 和其他论坛上的 seg 错误之后,我尝试实现一些不同的方法,例如使用 FILE *handle 而不是 int 将 open() 更改为 fopen()。这种改变被证明是不正确的。

经过数小时的调试和搜索,我一无所知,如果有任何见解,我将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>   ///Compile with -pthread or -lpthread
#include <sys/stat.h>

#define BUFFER_SIZE (1<<16)

void cleanup();
void get_filenames();
void* get_checksum();

char **filenames;
int file_cnt;
DIR *dir;

//int handle;
FILE *handle;
unsigned int checksum;
unsigned char* ptr;
int length;
int count;
unsigned char* buffer;
int* sum;
unsigned int total = 0;

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

        int i;
        pthread_t* file;

        atexit(cleanup);
        get_filenames();

        printf("There are %d files:\n", file_cnt);

        file = calloc(sizeof(pthread_t), file_cnt);
        sum = calloc(sizeof(int), file_cnt);
        for(i=0; i<file_cnt; i++){

                printf("%s\n", filenames[i]);

                pthread_create(&(file[i]), NULL, get_checksum, (void*)&filenames[i]);
        }
                for(i=0; i<file_cnt; i++){
                        total += sum[i];
                }
                printf("total is: %u\n", total);
        return EXIT_SUCCESS;
}

void* get_checksum(void* a){

        int b = *((int *)a);

        //handle = open(filenames[b], O_RDONLY); //SEG FAULT HERE
                handle = fopen(filenames[b], "r"); //SEG FAULT HERE
                 if( handle == NULL ){
                        printf( "Can't open file: %s\n", filenames[b]);
                        exit(1);
        }

                buffer = malloc(BUFFER_SIZE);
        if( buffer == NULL ){
                        printf( "Can't get enough memory\n" );
                        exit(1);
                }

                checksum = 0;

                 do{
                        //length = read( handle, buffer, BUFFER_SIZE );
                        length = read( handle, buffer, (sizeof(char)));

                        if( length == -1 ){
                                printf( "Error reading file: %s\n", filenames[b]);
                                        //return NULL;
                                        exit(1);
        }

                        ptr = buffer;
                        count = length;
                        while( count-- ){
                                checksum = checksum + (unsigned int)( *ptr++ );
                                sum[b] = checksum;
                                }
        } while( length );
                printf("Checksum= %d\nTimes at: %d\n", checksum, (int)clock());
}


void cleanup() {

        if(filenames && file_cnt > 0) {
                while(file_cnt-- > 0) {
                        if(filenames[file_cnt]) {
                                free(filenames[file_cnt]);
                        }
                }
                free(filenames);
        }

        if(dir) {
                closedir(dir);
        }

        return;
}


void get_filenames() {

        struct dirent *dir_entry;

        if((dir = opendir(".")) == NULL) {
                fprintf(stderr, "Couldn't open the directory entry for reading\n");
                exit(1);
        }

        errno = 0;
        file_cnt = 0;
        while((dir_entry = readdir(dir)) != NULL) {
                char **new_filenames = filenames;
                static int realative_dirs = 0;

                if(realative_dirs < 2 &&
                   (strcmp(".", dir_entry->d_name) == 0 || strcmp("..", dir_entry->d_name) == 0)
                  ) {
                        realative_dirs++;
                        continue;
                }

                new_filenames = (char **)realloc(filenames, sizeof(char **) * (file_cnt + 1));
                if(new_filenames == NULL) {
                        free(filenames[file_cnt]);
                        fprintf(stderr, "Could not allocate reference for filename[%d]\n", file_cnt);
                        exit(1);
                }

                filenames = new_filenames;
                filenames[file_cnt] = (char *)calloc(strlen(dir_entry->d_name) + 1, sizeof(char));
                if(filenames[file_cnt] == NULL) {
                        fprintf(stderr, "Could not allocate memory for filename[%d]'s string: \"%s\"\n",
                                file_cnt, dir_entry->d_name);
                        exit(1);
                }

                strcpy(filenames[file_cnt], dir_entry->d_name);
                file_cnt++;
        }

        if(errno != 0) {
                fprintf(stderr, "An error occured getting the filenam list\n");
                exit(1);
        }

        return;
}

下面是输出和gdb调试:

There are 24 files:
.windows
.xscreensaver
.alias
.cshrc
Segmentation fault

(gdb) run
Starting program: /home/nolooking/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
There are 24 files:
.windows
[New Thread 0x7ffff781e700 (LWP 15957)]
.xscreensaver
[New Thread 0x7ffff701d700 (LWP 15958)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff781e700 (LWP 15957)]
0x0000000000400d53 in get_checksum (a=0x60b610) at checksum.c:60
60              handle = open(filenames[b], O_RDONLY);
(gdb) backtrace
#0  0x0000000000400d53 in get_checksum (a=0x60b610) at checksum.c:60
#1  0x00007ffff7bc6374 in start_thread () from /lib64/libpthread.so.0
#2  0x00007ffff7907c3d in clone () from /lib64/libc.so.6
(gdb) quit
A debugging session is active.

更新: 我听取了 cmets 中一位用户的建议,他建议我使用: handle=fopen((char*)a, "r");。当 if 语句 if(handle==NULL) 被注释掉时,我可以成功打印出文件名。当我包含该 if 语句时,我会收到以下输出:

There are 24 files:
.windows
.xscreensaver
.alias
.cshrc
Can't open file: p▒`



#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/stat.h>

#define BUFFER_SIZE (1<<16)

void cleanup();
void get_filenames();
void* get_checksum();

char **filenames;
int file_cnt;
DIR *dir;

//int handle;
FILE *handle;
unsigned int checksum;
unsigned char* ptr;
int length;
int count;
unsigned char* buffer;
int* sum;
unsigned int total = 0;

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

        int i;
        pthread_t* file;

        atexit(cleanup);
        get_filenames();

        printf("There are %d files:\n", file_cnt);

        file = calloc(sizeof(pthread_t), file_cnt);
        sum = calloc(sizeof(int), file_cnt);
        for(i=0; i<file_cnt; i++){

                printf("%s\n", filenames[i]);

                pthread_create(&(file[i]), NULL, get_checksum, (void*)&filenames[i]);
        }
                for(i=0; i<file_cnt; i++){
                        total += sum[i];
                }
                printf("total is: %u\n", total);
        return EXIT_SUCCESS;
}

void* get_checksum(void* a){

        int b = *((int *)a);

                handle = fopen(((char*)a), "r");
                if( handle == NULL ){
                        printf( "Can't open file: %s\n", ((char*)a));
                        exit(1);
       }

                buffer = malloc(BUFFER_SIZE);
        if( buffer == NULL ){
                        printf( "Can't get enough memory\n" );
                        exit(1);
                }

                checksum = 0;

                 do{
                        length = read( handle, buffer, BUFFER_SIZE );

                        if( length == -1 ){
                                printf( "Error reading file: %s\n",  ((char*)a));
                                        //return NULL;
                                        exit(1);
        }

                        ptr = buffer;
                        count = length;
                        while( count-- ){
                                checksum = checksum + (unsigned int)( *ptr++ );
                                //sum[a] = checksum;
                                }
        } while( length );
                printf("Checksum= %d\nTimes at: %d\n", checksum, (int)clock());
}


void cleanup() {

        if(filenames && file_cnt > 0) {
                while(file_cnt-- > 0) {
                        if(filenames[file_cnt]) {
                                free(filenames[file_cnt]);
                        }
                }
                free(filenames);
        }

        if(dir) {
                closedir(dir);
        }

        return;
}


void get_filenames() {

        struct dirent *dir_entry;

        if((dir = opendir(".")) == NULL) {
                fprintf(stderr, "Couldn't open the directory entry for reading\n");
                exit(1);
        }

        errno = 0;
        file_cnt = 0;
        while((dir_entry = readdir(dir)) != NULL) {
                char **new_filenames = filenames;
                static int realative_dirs = 0;

                if(realative_dirs < 2 &&
                   (strcmp(".", dir_entry->d_name) == 0 || strcmp("..", dir_entry->d_name) == 0)
                  ) {
                        realative_dirs++;
                        continue;
                }

                new_filenames = (char **)realloc(filenames, sizeof(char **) * (file_cnt + 1));
                if(new_filenames == NULL) {
                        free(filenames[file_cnt]);
                        fprintf(stderr, "Could not allocate reference for filename[%d]\n", file_cnt);
                        exit(1);
                }

                filenames = new_filenames;
                filenames[file_cnt] = (char *)calloc(strlen(dir_entry->d_name) + 1, sizeof(char));
                if(filenames[file_cnt] == NULL) {
                        fprintf(stderr, "Could not allocate memory for filename[%d]'s string: \"%s\"\n",
                                file_cnt, dir_entry->d_name);
                        exit(1);
                }

                strcpy(filenames[file_cnt], dir_entry->d_name);
                file_cnt++;
        }

        if(errno != 0) {
                fprintf(stderr, "An error occured getting the filenam list\n");
                exit(1);
        }

        return;
}

为什么我在取消注释 if 语句后会收到该输出?

【问题讨论】:

  • (void*)&amp;filenames[i] 是 not int b = *((int *)a); 还是我遗漏了什么?
  • 是的。您将错误的东西作为 arg 传递给 pthread_create。 get_checksum 需要一个整数,而您正在传递文件名。
  • @AdrianoRepetti 我不太明白。 check_sum() 被调用并且需要一个 void* a,即 (void*)&filenames[i]。但是, i 在函数 check_sum() 中不可用,因此我创建了一个整数索引。请尽可能解释您的意思。
  • @bruceg 所以我需要将文件名类型转换为整数?
  • 您正在传递一个char*,但您正在转换为一个int*,然后进行延迟以获得char** 的索引。你已经有了文件名,只是handle = fopen((char*)a, "r");

标签: c multithreading segmentation-fault


【解决方案1】:

改一下

  pthread_create(&(file[i]), NULL, get_checksum, (void*)&filenames[i]);

成为

  pthread_create(&(file[i]), NULL, get_checksum, (void*)i);

还有这个

  int b = *((int *)a);

成为

  int b = (int)a;

您也不能在FILE* 上调用read(),因为它是由fopen() 返回的。请改用fread()。

【讨论】:

    【解决方案2】:

    不要使用 &i。我稍后会解释。您传递给线程的参数是错误的 a 不是整数。它是一个指向字符串的指针...

    将线程创建更改为此...

    pthread_create(&(file[i]), NULL, get_checksum, filenames[i]);
    

    然后按如下方式打印字符串...

    void* get_checksum(void *a){
    
      char *file_name = (char *)a;
      printf("filename=%s\n", file_name);
    

    您将字符串作为指向被调用函数的指针传递。在您的代码中,您试图将其用作数组的索引。

    如果您想将索引作为整数传递,请注意...这是行不通的..

    pthread_create(&(file[i]), NULL, get_checksum, &i);
    

    这是多线程的,&i 指向的值随着循环的运行而变化。将指针传递给字符串,并且在任何情况下都不要在线程运行时更改文件名。

    【讨论】:

    • 请看我的更新。我在 cmets 部分实施了一个建议,并且所有文件都已正确打印。但是,当我取消注释 if 语句时,并非所有文件名都可以打印。我在上面的更新中解释得更好。
    • @SamSmith 你要我对我已经给你答案时没有建议的事情发表评论?请阅读我写的内容并理解,即 pthread_create 的最后一个参数是传递给 get_checksum 的内容。您正在传递一个指向字符串的指针并尝试将其用作整数。那根本行不通。
    • 根据您的建议:handle = fopen(*file_name, "r"); 传递参数 1 从整数中生成指针而不进行强制转换。
    • @SamSmith 我没有这么建议。我不确定你为什么认为我让你这样做?
    • 我很抱歉造成混乱。我实施了您的建议,例如pthread_create(&amp;(file[i]), NULL, get_checksum, filenames[i]); 和` char *file_name = (char *)a;'。根据我刚才所说的建议,我尝试使用 fopen 并收到错误“传递参数 1 使指针从整数而不进行强制转换”。
    【解决方案3】:

    我认为您的问题仅仅是因为您传递的是&amp;filenames[i] 而不是简单的&amp;i。

    然后在void* get_checksum(void* a) 中,您尝试使用 char* 作为 int。

    代码会更像:

    for(i=0; i<file_cnt; i++){
    
                    printf("%s\n", filenames[i]);
    
                    pthread_create(&(file[i]), NULL, get_checksum, (void*)&i);
            }
    

    在void* get_checksum(void* a):

    int b = *((int *)a);
    
        handle = fopen(filenames[b], "r");
                 if( handle == NULL ){
                        printf( "Can't open file: %s\n", filenames[b]);
                        exit(1);
        }
    

    【讨论】:

    • 您的诊断是正确的,但解决方案却不是。
    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多