I am trying to print the words couted by the child processes and add them up.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
count_t word_count(char *file)
{
-counts the words from a file---
}
int main(int argc, char **argv)
{
int i,j, numFiles,pid_count[10],pid;
int global;
numFiles = atoi(argv[1]);
printf("counting %d files..\n", numFiles);
j=0;
for(i = 0; i < numFiles; i++)
{
pid_count[i] = fork();
if(pid_count[i] < 0) {
printf("Error creating the child process\n");
} else if (pid_count[i] == 0) {
char filename[100];
sprintf(filename, "%s/text.%02d", FILEPATH, i);
printf("read: %s\n", filename);
int local;
printf("Child PID: %d Handling File No : %d\n",getpid(), i);
local = word_count(filename);
global += local;
exit(0);
} else {
continue;
}
}
int wstatus;
for(i = 0; i < numFiles; i++)
{
waitpid(pid_count[i], &wstatus, 0);
}
printf("total word count is %d",global);
return(0);
}
but in the output the global variable prints 0 only while each local variable has the exact count, it just dont add up to global