1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Modified by Michael Haardt <michael@moria.de>
26 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
28 .\" Modified 2004-06-17 by Michael Kerrisk <mtk-manpages@gmx.net>
30 .TH PIPE 2 2004-06-17 "Linux 2.6.7" "Linux Programmer's Manual"
34 .B #include <unistd.h>
36 .BI "int pipe(int " filedes "[2]);"
39 creates a pair of file descriptors, pointing to a pipe inode, and places
40 them in the array pointed to by
47 On success, zero is returned. On error, \-1 is returned, and
57 Too many file descriptors are in use by the process.
60 The system limit on the total number of open files has been reached.
64 .\" fork.2 refers to this example program.
65 The following program creates a pipe, and then
67 to create a child process.
70 each process closes the descriptors that it doesn't need for the pipe
73 The parent then writes the string contained in the program's
74 command-line argument to the pipe,
75 and the child reads this string a byte at a time from the pipe
76 and echoes it on standard output.
87 main(int argc, char *argv[])
95 if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
98 if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
100 if (cpid == 0) { /* Child reads from pipe */
101 close(pfd[1]); /* Close unused write end */
103 while (read(pfd[0], &buf, 1) > 0)
104 write(STDOUT_FILENO, &buf, 1);
106 write(STDOUT_FILENO, "\\n", 1);
110 } else { /* Parent writes argv[1] to pipe */
111 close(pfd[0]); /* Close unused read end */
112 write(pfd[1], argv[1], strlen(argv[1]));
113 close(pfd[1]); /* Reader will see EOF */
114 wait(NULL); /* Wait for child */