// file: hello_pipe.c // // Synopsis: Simple illustration of pipe // from [Jumpstart]. // // (1) Creates a pipe // (2) Writes "Hello World" into one end // (3) Reads from the other end // (4) Write the result of reading to std output. // // THIS IS A TOY PROBLEM SINCE USUALLY PIPES // GOES BETWEEN TWO PROCESSES. // // REMARK: THIS PROGRAM WORKS ON BOTH CYGWIN and SOLARIS. // Chee Yap (Feb 10, 2005) #include #include #include #include #include main() { char *message = "Hello World!\n"; int length = strlen(message); char buf[1024]; int fdv[2]; int n; if (pipe(fdv) == -1) perror("pipe"); if (write(fdv[1], message, length) != length) perror("write to pipe"); n = read(fdv[0], buf, length); if (n != length) perror("read from pipe"); write(STDOUT_FILENO, buf, n); exit(0); }