使用进程计算文件数

时间:2015-03-28 00:35:28

标签: c process

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

我的代码:

#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. 睡觉(5)`没有效果。我不知道为什么
  2. 每个进程都正确地总结了文件的数量,但最后,如何对它们求和并输出?
  3. 我的程序永无止境(我认为是因为我没有发送return 0)如何正确地结束这个程序?
  4. 问题1的解决方案: How to avoid the interruption of sleep calls due to a signal in Linux

1 个答案:

答案 0 :(得分:1)

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

具体说明:

  

直到参数秒指定的实时秒数已经过去或者信号被传递到调用线程并且其操作是调用信号捕获函数或终止进程< / p>