使用 C for unix 使用线程根据给定根位置遍历文件系统

发布于 2024-10-20 08:37:34 字数 1672 浏览 1 评论 0原文

我想使用线程和进程遍历文件系统。我的程序必须假设第一个参数要么是提供多进程应用程序的“-p”,要么是以多线程方式运行的“-t”。第二个参数是 文件或目录的路径名。如果我的程序获取文件的路径,它应该打印出文件的大小(以字节为单位)。如果我的程序获取目录的路径,它应该以同样的方式打印出目录名称,然后处理该目录中的所有条目 除目录本身和父目录外的目录。如果给我的程序一个目录,它必须显示以指定目录为根的整个层次结构。我写了一些东西,但我陷入困境。我无法改进我的代码。请帮助我。

我的代码如下:

include

include

include

include

include

include

int

funcThread(DIR *D);

int main(int argc, char * argv[]) { pthread_t 线程[100]; DIR *dir指针; struct stat object_file; 结构体 *object_dir; 整数计数器;

    if(opendir(argv[1])==NULL)
{
    printf("\n\nERROR !\n\n Please enter -p or -t \n\n");
    return 0;
}

if((dirPointer=opendir(argv[1]))=="-t") 
{
    if ((object_dir = opendir(argv[2])) == NULL) 
        {
                printf("\n\nERROR !\n\nPlease enter the third argument\n\n");
                return 0;.
        }
    else
    {   
        counter=0;
        while ((object_dir = readdir(object_dir)) != NULL)
        {
            pthread_create(&thread[counter],NULL,funcThread,(void *) object_dir);
            counter++;
        }

    }

}

返回0; int

funcThread(DIR *dPtr) { 目录 *ptr; struct stat oFile; 结构体 *oDir; 整数;

if(ptr=readdir(dPtr)==NULL)
    rewinddir(ptr); 

if(S_ISDIR(oFile.st_mode)) 
{
    ptr=readdir(dPtr);
    printf("\t%s\n",ptr);
    return funcThread(ptr);
}
else
{
    while(ptr=readdir(dPtr)!=NULL) 
    {
        printf("\n%s\n",oDir->d_name);
        stat(oDir->d_name,&oFile);
        printf("\n%f\n",oFile.st_size);
    }
    rewinddir(ptr); 
}

}

I wanna traverse inside the file system by using threads and processes.My program has to assume the first parameter is either given as "-p" which offers a multi-process application or "-t" which runs in a multi-threaded way. The second parameter is the
pathname of a file or directory. If my program gets the path of a file, it should print out the size of the file in bytes. If my program gets the path of a directory, it should, in the same way, print out the directory name, then process all the entries in the
directory except the directory itself and the parent directory. If my program is given a directory, it must display the entire hierarchy rooted at the specified directory. I wrote something but I got stuck in.I can not improve my code.Please help me.

My code is as following:

include

include

include

include

include

include

include

int funcThread(DIR *D);

int main(int argc, char * argv[])
{
pthread_t thread[100];
DIR *dirPointer;
struct stat object_file;
struct dirent *object_dir;
int counter;

    if(opendir(argv[1])==NULL)
{
    printf("\n\nERROR !\n\n Please enter -p or -t \n\n");
    return 0;
}

if((dirPointer=opendir(argv[1]))=="-t") 
{
    if ((object_dir = opendir(argv[2])) == NULL) 
        {
                printf("\n\nERROR !\n\nPlease enter the third argument\n\n");
                return 0;.
        }
    else
    {   
        counter=0;
        while ((object_dir = readdir(object_dir)) != NULL)
        {
            pthread_create(&thread[counter],NULL,funcThread,(void *) object_dir);
            counter++;
        }

    }

}

return 0;
}

int funcThread(DIR *dPtr)
{
DIR *ptr;
struct stat oFile;
struct dirent *oDir;
int num;

if(ptr=readdir(dPtr)==NULL)
    rewinddir(ptr); 

if(S_ISDIR(oFile.st_mode)) 
{
    ptr=readdir(dPtr);
    printf("\t%s\n",ptr);
    return funcThread(ptr);
}
else
{
    while(ptr=readdir(dPtr)!=NULL) 
    {
        printf("\n%s\n",oDir->d_name);
        stat(oDir->d_name,&oFile);
        printf("\n%f\n",oFile.st_size);
    }
    rewinddir(ptr); 
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装纯掩盖桑 2024-10-27 08:37:34

这行:

if((dirPointer=opendir(argv[1]))=="-t") 

dirPointer 是一个指针 DIR* 那么它怎么能等于文字字符串指针呢?

This line:

if((dirPointer=opendir(argv[1]))=="-t") 

dirPointer is a pointer DIR* so how can it be equal to a literal string pointer?

最笨的告白 2024-10-27 08:37:34

我发现了一些错误:

  1. 你为什么使用 opendir() 来检查你的参数?你应该使用类似 strcmp 的东西。

  2. 您将 struct dirent* 传递给 funcThread(),但 funcThread() 接受一个 DIR*。

  3. 在初始化 funcThread() 之前(通过调用 stat()),您在 funcThread() 上使用了 oFile。

  4. 调用rewinddir()的目的是什么?我猜您正在盲目地尝试让 readdir() 与 struct dirent* 一起使用。

  5. 您正在使用 oDir,但它从未初始化。

  6. 您从多个线程调用 printf() ,却无法同步输出,因此输出将完全无序或出现乱码。

我建议您在使用这些函数之前阅读并理解它们的文档(谷歌“posix function_name”)并熟悉 C 的基础知识。在将线程引入方程式之前,尝试让它在单线程程序上工作。此外,除非您拥有接近那么多的核心,否则使用那么多线程不会看到性能的提高,这实际上会降低性能并增加资源使用量。

I spotted a few errors:

  1. Why are you using opendir() to check your arguments? You should use something like strcmp for that.

  2. You're passing struct dirent* to funcThread() but funcThread() takes a DIR*.

  3. You're using oFile on funcThread() before you initialize it (by calling stat()).

  4. What is the purpose of calling rewinddir()? I guess you're blindly trying to get readdir() to work with a struct dirent*.

  5. You're using oDir but it's never initialized.

  6. You're calling printf() from multiple threads with no means to synchronize the output so it would be completelly out of order or garbled.

I suggest you read and understand the documentation of all those functions before using them (google "posix function_name") and get familiar with the basics of C. And before bringing threads into the equation try to get it working on a single threaded program. Also you won't see an improvement in performance by using that many threads unless you have close to that many cores, it will actually decrease performance and increase resource usage.

热情消退 2024-10-27 08:37:34
if(ptr=readdir(dPtr)==NULL){}

= 运算符的优先级低于 ==

[此错误会重复几次]


if(ptr=readdir(dPtr)==NULL){}

The = operator has lower precedence than ==

[this error is repeated several times]


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文