C pthread中的Stat错误(S_ISDIR不工作)
我目前正试图编写一个程序,通过为每个新的子目录创建一个线程并使用该线程查找子目录的大小,来查找目录树的大小以及其中所有子目录的大小。这是一个非常简单的程序,但是调试起来非常困难。我有一大堆问题,S_ISDIR无法正常工作(常规文件正在传递if语句,我的程序正在尝试将dir更改为常规文件)。下面是我对这个问题的代码。我希望每个父目录都等待子目录完成,但不希望每个子目录都等待下一个C pthread中的Stat错误(S_ISDIR不工作),c,multithreading,pthreads,pthread-join,C,Multithreading,Pthreads,Pthread Join,我目前正试图编写一个程序,通过为每个新的子目录创建一个线程并使用该线程查找子目录的大小,来查找目录树的大小以及其中所有子目录的大小。这是一个非常简单的程序,但是调试起来非常困难。我有一大堆问题,S_ISDIR无法正常工作(常规文件正在传递if语句,我的程序正在尝试将dir更改为常规文件)。下面是我对这个问题的代码。我希望每个父目录都等待子目录完成,但不希望每个子目录都等待下一个 #define NAMESIZE 256 #define NUM_THREADS 100 #define MAX_PA
#define NAMESIZE 256
#define NUM_THREADS 100
#define MAX_PATH_LENGTH 500
int totalSum = 0;
pthread_mutex_t sum_mutex ;
pthread_mutex_t thread_mutex ;
void *findSize(void *p)
{
int levelSum = 0, numberThreads = 0, i = 0;
DIR *dir ;
struct dirent *entry ;
struct stat entry_stat ;
char cwd[2049] ;
char threads[NUM_THREADS] ;
char paths[NUM_THREADS][MAX_PATH_LENGTH];
char *path = (char*)p ;
// change into the directory that was passed in
if(chdir (p) == -1)
{
perror("chdir");
exit(1);
}
// get current working directory
if(!getcwd (cwd, 2049))
{
perror("getcwd") ;
return;
}
// open the directory to get entries within it
dir = opendir(".") ;
if(!dir)
{
perror("Cannot read directory");
return;
}
while((entry = readdir(dir)))
{
// call stat on the current entry in the directory
if(stat (entry->d_name, &entry_stat) == -1)
{
perror("stat error");
}
// skip the . and .. directories
if(strcmp (entry->d_name, ".") == 0)
continue;
if(strcmp (entry->d_name, "..") == 0)
continue;
// check if current entry is a directory
if(S_ISDIR (entry_stat.st_mode))
{
pthread_mutex_lock(&thread_mutex) ;
strcpy(paths[numberThreads], cwd) ;
strcat(paths[numberThreads], "/") ;
strcat(paths[numberThreads], entry->d_name) ;
pthread_t temp ;
// create new thread in threads array
if (pthread_create(&temp, NULL, findSize, (void *)paths[numberThreads]))
{
fprintf("failed to create thread for directory %s\n ", paths[numberThreads]) ;
exit(1) ;
}
threads[numberThreads] = temp;
// increment the number of threads created on this level of the directory tree
numberThreads++ ;
pthread_mutex_unlock(&thread_mutex) ;
}
if(S_ISREG(entry_stat.st_mode))
{
pthread_mutex_lock(&sum_mutex) ;
int fileSize = entry_stat.st_size ;
levelSum += fileSize ;
totalSum += fileSize ;
pthread_mutex_unlock(&sum_mutex) ;
}
}
void *status ;
for(i = 0; i < numberThreads; i++)
{
pthread_join(threads[i], NULL) ;
}
}
#定义名称大小256
#定义NUM_线程100
#定义最大路径长度500
整数总和=0;
pthread_mutex_t sum_mutex;
pthread_mutex_t thread_mutex;
void*findSize(void*p)
{
int levelSum=0,numberThreads=0,i=0;
DIR*DIR;
结构方向*条目;
结构统计条目_stat;
char-cwd[2049];
字符线程[NUM_线程];
字符路径[NUM_THREADS][MAX_PATH_LENGTH];
char*path=(char*)p;
//更改为传入的目录
if(chdir(p)=-1)
{
perror(“chdir”);
出口(1);
}
//获取当前工作目录
如果(!getcwd(cwd,2049))
{
佩罗(“getcwd”);
返回;
}
//打开目录以获取其中的条目
dir=opendir(“.”);
如果(!dir)
{
perror(“无法读取目录”);
返回;
}
while((entry=readdir(dir)))
{
//对目录中的当前条目调用stat
if(stat(entry->d_name,&entry_stat)=-1)
{
perror(“统计错误”);
}
//跳过.和..目录
如果(strcmp(条目->数据单元名称“.”=0)
继续;
如果(strcmp(条目->数据单元名称,“…”)==0)
继续;
//检查当前条目是否为目录
if(S_ISDIR(输入统计模式))
{
pthread_mutex_lock(&thread_mutex);
strcpy(路径[numberThreads],cwd);
strcat(路径[numberThreads],“/”;
strcat(路径[numberThreads],条目->数据单元名称);
pthread_t temp;
//在线程数组中创建新线程
if(pthread_create(&temp,NULL,findSize,(void*)路径[numberThreads]))
{
fprintf(“未能为目录%s创建线程,\n”,路径[numberThreads]);
出口(1);
}
螺纹[numberThreads]=温度;
//增加在此目录树级别上创建的线程数
numberThreads++;
pthread_mutex_unlock(&thread_mutex);
}
if(S_ISREG(进入统计模式))
{
pthread_mutex_lock(&sum_mutex);
int fileSize=entry\u stat.st\u size;
levelSum+=文件大小;
totalSum+=文件大小;
pthread_mutex_unlock(&sum_mutex);
}
}
无效*状态;
对于(i=0;i
大体上,我只是使用findSize函数和用户传入的路径来创建pthread_。我有很多统计错误,但我不知道如何修复它们。当前目录不是线程本地目录;这是过程的属性。因此,如果每个线程都试图
chdir
,那么您最终会做一些无意义的事情。您需要根据readdir
的结果构造完整路径名,而不使用chdir
,或者使用“*at”接口(openat
,fstatat
,等等)打开相对于目录文件描述符的文件。当前目录不是线程本地;这是过程的属性。因此,如果每个线程都试图chdir
,那么您最终会做一些无意义的事情。您需要根据readdir
的结果构造完整路径名,而不使用chdir
,或者使用“*at”接口(openat
,fstatat
,等等)打开与目录文件描述符相关的文件。我已删除所有打印语句,但是我的代码中的打印语句显示试图将目录更改为子目录中的文件(这应该是不可能的,因为我检查了S_ISDIR)。您不需要自己的MAX_PATH_LENGTH
,系统头文件已经为您提供了PATH_MAX
。我已经删除了所有打印语句,但是我的代码中的print语句显示试图将目录更改为子目录中的文件(这不可能,因为我检查了S_ISDIR)您不需要自己的MAX_PATH_LENGTH
,系统头文件已经为您提供了PATH_MAX
。好的,我正在尝试在没有chdir的情况下重写代码,我会在运行某些东西时更新。我仍然会遇到opendir试图打开常规文件的问题。opendir应该只在新线程上调用,这些线程只为新的Directories创建。Rhaps使用运行它的目录树的描述更新你的问题(如果其中包含私有信息,请在更简单的示例树上尝试,看看是否可以重现问题)。好的,我正在尝试在没有chdir的情况下重写代码,我会在运行某些东西时更新。我仍然会遇到opendir试图打开常规文件的问题。opendir只应在新线程上调用,而新线程仅为新的Directories创建。使用运行问题的目录树的描述更新问题(如果其中包含私有信息,请在更简单的示例树上尝试,看看是否可以重现问题)。