我需要遍历目录并对遇到的每个文件执行某种 grep 操作。
这是一些代码,我在一个线程中启动了该函数,因为我想继续执行它的任务。因为某些原因 它完成迭代,然后出现段错误。
为什么会出现段错误?我究竟做错了什么?我应该如何解决这个问题?
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 * ...
}
最佳答案
你的问题是这一行:
pthread_create( &thread_id, NULL, list_dir("."), (void*) path);
pthread_create()
需要一个指向新线程应该执行的函数的指针。但是编写 list_dir(".")
调用函数(甚至在调用 pthread_create()
之前)。无论它返回什么,都会作为要在线程中执行的函数的地址提供给 pthread_create()
。
在您的例子中,您在函数末尾执行了return 0
,因此pthread_create()
将尝试取消引用空指针终于开始线程了,这就是你崩溃的原因。
要正确传递函数指针,您可以使用寻址运算符 (&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;
关于遍历目录时的 C 编程段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51475294/