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