1 .\" Copyright (C) 2005, 2008, Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" (A few fragments remain from an earlier (1992) version by
3 .\" Drew Eckhardt <drew@cs.colorado.edu>.)
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" 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 2020-06-09 "Linux" "Linux Programmer's Manual"
38 pipe, pipe2 \- create pipe
41 .B #include <unistd.h>
43 /* On Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64; see NOTES */
47 .B struct fd_pair pipe();
49 /* On all other architectures */
50 .BI "int pipe(int " pipefd "[2]);"
52 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
53 .BR "#include <fcntl.h>" " /* Obtain O_* constant definitions */"
54 .B #include <unistd.h>
56 .BI "int pipe2(int " pipefd "[2], int " flags );
60 creates a pipe, a unidirectional data channel that
61 can be used for interprocess communication.
64 is used to return two file descriptors referring to the ends of the pipe.
66 refers to the read end of the pipe.
68 refers to the write end of the pipe.
69 Data written to the write end of the pipe is buffered by the kernel
70 until it is read from the read end of the pipe.
71 For further details, see
80 The following values can be bitwise ORed in
82 to obtain different behavior:
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 .BR O_DIRECT " (since Linux 3.4)"
93 .\" commit 9883035ae7edef3ec62ad215611cb8e17d6a1a5d
94 Create a pipe that performs I/O in "packet" mode.
97 to the pipe is dealt with as a separate packet, and
99 from the pipe will read one packet at a time.
100 Note the following points:
103 Writes of greater than
107 will be split into multiple packets.
115 specifies a buffer size that is smaller than the next packet,
116 then the requested number of bytes are read,
117 and the excess bytes in the packet are discarded.
118 Specifying a buffer size of
120 will be sufficient to read the largest possible packets
121 (see the previous point).
123 Zero-length packets are not supported.
126 that specifies a buffer size of zero is a no-op, and returns 0.)
129 Older kernels that do not support this flag will indicate this via an
134 .\" commit 0dbf5f20652108106cb822ad7662c786baaa03ff
135 .\" FIXME . But, it is not possible to specify O_DIRECT when opening a FIFO
136 it is possible to change the
138 setting of a pipe file descriptor using
144 file status flag on the open file descriptions
145 referred to by the new file descriptors.
146 Using this flag saves extra calls to
148 to achieve the same result.
150 On success, zero is returned.
151 On error, \-1 is returned,
153 is set to indicate the error, and
157 On Linux (and other systems),
162 A requirement standardizing this behavior was added in POSIX.1-2008 TC2.
163 .\" http://austingroupbugs.net/view.php?id=467
167 likewise does not modify
182 The per-process limit on the number of open file descriptors has been reached.
185 The system-wide limit on the total number of open files has been reached.
188 The user hard limit on memory that can be allocated for pipes
189 has been reached and the caller is not privileged; see
193 was added to Linux in version 2.6.27;
194 glibc support is available starting with
198 POSIX.1-2001, POSIX.1-2008.
203 .\" See http://math-atlas.sourceforge.net/devel/assembly/64.psabi.1.33.ps.Z
204 .\" for example, section 3.2.1 "Registers and the Stack Frame".
205 The System V ABI on some architectures allows the use of more than one register
206 for returning multiple values; several architectures
207 (namely, Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64)
208 (ab)use this feature in order to implement the
210 system call in a functional manner:
211 the call doesn't take any arguments and returns
212 a pair of file descriptors as the return value on success.
215 wrapper function transparently deals with this.
218 for information regarding registers used for storing second file descriptor.
220 .\" fork.2 refers to this example program.
221 The following program creates a pipe, and then
223 to create a child process;
224 the child inherits a duplicate set of file
225 descriptors that refer to the same pipe.
228 each process closes the file descriptors that it doesn't need for the pipe
231 The parent then writes the string contained in the program's
232 command-line argument to the pipe,
233 and the child reads this string a byte at a time from the pipe
234 and echoes it on standard output.
237 #include <sys/types.h>
238 #include <sys/wait.h>
245 main(int argc, char *argv[])
252 fprintf(stderr, "Usage: %s <string>\en", argv[0]);
256 if (pipe(pipefd) == \-1) {
267 if (cpid == 0) { /* Child reads from pipe */
268 close(pipefd[1]); /* Close unused write end */
270 while (read(pipefd[0], &buf, 1) > 0)
271 write(STDOUT_FILENO, &buf, 1);
273 write(STDOUT_FILENO, "\en", 1);
277 } else { /* Parent writes argv[1] to pipe */
278 close(pipefd[0]); /* Close unused read end */
279 write(pipefd[1], argv[1], strlen(argv[1]));
280 close(pipefd[1]); /* Reader will see EOF */
281 wait(NULL); /* Wait for child */