1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) 2005, 2008, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" (A few fragments remain from an earlier (1992) version by
5 .\" Drew Eckhardt <drew@cs.colorado.edu>.)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified by Michael Haardt <michael@moria.de>
28 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" Modified 2005, mtk: added an example program
32 .\" Modified 2008-01-09, mtk: rewrote DESCRIPTION; minor additions
34 .\" 2008-10-10, mtk: add description of pipe2()
36 .TH PIPE 2 2010-09-10 "Linux" "Linux Programmer's Manual"
38 pipe, pipe2 \- create pipe
41 .B #include <unistd.h>
43 .BI "int pipe(int " pipefd "[2]);"
45 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
46 .B #include <unistd.h>
48 .BI "int pipe2(int " pipefd "[2], int " flags );
52 creates a pipe, a unidirectional data channel that
53 can be used for interprocess communication.
56 is used to return two file descriptors referring to the ends of the pipe.
58 refers to the read end of the pipe.
60 refers to the write end of the pipe.
61 Data written to the write end of the pipe is buffered by the kernel
62 until it is read from the read end of the pipe.
63 For further details, see
72 The following values can be bitwise ORed in
74 to obtain different behavior:
79 file status flag on the two new open file descriptions.
80 Using this flag saves extra calls to
82 to achieve the same result.
87 flag on the two new file descriptors.
88 See the description of the same flag in
90 for reasons why this may be useful.
92 On success, zero is returned.
93 On error, \-1 is returned, and
108 Too many file descriptors are in use by the process.
111 The system limit on the total number of open files has been reached.
114 was added to Linux in version 2.6.27;
115 glibc support is available starting with
124 .\" fork.2 refers to this example program.
125 The following program creates a pipe, and then
127 to create a child process;
128 the child inherits a duplicate set of file
129 descriptors that refer to the same pipe.
132 each process closes the descriptors that it doesn't need for the pipe
135 The parent then writes the string contained in the program's
136 command-line argument to the pipe,
137 and the child reads this string a byte at a time from the pipe
138 and echoes it on standard output.
141 #include <sys/wait.h>
148 main(int argc, char *argv[])
155 fprintf(stderr, "Usage: %s <string>\\n", argv[0]);
159 if (pipe(pipefd) == \-1) {
170 if (cpid == 0) { /* Child reads from pipe */
171 close(pipefd[1]); /* Close unused write end */
173 while (read(pipefd[0], &buf, 1) > 0)
174 write(STDOUT_FILENO, &buf, 1);
176 write(STDOUT_FILENO, "\\n", 1);
180 } else { /* Parent writes argv[1] to pipe */
181 close(pipefd[0]); /* Close unused read end */
182 write(pipefd[1], argv[1], strlen(argv[1]));
183 close(pipefd[1]); /* Reader will see EOF */
184 wait(NULL); /* Wait for child */