遍历目录时的C编程段错误

时间:2018-07-23 09:13:31

标签: c multithreading directory segmentation-fault iteration

我需要遍历目录,并对遇到的每个文件执行某种grep。

这是一些代码,我在线程中启动了该函数,因为我希望main继续执行其任务。因为某些原因 它完成了迭代,然后进行了段错误。

为什么会出现段错误?我究竟做错了什么?我该如何解决?

int main(int argc, char **argv){
 int ccn = 1;

  if(ccn == 1){
  pthread_t thread_id;
      char *path = ".";
    pthread_create( &thread_id, NULL, list_dir("."), (void*) path);
}
   /*code continues 
    ...
    ...
   */
 }

>

void *list_dir (const char * dir_name)
{
    DIR * d;

    /* Open the directory specified by "dir_name". */

    d = opendir (dir_name);

    /* Check it was opened. */
    if (! d) {
        fprintf (stderr, "Cannot open directory '%s': %s\n",
                 dir_name, strerror (errno));
        exit (EXIT_FAILURE);
    }
    while (1) {
        struct dirent * entry;
        const char * d_name;

        /* "Readdir" gets subsequent entries from "d". */
        entry = readdir (d);
        if (! entry) {
            /* There are no more entries in this directory, so break
               out of the while loop. */
            break;
        }
        d_name = entry->d_name;
        /* Print the name of the file and directory. */
    printf ("%s/%s\n", dir_name, d_name);

#if 0
    /* If you don't want to print the directories, use the
       following line: */

        if (! (entry->d_type & DT_DIR)) {
        printf ("%s/%s\n", dir_name, d_name);
    }

#endif /* 0 */


        if (entry->d_type & DT_DIR) {

            /* Check that the directory is not "d" or d's parent. */

            if (strcmp (d_name, "..") != 0 &&
                strcmp (d_name, ".") != 0) {
                int path_length;
                char path[PATH_MAX];

                path_length = snprintf (path, PATH_MAX,
                                        "%s/%s", dir_name, d_name);
                printf ("%s\n", path);
                if (path_length >= PATH_MAX) {
                    fprintf (stderr, "Path length has got too long.\n");
                    exit (EXIT_FAILURE);
                }
                /* Recursively call "list_dir" with the new path. */
                list_dir (path);
            }
    }
    }
    /* After going through all the entries, close the directory. */
    if (closedir (d)) {
        fprintf (stderr, "Could not close '%s': %s\n",
                 dir_name, strerror (errno));
        exit (EXIT_FAILURE);
    }
    return 0; // This is demanded for a void * ...
}

1 个答案:

答案 0 :(得分:3)

您的问题是这一行:

pthread_create( &thread_id, NULL, list_dir("."), (void*) path);

pthread_create()需要一个指向新线程应执行的功能的指针。但是编写list_dir(".") 会调用函数(甚至在调用pthread_create()之前)。无论返回什么,都将作为要在线程中执行的函数的地址馈入pthread_create()

在您的情况下,您在函数末尾执行return 0,因此pthread_create()将在最终启动线程时尝试取消引用 nullpointer ,这就是您的崩溃。

要正确传递函数指针,可以使用地址运算符(&list_dir),也可以仅使用函数的标识符,该标识符也将评估为指向函数的指针:

pthread_create( &thread_id, NULL, list_dir, path);

我还删除了此处不必要的void *强制转换:任何数据指针都可以在C中void *的隐式转换。

但是还有另一个问题,您的函数签名当前显示为:

void *list_dir (const char * dir_name)

pthread_create()需要一个带有void *参数的函数。在大多数平台上,它实际上都可以按照您编写它的方式工作,但是仍然是错误的,并且在实践中可以出错,因为在C中无法保证char *指针具有相同的内部表示为void *指针。因此,您必须实际进行转换。将函数更改为采用void *并将指针转换为第一行中的正确类型,例如像这样:

void *list_dir(void *arg) {
    const char *dir_name = arg;
相关问题