Advertisement
STANAANDREY

count vowels kid/parent linux

Nov 2nd, 2024 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10.  
  11. #define VOWELS "aeiou"
  12.  
  13. int main(int argc, char *argv[]) {
  14.     if (argc != 2) {
  15.         fprintf(stderr, "Wrong usage!\n");
  16.         exit(1);
  17.     }
  18.     int pbuff[2];
  19.     if (pipe(pbuff)) {
  20.         perror("pipe");
  21.         exit(1);
  22.     }
  23.     pid_t pid = fork();
  24.     if (pid == -1) {
  25.         perror("");
  26.         exit(-1);
  27.     }
  28.     if (pid == 0) {//kid
  29.         close(pbuff[0]);
  30.         int fd = open(argv[1], O_RDONLY);
  31.         if (fd < 0) {
  32.             perror("open");
  33.             exit(1);
  34.         }
  35.  
  36.         char ch;
  37.         while (read(fd, &ch, 1)) {
  38.             if (strchr(VOWELS, ch)) {
  39.                 write(pbuff[1], &ch, 1);
  40.             }
  41.         }
  42.         close(fd);
  43.         close(pbuff[1]);
  44.         exit(0);
  45.     }
  46.     close(pbuff[1]);
  47.     char ch;
  48.     int cnt = 0;
  49.     while (read(pbuff[0], &ch, 1)) {
  50.         cnt++;
  51.     }
  52.     printf("Cnt: %d\n", cnt);
  53.     close(pbuff[0]);
  54.     int stat;
  55.     wait(&stat);
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement