gpt4 book ai didi

c - 使用线程和互斥锁来搜索目录

转载 作者:行者123 更新时间:2023-11-30 16:38:24 24 4
gpt4 key购买 nike

我是线程新手,我相信我理解这个概念。由于锁是使用线程的必要工具,但(或至少对我来说)对如何使用感到困惑,我需要使用它们,但似乎无法正确使用它们。这里的想法是搜索目录以查找 CSV 文件。 (更多的工作将在 CSV 上完成,但这与这里无关)我有一个算法来搜索目录,无需使用线程即可正常工作。 (请记住,搜索目录是一种非常适合递归的任务,因为您需要搜索一个目录来查找另一个目录,当您找到新目录时,您想要搜索该目录)因为我需要使用线程在查找新目录的每个实例中,我都设置了两次相同的算法。一旦进入主目录,它就会找到目录并调用一个函数(通过线程)来搜索找到的目录。同样,如果我在没有线程的情况下使用此方法,则问题为零,但通过线程,我发送到函数的参数将被覆盖。即使我锁定整个函数也会发生这种情况。显然我没有正确使用锁和线程,但我不明白哪里出错了。我有测试目录来验证它是否正常工作。我在“.”中有3个目录。目录以及除此之外的子目录。它可以很好地找到前三个目录(在主目录中),然后当它将这些目录传递给线程函数时,它将搜索三次不同的时间,但通常会多次搜索同一目录。换句话说,路径名似乎被覆盖了。我将发布代码,以便您可以看到我在做什么。我提前谢谢你。完整代码链接:sorter.h https://pastebin.com/0vQZbrmh sorter.c https://pastebin.com/9wd8aa74 dirWorker.c https://pastebin.com/Jd4i1ecr

在 sorter.h 中

#define MAXTHREAD 255
extern pthread_mutex_t lock;
typedef
struct _dir_proc
{
char* path; //the path to the new found directory
char* colName; //related to the other work that must be done
} dir_proc;

在 sorter.c 中

#include <pthread.h>
#include <assert.h>
#include <dirent.h>
#include "sorter.h"

pthread_mutex_t lock;
int main(int argc, char* argv[])
{
int err = 0;
pthread_t threads[MAXTHREAD];
DIR *dirPointer;
char* searchedDirectory = ".";
struct dirent *directEntry;
dir_proc *dir_proc_args = malloc(sizeof(struct _dir_proc));
assert(dir_proc_args != NULL);
dir_proc_args->path = (char*) malloc(256 * (sizeof(char));
assert(dir_proc_args->path != NULL);
dir_proc_args->colName = (char*) malloc(256 * sizeof(char));
assert(dir_proc_args->colName != NULL);
pthread_mutex_init(&lock, NULL)

//dir_proc_args->colName is saved here
if(!(dirPointer = opendir(searchedDirectory)))
{
fprintf(stderr, "opening of directory has failed");
exit(1);
}

while((directEntry = readdir(dirPointer)) != NULL)
{
//do stuff here to ensure it is a directory
//ensure that the dir we are looking at is not current or parent dir
//copy path of found directory to dir_proc_args->path
err = pthread_create(&threads[count++], NULL, &CSVFinder, (void*)dir_proc_args);
if(err != 0)
printf("can't create thread);
}
int i;
for(i=0; i < count; ++i)
{
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(&lock);
}

在 CSVFinder 函数中

#include <assert.h>
#include <pthread.h>
#include "sorter.h"
#include <dirent.h>

void *CSVFinder(void *args)
{
pthread_mutex_lock(&lock); //I have locked the entire function to see I can get it to work. this makes no sense to actually do
DIR *dirPointer;
struct dirent *directEntry;
dir_proc *funcArgs = (struct _dir_proc*)args;
char path[255];
strncpy(path, funcArgs->path, sizeof(path));

if(!(dirPointer = opendir(funcArgs->path)))
{
fprintf(stderr, "opening of directory has failed");
exit(1);
}
while((directEntry = readdir(dirPointer)) != NULL)
{
if(directEntry->d_type == DT_DIR) //if we are looking at a directory
{
//make sure the dir we are looking at is not current or parent dir
snprintf(funcArgs->path, (sizeof(path) + sizeof(directEntry->d_name)), "%s/%s", path, directEntry->d_name);
//I would like to be able to do a recursive call here
//to search for more directories but one thing at a time
}
}
closedir(dirPointer);
pthread_mutex_unlock(&lock);
return(NULL);
}

我希望我没有遗漏任何相关代码。我试图将代码保持在最低限度,同时不遗漏任何必要的内容。

最佳答案

我不清楚为什么要创建一个线程来简单地遍历目录结构。不过,我会指出一些我看到的问题。

一个小问题是您在 CSVFinder 函数中调用了 readder,而不是 readdir。

但对我来说一个明显的问题是您没有在 main 或 CSVFinder() 函数中初始化 dirPointer。我希望看到这样的电话

dirPointer = opendir("/");

在 main() 函数中 while 循环之前。

然后我希望看到 CSVFinder() 通过调用 opendir(path) 来初始化其 dirPointer,其中 path 是主循环中找到的子目录的名称。

有关如何遍历目录结构的良好引用,请转到此处...

https://www.lemoda.net/c/recursive-directory/

关于c - 使用线程和互斥锁来搜索目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47500583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com