SHARE
TWEET

Untitled

a guest Sep 20th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/wait.h>
  9.  
  10. #include "word_count.h"
  11. #include "word_helpers.h"
  12.  
  13. /*
  14.  * Read stream of counts and accumulate globally.
  15.  */
  16. void merge_counts(word_count_list_t *wclist, FILE *count_stream) {
  17.   char *word;
  18.   int count;
  19.   int rv;
  20.   while ((rv = fscanf(count_stream, "%8d\t%ms\n", &count, &word)) == 2) {
  21.     add_word_with_count(wclist, word, count);
  22.   }
  23.   if ((rv == EOF) && (feof(count_stream) == 0)) {
  24.     perror("could not read counts");
  25.   } else if (rv != EOF) {
  26.     fprintf(stderr, "read ill-formed count (matched %d)\n", rv);
  27.   }
  28. }
  29.  
  30. /*
  31.  * main - handle command line, spawning one process per file.
  32.  */
  33. int main(int argc, char *argv[]) {
  34.   /* Create the empty data structure. */
  35.   word_count_list_t word_counts;
  36.   init_words(&word_counts);
  37.  
  38.   if (argc <= 1) {
  39.     /* Process stdin in a single process. */
  40.     count_words(&word_counts, stdin);
  41.   } else {
  42.     int pid_lst[argc - 1];
  43.     int pipefd[argc - 1][2];
  44.     for (int i = 1; i < argc; i++) {
  45.        if (pipe(pipefd[i - 1]) == -1) {
  46.          perror("pipe");
  47.          exit(EXIT_FAILURE);
  48.        }
  49.     }
  50.     for (int i = 1; i < argc; i++) {
  51.       int pid = fork();
  52.       if (pid < 0) {
  53.         perror("fork");
  54.         exit(EXIT_FAILURE);
  55.       } else if (pid == 0) {
  56.          pid_lst[i - 1] = pid;
  57.          FILE *infile = fopen(argv[i], "r");
  58.          if (infile == NULL) {
  59.             perror("fopen");
  60.          }
  61.          //printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
  62.          count_words(&word_counts, infile);
  63.          close(pipefd[i - 1][0]); //close read
  64.          for (int j = 1; j < argc; j++) {
  65.             if (j != i) {
  66.                 close(pipefd[j - 1][1]);
  67.             }
  68.          }
  69.          FILE *stream = fdopen(pipefd[i - 1][1], "w");
  70.          fprint_words(&word_counts, stream);
  71.          fclose(stream);
  72.          exit(0);
  73.       }
  74.     }
  75.     for (int i = 1; i < argc; i++) {
  76.        waitpid(pid_lst[i - 1], NULL, 0);
  77.        close(pipefd[i - 1][1]); //close write
  78.        FILE *stream = fdopen(pipefd[i - 1][0], "r");
  79.        merge_counts(&word_counts, stream);
  80.        fclose(stream);
  81.     }
  82.   }
  83.   /* Output final result of all process' work. */
  84.   wordcount_sort(&word_counts, less_count);
  85.   fprint_words(&word_counts, stdout);
  86.   return 0;
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top