使用进程来计算文件数量

Using processes to count number of files

我编写的程序的主要目标是计算目录(和所有子目录)中的文件数。程序每次都应该创建新进程,它会搜索新目录。所以基本上每个目录一个进程。

我的代码:

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    //if wSwtich is set to 1 each process will sleep
    int wSwitch = 0;

   if(getenv("MASTER_OF_PIDS")==NULL)
{
    char *toPass = (char*)malloc(sizeof(char)*10);
    sprintf(toPass,"%ld",(long)getpid());
    setenv("MASTER_OF_PIDS",toPass,1);
}

    char *w = "-w";
    if (argc > 1)
    {
        if (strcmp(argv[1], w) == 0)
        {
            printf("Switch -w set to on \n");
            wSwitch = 1;
        }
    }
    else
    {
        printf("No additional options enabled \n");
    }

    DIR *dir;
    struct dirent *entry;
    struct stat info;
    char * path;
    int counter = 0;
    int files  = 0;
    int childProcesses = 0;
    pid_t pid;

    //checking if path_to_browse was set
    if((path=getenv("PATH_TO_BROWSE"))==NULL)
    {
        path = (char *)malloc(sizeof(char)*512);
        strcpy(path,"./");
    }

    //getting the current dir
    dir = opendir(path);
    if (dir == NULL)
    {
        printf("Error while getting acces to directory ! \n");
        _exit(1);
    }

    while ((entry = readdir(dir)) != NULL)
    {
        char *name = entry->d_name;

        //have to check if this is current directory if so move on

        if(strcmp(name,".")==0 || strcmp(name,"..") == 0 || strcmp(name," ") == 0)
        {
            continue;
        }

        //checking if file i a regular one, if so increase the counter and continue
        if (entry->d_type == DT_REG)
        {
            counter = counter + 1;
            printf("File: %s counter: %d\n",name,counter);
            continue;
        }

        //checking if it is a dir, if so fork and search
        if(entry->d_type == DT_DIR)
        {
            char nextPath[512];
            strcpy(nextPath,path);
            strcat(nextPath,"/");
            strcat(nextPath,entry->d_name);

            //setting the PATH_TO_BROWSE
            //last argument is overwriting
            setenv("PATH_TO_BROWSE",nextPath,1);

            printf("Directory: %s \n",nextPath);
            childProcesses++;
            pid = fork();
            if(pid<0)
            {
                printf("Error ! \n");
                _exit(1);
            }

            int errorChecker = 1;
            if(pid==0)
            {
                if(wSwitch)
                {
                    errorChecker = execlp(argv[0],argv[0],"-w",NULL);
                }
                else
                {
                    errorChecker = execlp(argv[0], argv[0], NULL);
                }
            }

            if(errorChecker == -1)
            {
                printf("Error ! \n");
                _exit(1);
            }
        }
    }

    if(wSwitch)
    {
        printf("I am in the switch! \n");
        sleep(5);
    }

    int status;
    while(childProcesses--)
    {
        wait(&status);
        files += WEXITSTATUS(status);
    }

if(atoi(getenv("MASTER_OF_PIDS"))==getpid())
{
    printf("Counter: %d \n",files + counter);
    return 0;
}
    _exit(counter + files);
}

程序成功搜索所有目录和子目录并正确识别文件。

我的问题:

  1. sleep(5)` 无效。我不知道为什么
  2. 每个进程都正确的对文件个数求和,但是最后如何求和输出?
  3. 我的程序一直没有结束(我想是因为我没有发送return 0)如何正确结束这个程序?

问题1的解答: How to avoid the interruption of sleep calls due to a signal in Linux

由于 wait() 和 sleep() 之间的交互,您可能无法获得预期的行为 - see docs for details.

特别说明:

until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process