#include #include #include #include #include #include #include #define IO_SIZE 4096 int main(int argc, char **argv) { int pipes[2]; int pid; int file; int io; void *buf; if (argc != 2) { printf("Usage: %s \n", argv[0]); return 0; } if ((pid = fork()) == -1) { perror("Could not fork"); return 1; } if (pipe(pipes) == -1) { perror("Could not open pipes"); return 1; } if (pid == 0) { close(pipes[1]); if (dup2(pipes[0], fileno(stdin)) == -1) { perror("Could not set standard input of the child"); return 1; } close(pipes[0]); /* fprintf(stderr, "Starting child process.\n"); execlp("nl", "nl", "-l1", (char *) NULL); fprintf(stderr, "Whoops...\n"); */ char c; int lines = 1; int chars = 0; fprintf(stderr, "Consumer: Start reading shit.\n"); while ((c = getchar()) != EOF) { if (c == '\n') lines++; chars++; fprintf(stderr, "Consumer: Read a char. %d thus far.\n", chars); } fprintf(stderr, "Consumer: Lines: %d\n", lines); fprintf(stderr, "Consumer: Chars: %d\n", chars); } else { int status; buf = malloc(sizeof(char) * IO_SIZE); file = open(argv[1], O_RDONLY); while ((io = read(file, buf, IO_SIZE)) > 0) { fprintf(stderr, "Producer: Read %dbytes\n", io); if ((io = write(pipes[1], buf, io)) == -1) { perror("Producer: Couldn't write to pipe"); break; } fprintf(stderr, "Producer: Wrote %dbytes\n", io); } close(file); fprintf(stderr, "Producer: Closed input file.\n"); free(buf); fprintf(stderr, "Producer: Freed buffer.\n"); close(pipes[1]); fprintf(stderr, "Producer: Closed write-end of pipe.\n"); fprintf(stderr, "Producer: Waiting for child to exit.\n"); do { waitpid(pid, &status, 0); } while (!WIFEXITED(status)); fprintf(stderr, "Producer: consumer exited, and so do I.\n"); } return 0; }