samedi 28 mars 2015

Using processes to count number of files

I wrote the program which main goal is to count number of files that are located in the directory (and all the subdirectories). Program should create new process every time, it searches new directory. So basically one process per directory.


My code:



#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);
}


Program succeed in searching all directories and subdirectories correctly identifying files.


My problems:



  1. sleep(5)` has no effect. I have no idea why

  2. Each process correctly sum the number of the files, but at the end, how to sum them all and output?

  3. My program is never ending (I think it is because I am not sending return 0) How to end this program correctly?


Solution of problem 1: How to avoid the interruption of sleep calls due to a signal in Linux


Aucun commentaire:

Enregistrer un commentaire