diff options
Diffstat (limited to 'manual/examples')
-rw-r--r-- | manual/examples/pipe.c | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/manual/examples/pipe.c b/manual/examples/pipe.c index 054550fec6..92d339a7b9 100644 --- a/manual/examples/pipe.c +++ b/manual/examples/pipe.c @@ -5,7 +5,7 @@ /* Read characters from the pipe and echo them to @code{stdout}. */ -void +void read_from_pipe (int file) { FILE *stream; @@ -18,7 +18,7 @@ read_from_pipe (int file) /* Write some random text to the pipe. */ -void +void write_to_pipe (int file) { FILE *stream; @@ -35,7 +35,7 @@ main (void) int mypipe[2]; /*@group*/ - /* Create the pipe. */ + /* Create the pipe. */ if (pipe (mypipe)) { fprintf (stderr, "Pipe failed.\n"); @@ -43,23 +43,27 @@ main (void) } /*@end group*/ - /* Create the child process. */ + /* Create the child process. */ pid = fork (); if (pid == (pid_t) 0) { - /* This is the child process. */ + /* This is the child process. + Close other end first. */ + close (mypipe[1]); read_from_pipe (mypipe[0]); return EXIT_SUCCESS; } else if (pid < (pid_t) 0) { - /* The fork failed. */ + /* The fork failed. */ fprintf (stderr, "Fork failed.\n"); return EXIT_FAILURE; } else { - /* This is the parent process. */ + /* This is the parent process. + Close other end first. */ + close (mypipe[0]); write_to_pipe (mypipe[1]); return EXIT_SUCCESS; } |