diff options
Diffstat (limited to 'pipe.c')
-rw-r--r-- | pipe.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define MAX_LINE 80 +#define PIPE_STDIN 0 +#define PIPE_STDOUT 1 + +int main(int argc, char **argv) +{ + const char *string = {"A sample message."}; + int ret, myPipe[2]; + char buffer[MAX_LINE+1]; + + /* create the pipe */ + ret = pipe(myPipe); + + if (ret == 0) + { + /* write message into the pipe */ + write(myPipe[PIPE_STDOUT], string, strlen(string)); + + /* read the message from the pipe */ + ret = read(myPipe[PIPE_STDIN], buffer, MAX_LINE); + + /* null terminate the string */ + buffer[ret] = 0; + + printf("%s\n", buffer); + } + else + { + perror("pipe"); + exit(EXIT_FAILURE); + } + + return 0; +} + |