在过去一天半的空闲时间里,我一直在尝试调试我的代码,我不知道我的代码有什么问题。当我将close()
函数添加到递归调用时,程序会给我一个无效的指针。但是当我删除close()
函数调用时,程序运行正常,除了它没有做它应该做的事情,这是:
相反,它会将输入目录中的所有文件大小相加,并且能够打开最后一个子目录,并将该目录中的文件添加到总文件大小计数中。
我正在尝试使用线程执行此操作。 main()
函数从用户输入目录创建一个主线程,然后运行opendirectory()
。
/*
* Iterates through given directory
*/
void *opendirectory(void *t)
{
pthread_mutex_lock(&dirlock);
DIR *dpntr;
struct dirent *dentry;
char new_directory[512], dir = t;
printf("OPENING DIRECTORY ... %s\n", t);
/* Checks if given directory can be opened */
if((dpntr = opendir(t)) == NULL) {
printf("DIRECTORY FAILED ...%s\n",t);
perror("ERROR -- COULD NOT OPEN DIR");
pthread_exit(NULL);
}
printf("DIRECTORY OPENED: %s\n", t);
/* Read each file in current directory */
while ((dentry = readdir(dpntr)) != NULL ) {
/* Ignore special directories */
if(strcmp(dentry -> d_name, ".") == 0 || strcmp(dentry -> d_name, "..") == 0) {
continue;
} else {
compilelist( t, dentry->d_name );
}
}
pthread_mutex_unlock(&dirlock);
/* Checks if directory can be closed */
if(closedir(dpntr) < 0)
printf("ERROR CLOSING %s.\n", t);
}
这是确定是否应该创建新线程并且应该递归运行的函数。
/*
* Determines if current file is a directory
* Creates a new thread if true
*/
void compilelist (const char* dirname, const char *filename)
{
pthread_mutex_lock(&filelock);
struct stat statdata;
char *filepathname, *dpntr;
/* Allocate memory for filepathname */
if((filepathname = (char *) malloc(sizeof(char) * strlen(dirname))) == NULL)
{
printf("CANNOT ALLOCATE MEMORY FOR FILE PATH NAME.");
pthread_exit(NULL);
}
/* Concats directory name with file name */
if(dirname[strlen(dirname) -1] == '/')
{
pthread_mutex_lock(&pathlock);
sprintf(filepathname, "%s%s", dirname, filename);
pthread_mutex_unlock(&pathlock);
}else
{
pthread_mutex_lock(&pathlock);
sprintf(filepathname, "%s/%s", dirname, filename);
pthread_mutex_unlock(&pathlock);
}
lstat(filepathname, &statdata);
/* Calls print_statdata() if current item is a file */
if(!(S_ISDIR(statdata.st_mode)))
{
printf("FILE: %s\n", filepathname);
if(!stat( filepathname, &statdata))
{
print_statdata( filename, &statdata );
}
else {
fprintf (stderr, "GETTING STAT FOR %s", filepathname);
perror( "ERROR IN STATDATA WHILE GETTING STAT");
}
}
/* Recursive call to opendirectory() */
else {
pthread_mutex_lock(&dircountlock);
dirCount++;
pthread_mutex_unlock(&dircountlock);
dpntr = filepathname;
free(filepathname);
printf("SUB-DIRECTORY THREAD: %s\nTHREAD ID NUMBER: %d\n", dpntr, dirCount);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&threads[dirCount-1], &attr, opendirectory, (void *)dpntr);
}
pthread_mutex_unlock(&filelock);
}
以下是main()
/*
* Main function prompts user for a directory
*/
int main(int argc, char *argv[])
{
int i;
char *dPtr;
// pthread_attr_t attr;
printf("ENTER A DIRECTORY:\n\t");
scanf("%s", directory);
dPtr = directory;
/* Initialize mutex and condition variable objects */
pthread_mutex_init(&mutex, NULL);
pthread_mutex_init(&filelock, NULL);
pthread_mutex_init(&dirlock, NULL);
pthread_mutex_init(&dircountlock, NULL);
pthread_cond_init (&count_threshold_cv, NULL);
/* For portability, explicitly create threads in a joinable state */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&threads[0], &attr, opendirectory, (void *)dPtr);
/* Wait for all threads to complete */
for (i = 0; i < dirCount; i++) {
pthread_join(threads[i], NULL);
}
printf("TOTAL DIRECTORY SIZE: %d\n", dirSize);
/* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&filelock);
pthread_mutex_destroy(&dirlock);
pthread_mutex_destroy(&dircountlock);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);
}
全局变量......
pthread_mutex_t mutex;
pthread_mutex_t dirlock;
pthread_mutex_t filelock;
pthread_mutex_t dircountlock;
pthread_mutex_t threadlock;
pthread_cond_t count_threshold_cv;
pthread_attr_t attr;
pthread_t threads[128]; // handles up to 128 threads (i.e. 128 directories, change accordingly)
char directory[512];
int dirSize = 0;
int dirCount = 1; // user's input directory
我觉得pthread_create()
函数底部调用的compilelist()
无法正常工作。线程[]引用一个全局线程数组,其默认大小为20,假设总目录不超过20个。由于用户的输入目录,dirCount从1开始,并在遇到新目录时增加。
答案 0 :(得分:2)
您的代码:
dpntr = opendir(t)
...
if(closedir(t) < 0)
应该是:
if(closedir(dpntr) < 0)
答案 1 :(得分:1)
在这里,我发现了2个代码问题:
如同提出的争吵,closedir(t)引发了段错误。
“char filepathname [512];” compilelist()是一个本地内存缓冲区,但是你将它传递给你的线程(opendirectory)并连续使用它。您应该使用复制或动态分配。
Effo Upd @ 2009nov17: 修好2点以上,到目前为止我的FC9 x86_64工作正常。顺便说一下:20号线是不够的。
答案 2 :(得分:0)
第一个问题:
每当我有一天半的空闲时间
不要这样做,你的大脑不是为它而建的。分配时间,告诉你的同事/妻子和孩子,如果他们在这段时间打扰你,会有枪声和警察参与: - )
其他问题:不知道(因此社区维基)。