Start of man-pages-5.14: updating Changes and Changes.old
[man-pages.git] / man2 / pipe.2
blob41a482f3796eec1fcfa044d55b5f60922b16ca36
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>.)
4 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
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
33 .\"     to EXAMPLE text.
34 .\" 2008-10-10, mtk: add description of pipe2()
35 .\"
36 .TH PIPE 2 2021-03-22 "Linux" "Linux Programmer's Manual"
37 .SH NAME
38 pipe, pipe2 \- create pipe
39 .SH SYNOPSIS
40 .nf
41 .B #include <unistd.h>
42 .PP
43 .BI "int pipe(int " pipefd [2]);
44 .PP
45 .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
46 .BR "#include <fcntl.h>" "              /* Definition of " O_* " constants */"
47 .B #include <unistd.h>
48 .PP
49 .BI "int pipe2(int " pipefd "[2], int " flags );
50 .PP
51 /* On Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64, pipe() has the
52    following prototype; see NOTES */
53 .PP
54 .B #include <unistd.h>
55 .PP
56 .B struct fd_pair {
57 .B "    long fd[2];"
58 .B "};"
59 .B struct fd_pair pipe(void);
60 .fi
61 .SH DESCRIPTION
62 .BR pipe ()
63 creates a pipe, a unidirectional data channel that
64 can be used for interprocess communication.
65 The array
66 .IR pipefd
67 is used to return two file descriptors referring to the ends of the pipe.
68 .IR pipefd[0]
69 refers to the read end of the pipe.
70 .IR pipefd[1]
71 refers to the write end of the pipe.
72 Data written to the write end of the pipe is buffered by the kernel
73 until it is read from the read end of the pipe.
74 For further details, see
75 .BR pipe (7).
76 .PP
78 .IR flags
79 is 0, then
80 .BR pipe2 ()
81 is the same as
82 .BR pipe ().
83 The following values can be bitwise ORed in
84 .IR flags
85 to obtain different behavior:
86 .TP
87 .B O_CLOEXEC
88 Set the close-on-exec
89 .RB ( FD_CLOEXEC )
90 flag on the two new file descriptors.
91 See the description of the same flag in
92 .BR open (2)
93 for reasons why this may be useful.
94 .TP
95 .BR O_DIRECT " (since Linux 3.4)"
96 .\" commit 9883035ae7edef3ec62ad215611cb8e17d6a1a5d
97 Create a pipe that performs I/O in "packet" mode.
98 Each
99 .BR write (2)
100 to the pipe is dealt with as a separate packet, and
101 .BR read (2)s
102 from the pipe will read one packet at a time.
103 Note the following points:
105 .IP * 3
106 Writes of greater than
107 .BR PIPE_BUF
108 bytes (see
109 .BR pipe (7))
110 will be split into multiple packets.
111 The constant
112 .BR PIPE_BUF
113 is defined in
114 .IR <limits.h> .
115 .IP *
116 If a
117 .BR read (2)
118 specifies a buffer size that is smaller than the next packet,
119 then the requested number of bytes are read,
120 and the excess bytes in the packet are discarded.
121 Specifying a buffer size of
122 .BR PIPE_BUF
123 will be sufficient to read the largest possible packets
124 (see the previous point).
125 .IP *
126 Zero-length packets are not supported.
128 .BR read (2)
129 that specifies a buffer size of zero is a no-op, and returns 0.)
132 Older kernels that do not support this flag will indicate this via an
133 .B EINVAL
134 error.
136 Since Linux 4.5,
137 .\" commit 0dbf5f20652108106cb822ad7662c786baaa03ff
138 .\" FIXME . But, it is not possible to specify O_DIRECT when opening a FIFO
139 it is possible to change the
140 .B O_DIRECT
141 setting of a pipe file descriptor using
142 .BR fcntl (2).
144 .B O_NONBLOCK
145 Set the
146 .BR O_NONBLOCK
147 file status flag on the open file descriptions
148 referred to by the new file descriptors.
149 Using this flag saves extra calls to
150 .BR fcntl (2)
151 to achieve the same result.
152 .SH RETURN VALUE
153 On success, zero is returned.
154 On error, \-1 is returned,
155 .I errno
156 is set to indicate the error, and
157 .I pipefd
158 is left unchanged.
160 On Linux (and other systems),
161 .BR pipe ()
162 does not modify
163 .I pipefd
164 on failure.
165 A requirement standardizing this behavior was added in POSIX.1-2008 TC2.
166 .\" http://austingroupbugs.net/view.php?id=467
167 The Linux-specific
168 .BR pipe2 ()
169 system call
170 likewise does not modify
171 .I pipefd
172 on failure.
173 .SH ERRORS
175 .B EFAULT
176 .I pipefd
177 is not valid.
179 .B EINVAL
180 .RB ( pipe2 ())
181 Invalid value in
182 .IR flags .
184 .B EMFILE
185 The per-process limit on the number of open file descriptors has been reached.
187 .B ENFILE
188 The system-wide limit on the total number of open files has been reached.
190 .B ENFILE
191 The user hard limit on memory that can be allocated for pipes
192 has been reached and the caller is not privileged; see
193 .BR pipe (7).
194 .SH VERSIONS
195 .BR pipe2 ()
196 was added to Linux in version 2.6.27;
197 glibc support is available starting with
198 version 2.9.
199 .SH CONFORMING TO
200 .BR pipe ():
201 POSIX.1-2001, POSIX.1-2008.
203 .BR pipe2 ()
204 is Linux-specific.
205 .SH NOTES
206 .\" See http://math-atlas.sourceforge.net/devel/assembly/64.psabi.1.33.ps.Z
207 .\" for example, section 3.2.1 "Registers and the Stack Frame".
208 The System V ABI on some architectures allows the use of more than one register
209 for returning multiple values; several architectures
210 (namely, Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64)
211 (ab)use this feature in order to implement the
212 .BR pipe ()
213 system call in a functional manner:
214 the call doesn't take any arguments and returns
215 a pair of file descriptors as the return value on success.
216 The glibc
217 .BR pipe ()
218 wrapper function transparently deals with this.
220 .BR syscall (2)
221 for information regarding registers used for storing second file descriptor.
222 .SH EXAMPLES
223 .\" fork.2 refers to this example program.
224 The following program creates a pipe, and then
225 .BR fork (2)s
226 to create a child process;
227 the child inherits a duplicate set of file
228 descriptors that refer to the same pipe.
229 After the
230 .BR fork (2),
231 each process closes the file descriptors that it doesn't need for the pipe
232 (see
233 .BR pipe (7)).
234 The parent then writes the string contained in the program's
235 command-line argument to the pipe,
236 and the child reads this string a byte at a time from the pipe
237 and echoes it on standard output.
238 .SS Program source
240 #include <sys/types.h>
241 #include <sys/wait.h>
242 #include <stdio.h>
243 #include <stdlib.h>
244 #include <unistd.h>
245 #include <string.h>
248 main(int argc, char *argv[])
250     int pipefd[2];
251     pid_t cpid;
252     char buf;
254     if (argc != 2) {
255         fprintf(stderr, "Usage: %s <string>\en", argv[0]);
256         exit(EXIT_FAILURE);
257     }
259     if (pipe(pipefd) == \-1) {
260         perror("pipe");
261         exit(EXIT_FAILURE);
262     }
264     cpid = fork();
265     if (cpid == \-1) {
266         perror("fork");
267         exit(EXIT_FAILURE);
268     }
270     if (cpid == 0) {    /* Child reads from pipe */
271         close(pipefd[1]);          /* Close unused write end */
273         while (read(pipefd[0], &buf, 1) > 0)
274             write(STDOUT_FILENO, &buf, 1);
276         write(STDOUT_FILENO, "\en", 1);
277         close(pipefd[0]);
278         _exit(EXIT_SUCCESS);
280     } else {            /* Parent writes argv[1] to pipe */
281         close(pipefd[0]);          /* Close unused read end */
282         write(pipefd[1], argv[1], strlen(argv[1]));
283         close(pipefd[1]);          /* Reader will see EOF */
284         wait(NULL);                /* Wait for child */
285         exit(EXIT_SUCCESS);
286     }
289 .SH SEE ALSO
290 .BR fork (2),
291 .BR read (2),
292 .BR socketpair (2),
293 .BR splice (2),
294 .BR tee (2),
295 .BR vmsplice (2),
296 .BR write (2),
297 .BR popen (3),
298 .BR pipe (7)