2 Copyright (C) 1991-2013 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
18 #include <sys/types.h>
23 /* Read characters from the pipe and echo them to @code{stdout}. */
26 read_from_pipe (int file)
30 stream = fdopen (file, "r");
31 while ((c = fgetc (stream)) != EOF)
36 /* Write some random text to the pipe. */
39 write_to_pipe (int file)
42 stream = fdopen (file, "w");
43 fprintf (stream, "hello, world!\n");
44 fprintf (stream, "goodbye, world!\n");
55 /* Create the pipe. */
58 fprintf (stderr, "Pipe failed.\n");
63 /* Create the child process. */
67 /* This is the child process.
68 Close other end first. */
70 read_from_pipe (mypipe[0]);
73 else if (pid < (pid_t) 0)
75 /* The fork failed. */
76 fprintf (stderr, "Fork failed.\n");
81 /* This is the parent process.
82 Close other end first. */
84 write_to_pipe (mypipe[1]);