|
| 1 | +/* |
| 2 | + * usage: ./a.out input_file text_pattern |
| 3 | + * Executes the command "cat input_file | grep text_pattern | cut -b 1-10". |
| 4 | + * Note only minimal error checking is done for simplicity/shortness of code. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include <fcntl.h> |
| 10 | +#include <sys/types.h> |
| 11 | +#include <sys/stat.h> |
| 12 | +#include <wait.h> |
| 13 | + |
| 14 | +int main(int argc, char **argv) |
| 15 | +{ |
| 16 | + int status; |
| 17 | + int i; |
| 18 | + |
| 19 | + if (argc == 3) |
| 20 | + { |
| 21 | + // arguments for commands |
| 22 | + char *cat_args[] = {"cat", argv[1], NULL}; |
| 23 | + char *grep_args[] = {"grep", argv[2], NULL}; |
| 24 | + char *cut_args[] = {"cut", "-b", "1-10", NULL}; |
| 25 | + |
| 26 | + // file descriptors for 2 pipes: fd1 for cat-to-grep, fd2 for grep-to-cut |
| 27 | + int fd1[2], fd2[2]; |
| 28 | + |
| 29 | + // make pipe for cat to grep |
| 30 | + pipe(fd1); |
| 31 | + |
| 32 | + // fd1[0] = read end of cat->grep pipe (read by grep) |
| 33 | + // fd1[1] = write end of cat->grep pipe (written by cat) |
| 34 | + |
| 35 | + // make pipe for grep to cut |
| 36 | + pipe(fd2); |
| 37 | + |
| 38 | + // fd2[0] = read end of grep->cut pipe (read by cut) |
| 39 | + // fd2[1] = write end of grep->cut pipe (written by grep) |
| 40 | + |
| 41 | + // fork the first child (to execute cat) |
| 42 | + if (fork() == 0) |
| 43 | + { |
| 44 | + // duplicate write end of cat->grep pipe to stdout |
| 45 | + dup2(fd1[1], 1); |
| 46 | + |
| 47 | + // close both ends of all created fd# pipes (very important!) |
| 48 | + close(fd1[0]); |
| 49 | + close(fd1[1]); |
| 50 | + close(fd2[0]); |
| 51 | + close(fd2[1]); |
| 52 | + |
| 53 | + execvp(*cat_args, cat_args); |
| 54 | + } |
| 55 | + else // parent (assume no error) |
| 56 | + { |
| 57 | + // fork second child (to execute grep) |
| 58 | + if (fork() == 0) |
| 59 | + { |
| 60 | + // duplicate read end of cat->grep pipe to stdin (of grep) |
| 61 | + dup2(fd1[0], 0); |
| 62 | + |
| 63 | + // duplicate write end of grep->cut pipe to stdout (of grep) |
| 64 | + dup2(fd2[1], 1); |
| 65 | + |
| 66 | + // close both ends of all created fd# pipes (very important!) |
| 67 | + close(fd1[0]); |
| 68 | + close(fd1[1]); |
| 69 | + close(fd2[0]); |
| 70 | + close(fd2[1]); |
| 71 | + |
| 72 | + execvp(*grep_args, grep_args); |
| 73 | + } |
| 74 | + else // parent (assume no error) |
| 75 | + { |
| 76 | + // fork third child (to execute cut) |
| 77 | + if (fork() == 0) |
| 78 | + { |
| 79 | + // duplicate read end of grep->cut pipe to stadin (of cut) |
| 80 | + dup2(fd2[0], 0); |
| 81 | + |
| 82 | + // close both ends of all created fd# pipes (very important!) |
| 83 | + close(fd1[0]); |
| 84 | + close(fd1[1]); |
| 85 | + close(fd2[0]); |
| 86 | + close(fd2[1]); |
| 87 | + |
| 88 | + execvp(*cut_args, cut_args); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // only the parent gets here, close all pipes and wait for 3 children to finish |
| 94 | + close(fd1[0]); |
| 95 | + close(fd1[1]); |
| 96 | + close(fd2[0]); |
| 97 | + close(fd2[1]); |
| 98 | + |
| 99 | + for (i = 0; i < 3; i++) |
| 100 | + { |
| 101 | + wait(&status); |
| 102 | + } |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + printf("usage: %s input_file text_pattern\n", argv[0]); |
| 107 | + } |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
0 commit comments