/* pipedemo.c -- Parent/child processes communication via a pipe * Using a pipe to send data from a parent to a child process * John Gray, Interprocess Communications in UNIX, P.125 */ #include #include #include #include #include int main(int argc, char *argv[]){ int f_des[2]; static char message[BUFSIZ]; if (argc != 2){ fprintf(stderr, "Usage: %s message\n", *argv); exit(1); } if (pipe(f_des) == -1){ /* generate the pipe */ perror("pipe"); exit(2); } switch (fork() ) { case -1: perror("Fork"); exit(3); case 0: /* In the child */ close(f_des[1]); sleep(1); printf("My Mommy's pid is %d\n",getppid()); if (read(f_des[0], message, BUFSIZ) != -1) { printf("Message received by child: [%s]\n", message); fflush(stdout); } else { perror("Read"); exit(4); } break; default: close(f_des[0]); if (write(f_des[1], argv[1], strlen(argv[1])) !=-1) { printf("Message sent by parent: [%s]\n", argv[1]); fflush(stdout); } else { perror("Write"); exit (5); } } exit(0); }