#include #include #include #include #include #include #include #include int main () { int pid; char c; int goodfd, badfd, infd; int pipefds[2]; unlink ("pipe"); if (mkfifo ("pipe", S_IRUSR | S_IWUSR) < 0) { perror ("mkfifo"); exit (1); } infd = open ("pipe", O_RDONLY); if (infd < 0) { perror ("open"); exit (1); } /* Create good child */ if (pipe (pipefds) < 0) { perror ("pipe (good)"); exit (1); } pid = fork (); if (pid < 0) { perror ("fork (good)"); exit (1); } if (pid == 0) { /* Good Child, first Element in pipe (sort) */ /* Close write end of pipe and assign read end to stdin */ close (pipefds[1]); close (0); dup (pipefds[0]); /* Close duplicate file descriptior */ close (pipefds[0]); /* Create a new pipe for standard output */ if (pipe (pipefds) < 0) { perror ("pipe"); exit (1); } pid = fork (); if (pid < 0) { perror ("fork"); exit (1); } if (pid) { /* Second Child (uniq) */ /* Close write end of pipe and assign read end to * stdin */ close (pipefds[1]); close (0); dup (pipefds[0]); close (pipefds[0]); printf ("Hier Toepfchen:\n"); execlp ("uniq", "uniq", "-c", NULL); perror ("execlp"); exit (1); } /* Close read end of pipe */ close (pipefds[0]); /* Assign write end of pipe to stdin */ close (1); dup (pipefds[1]); close (pipefds[1]); execlp ("sort", NULL); perror ("execlp"); exit (1); } close (pipefds[0]); goodfd = pipefds[1]; /* Create Bad child */ if (pipe (pipefds) < 0) { perror ("pipe (bad)"); exit (1); } pid = fork (); if (pid < 0) { perror ("fork (bad)"); exit (1); } if (pid == 0) { /* Bad Child */ int ret; int count = 0; /* Close write end of pipe and assign read end to infd */ close (pipefds[1]); infd = pipefds[0]; while (1) { ret = read (infd, &c, 1); if (ret == 0) /* End of file */ break; if (ret < 0) { perror ("Warning: read"); } count ++; } printf ("Hier Kroepfchen: %d\n", count); exit (0); } close (pipefds[0]); badfd = pipefds[1]; /* Main Program */ while (1) { int ret; ret = read (infd, &c, 1); if (ret < 0) { perror ("Warning: read"); continue; } if (ret == 0) { close (infd); break; } if (strchr ("gutGUT", c)) { char buf[2]; buf[0] = c; buf[1] = '\n'; if (write (goodfd, buf, 2) < 0) { perror ("write"); } } else if (strchr ("schlechtSCHLECHT", c)) { if (write (badfd, &c, 1) < 0) { perror ("write"); } } } /* Signal end of file to children */ close (goodfd); close (badfd); /* Wait for termination of children */ wait (NULL); wait (NULL); return 0; }