The code I've written finds out the number of words in multiple text files by creating multiple processes with each process being responsible for one file to count its words.

What I want to do is using pipes to find out total number of words in all files.

So the parent should:

1. creates a pipe between the each child and itself so it can get the number of words from each child
2. reports the total number of words in all the files by adding the numbers received through pipes
3. checks the exit status of each child and prints out how that child
exited

also let each child:

1. sends the number of the words to the parent via the pipe
2. send 0 as word count through the pipe to the parent if the file does
not exist or any other error happens
3. returns/exits with 0 if it is successfull in opening the file and
counting the words in that file, returns/exits with 1 if there is an
error (e.g., file does not exist etc.)

```
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#define MAX_CHAR 100

pid_t getpid(void);
pid_t getppid(void);

char* itoa(int i, char b[]){
    char const digit[] = "0123456789";
    char* p = b;
    if(i<0){
        *p++ = '-';
        i *= -1;
    }
    int shifter = i;
    do{ //Move to where representation ends
        ++p;
        shifter = shifter/10;
    }while(shifter);
    *p = '\0';
    do{ //Move back, inserting digits as u go
        *--p = digit[i%10];
        i = i/10;
    }while(i);
    return b;
}
int countWords(char * fp, int pid) {
   FILE * file;
   int words = 0;
   char word[MAX_CHAR];
   //execute this function only if child process of parent, no gradchild is allowed to execute this function!
   if (pid == getppid()) {
      file = fopen(fp, "r");
      if (file == NULL) {
         return -1;
      }
      //find string in the file and count the words.
      while (fscanf(file, "%s", word) != EOF) {
         words++;
      }
      return words;
   } else {
      return -1;
   }
   return 0;
}

int main(int argc, char * arvg[]) {
   //if invalid arguments
   if (argc < 2) {
      fprintf(stderr, "ERROR: INVALID ARGUMENTS");
      exit(-1);
   }

   int count = 0, pid, ppid, status, totalwords;

   int result = -1;
   int fd[2];
   char string[100];
   char readbuffer[80];
   int *write_fd = &fd[1];
   int *read_fd = &fd[0];
   result = pipe(fd);
   if(-1 == result){
     perror("pipe");
     return -1;
   }
   //creates (argc - 1) child processes using fork()
   pid = (int) malloc((argc - 1) * sizeof(int));
   //parent pid
   ppid = getpid();

   //each child process to count the number of words in each file
   for (int i = 1; i < argc; i++) {
      //child process
      pid = fork();
      if( pid == -1){
          perror("failed to fork");
          return -1;
      }else if (pid == 0) {
         // call a function to count the number of words in file arvg[i]
         int words = countWords(arvg[i], ppid);
         close(*read_fd);
         if (words >= 0) {
            printf("Child process pid_%d for %s :number of words is %d\n", i, arvg[i], words);
            //I don't know how to write int into the pipe,so below might be wrong
            write(*write_fd, words, 1);

            return 0;
         } else if (words == -1) {
            printf("Child process pid_%d for %s :does not exists\n", i, arvg[I]);
            //I don't know how to write int into the pipe,so below might be wrong
            write(STDOUT_FILENO, words, 1);

            exit(1);
         }
      } else {
         close(*write_fd);

         //and I have no idea how to read int from pipes
         read(*read_fd, &readbuffer, 1);

         totalwords += ???
         close(*read_fd);
         //Wait until all child processes exit/return
         if (ppid == getpid()) {
            wait( & status);
         }
         //inspect their exit codes, WEXITSTATUS = return code when child exits
         if (WEXITSTATUS(status) == 1) {
            count++;
         }
      }
   }
   printf("Main process created %d child processes to count words in %d files\n", argc - 1, argc - 1);
   printf("Total words is %d", totalwords);
   printf("%d files have been counted sucessfully!\n", argc - 1 - count);
   printf("%d files did not exist.\n", count);

   return 0;
}```

Can someone help me to figure out this? I don't really know how to achieve my goal with pipe.

```