]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/pipe.7
execve.2: Some tweaks to Shawn Landden's patch
[thirdparty/man-pages.git] / man7 / pipe.7
CommitLineData
c11b1abf 1.\" Copyright (C) 2005 Michael Kerrisk <mtk.manpages@gmail.com>
2adb3bd6 2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
2adb3bd6
MK
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
2adb3bd6
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
10d76543
MK
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
2adb3bd6
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
2adb3bd6 24.\"
4b8c67d9 25.TH PIPE 7 2017-09-15 "Linux" "Linux Programmer's Manual"
2adb3bd6 26.SH NAME
3a20b4ca 27pipe \- overview of pipes and FIFOs
2adb3bd6 28.SH DESCRIPTION
c13182ef 29Pipes and FIFOs (also known as named pipes)
2adb3bd6 30provide a unidirectional interprocess communication channel.
c13182ef
MK
31A pipe has a
32.I read end
33and a
2adb3bd6
MK
34.IR "write end" .
35Data written to the write end of a pipe can be read
48afe71d 36from the read end of the pipe.
a721e8b2 37.PP
c13182ef 38A pipe is created using
2adb3bd6
MK
39.BR pipe (2),
40which creates a new pipe and returns two file descriptors,
c13182ef 41one referring to the read end of the pipe,
2adb3bd6 42the other referring to the write end.
c13182ef
MK
43Pipes can be used to create a communication channel between related
44processes; see
6d563954
MK
45.BR pipe (2)
46for an example.
a721e8b2 47.PP
28955f15
SP
48A FIFO (short for First In First Out) has a name within the filesystem
49(created using
2adb3bd6
MK
50.BR mkfifo (3)),
51and is opened using
c13182ef 52.BR open (2).
2adb3bd6 53Any process may open a FIFO, assuming the file permissions allow it.
c13182ef 54The read end is opened using the
2adb3bd6
MK
55.B O_RDONLY
56flag; the write end is opened using the
57.B O_WRONLY
58flag.
59See
af5b2ef2 60.BR fifo (7)
2adb3bd6 61for further details.
c13182ef 62.IR Note :
9ee4a2b6 63although FIFOs have a pathname in the filesystem,
c13182ef 64I/O on FIFOs does not involve operations on the underlying device
48afe71d 65(if there is one).
73d8cece 66.SS I/O on pipes and FIFOs
2adb3bd6 67The only difference between pipes and FIFOs is the manner in which
c13182ef
MK
68they are created and opened.
69Once these tasks have been accomplished,
2adb3bd6 70I/O on pipes and FIFOs has exactly the same semantics.
a721e8b2 71.PP
2adb3bd6 72If a process attempts to read from an empty pipe, then
c13182ef 73.BR read (2)
2adb3bd6
MK
74will block until data is available.
75If a process attempts to write to a full pipe (see below), then
76.BR write (2)
77blocks until sufficient data has been read from the pipe
78to allow the write to complete.
44732c9c 79Nonblocking I/O is possible by using the
2adb3bd6 80.BR fcntl (2)
c13182ef 81.B F_SETFL
2adb3bd6
MK
82operation to enable the
83.B O_NONBLOCK
84open file status flag.
a721e8b2 85.PP
48afe71d 86The communication channel provided by a pipe is a
c13182ef 87.IR "byte stream" :
48afe71d 88there is no concept of message boundaries.
a721e8b2 89.PP
c13182ef
MK
90If all file descriptors referring to the write end of a pipe
91have been closed, then an attempt to
2adb3bd6
MK
92.BR read (2)
93from the pipe will see end-of-file
94.RB ( read (2)
95will return 0).
c13182ef 96If all file descriptors referring to the read end of a pipe
2adb3bd6
MK
97have been closed, then a
98.BR write (2)
99will cause a
100.B SIGPIPE
101signal to be generated for the calling process.
102If the calling process is ignoring this signal, then
103.BR write (2)
104fails with the error
105.BR EPIPE .
c13182ef 106An application that uses
2adb3bd6
MK
107.BR pipe (2)
108and
109.BR fork (2)
c13182ef 110should use suitable
2adb3bd6
MK
111.BR close (2)
112calls to close unnecessary duplicate file descriptors;
c13182ef
MK
113this ensures that end-of-file and
114.BR SIGPIPE / EPIPE
2adb3bd6 115are delivered when appropriate.
a721e8b2 116.PP
48afe71d 117It is not possible to apply
2adb3bd6 118.BR lseek (2)
48afe71d 119to a pipe.
73d8cece 120.SS Pipe capacity
2adb3bd6
MK
121A pipe has a limited capacity.
122If the pipe is full, then a
123.BR write (2)
124will block or fail, depending on whether the
c13182ef 125.B O_NONBLOCK
2adb3bd6
MK
126flag is set (see below).
127Different implementations have different limits for the pipe capacity.
c13182ef
MK
128Applications should not rely on a particular capacity:
129an application should be designed so that a reading process consumes data
130as soon as it is available,
2adb3bd6 131so that a writing process does not remain blocked.
a721e8b2 132.PP
c13182ef 133In Linux versions before 2.6.11, the capacity of a pipe was the same as
34ccb744 134the system page size (e.g., 4096 bytes on i386).
de694c98
MK
135Since Linux 2.6.11, the pipe capacity is 16 pages
136(i.e., 65,536 bytes in a system with a page size of 4096 bytes).
137Since Linux 2.6.35, the default pipe capacity is 16 pages,
640a438d 138but the capacity can be queried and set using the
91d4de66
EDB
139.BR fcntl (2)
140.BR F_GETPIPE_SZ
141and
142.BR F_SETPIPE_SZ
143operations.
144See
145.BR fcntl (2)
146for more information.
a721e8b2 147.PP
2293a55f
MK
148The following
149.BR ioctl (2)
150operation, which can be applied to a file descriptor
151that refers to either end of a pipe,
152places a count of the number of unread bytes in the pipe in the
153.I int
154buffer pointed to by the final argument of the call:
a721e8b2 155.PP
2293a55f 156 ioctl(fd, FIONREAD, &nbytes);
a721e8b2 157.PP
2293a55f
MK
158The
159.B FIONREAD
160operation is not specified in any standard,
161but is provided on many implementations.
787dd4ad 162.\"
81c4577e
VN
163.SS /proc files
164On Linux, the following files control how much memory can be used for pipes:
165.TP
12033ac4 166.IR /proc/sys/fs/pipe-max-pages " (only in Linux 2.6.34)"
81c4577e 167.\" commit b492e95be0ae672922f4734acf3f5d35c30be948
1b9d5819 168An upper limit, in pages, on the capacity that an unprivileged user
12033ac4
MK
169(one without the
170.BR CAP_SYS_RESOURCE
171capability)
172can set for a pipe.
a721e8b2 173.IP
12033ac4
MK
174The default value for this limit is 16 times the default pipe capacity
175(see above); the lower limit is two pages.
a721e8b2 176.IP
12033ac4
MK
177This interface was removed in Linux 2.6.35, in favor of
178.IR /proc/sys/fs/pipe-max-size .
81c4577e
VN
179.TP
180.IR /proc/sys/fs/pipe-max-size " (since Linux 2.6.35)"
181.\" commit ff9da691c0498ff81fdd014e7a0731dab2337dac
cc6b4da2
MK
182The maximum size (in bytes) of individual pipes that can be set
183.\" This limit is not checked on pipe creation, where the capacity is
184.\" always PIPE_DEF_BUFS, regardless of pipe-max-size
185by users without the
81c4577e
VN
186.B CAP_SYS_RESOURCE
187capability.
33dc4b59
MK
188The value assigned to this file may be rounded upward,
189to reflect the value actually employed for a convenient implementation.
190To determine the rounded-up value,
191display the contents of this file after assigning a value to it.
a721e8b2 192.IP
c4b7e5ac 193The default value for this file is 1048576 (1\ MiB).
33dc4b59 194The minimum value that can be assigned to this file is the system page size.
12033ac4
MK
195Attempts to set a limit less than the page size cause
196.BR write (2)
197to fail with the error
198.BR EINVAL .
a721e8b2 199.IP
3d898875
MK
200Since Linux 4.9,
201.\" commit 086e774a57fba4695f14383c0818994c0b31da7c
202the value on this file also acts as a ceiling on the default capacity
203of a new pipe or newly opened FIFO.
81c4577e 204.TP
12033ac4 205.IR /proc/sys/fs/pipe-user-pages-hard " (since Linux 4.5)"
81c4577e 206.\" commit 759c01142a5d0f364a462346168a56de28a80f52
12033ac4
MK
207The hard limit on the total size (in pages) of all pipes created or set by
208a single unprivileged user (i.e., one with neither the
81c4577e 209.B CAP_SYS_RESOURCE
12033ac4 210nor the
81c4577e 211.B CAP_SYS_ADMIN
12033ac4
MK
212capability).
213So long as the total number of pages allocated to pipe buffers
214for this user is at this limit,
215attempts to create new pipes will be denied,
216and attempts to increase a pipe's capacity will be denied.
a721e8b2 217.IP
12033ac4
MK
218When the value of this limit is zero (which is the default),
219no hard limit is applied.
220.\" The default was chosen to avoid breaking existing applications that
221.\" make intensive use of pipes (e.g., for splicing).
81c4577e 222.TP
12033ac4 223.IR /proc/sys/fs/pipe-user-pages-soft " (since Linux 4.5)"
81c4577e 224.\" commit 759c01142a5d0f364a462346168a56de28a80f52
12033ac4
MK
225The soft limit on the total size (in pages) of all pipes created or set by
226a single unprivileged user (i.e., one with neither the
81c4577e 227.B CAP_SYS_RESOURCE
12033ac4 228nor the
81c4577e 229.B CAP_SYS_ADMIN
12033ac4
MK
230capability).
231So long as the total number of pages allocated to pipe buffers
232for this user is at this limit,
233individual pipes created by a user will be limited to one page,
234and attempts to increase a pipe's capacity will be denied.
a721e8b2 235.IP
12033ac4
MK
236When the value of this limit is zero, no soft limit is applied.
237The default value for this file is 16384,
238which permits creating up to 1024 pipes with the default capacity.
1cb4e899
MK
239.PP
240Before Linux 4.9, some bugs affected the handling of the
241.IR pipe-user-pages-soft
242and
243.IR pipe-user-pages-hard
244limits; see BUGS.
81c4577e 245.\"
2adb3bd6 246.SS PIPE_BUF
3330e739 247POSIX.1 says that
2adb3bd6 248.BR write (2)s
c13182ef
MK
249of less than
250.B PIPE_BUF
251bytes must be atomic: the output data is written to the pipe as a
2adb3bd6
MK
252contiguous sequence.
253Writes of more than
254.B PIPE_BUF
24b74457 255bytes may be nonatomic: the kernel may interleave the data
2adb3bd6 256with data written by other processes.
3330e739 257POSIX.1 requires
c13182ef
MK
258.B PIPE_BUF
259to be at least 512 bytes.
260(On Linux,
261.B PIPE_BUF
2adb3bd6 262is 4096 bytes.)
ff40dbb3 263The precise semantics depend on whether the file descriptor is nonblocking
2adb3bd6 264.RB ( O_NONBLOCK ),
c13182ef 265whether there are multiple writers to the pipe, and on
2adb3bd6
MK
266.IR n ,
267the number of bytes to be written:
268.TP
269\fBO_NONBLOCK\fP disabled, \fIn\fP <= \fBPIPE_BUF\fP
270All
271.I n
272bytes are written atomically;
273.BR write (2)
274may block if there is not room for
275.I n
276bytes to be written immediately
277.TP
278\fBO_NONBLOCK\fP enabled, \fIn\fP <= \fBPIPE_BUF\fP
279If there is room to write
280.I n
281bytes to the pipe, then
282.BR write (2)
283succeeds immediately, writing all
284.I n
c13182ef 285bytes; otherwise
2adb3bd6
MK
286.BR write (2)
287fails, with
288.I errno
289set to
290.BR EAGAIN .
291.TP
292\fBO_NONBLOCK\fP disabled, \fIn\fP > \fBPIPE_BUF\fP
24b74457 293The write is nonatomic: the data given to
c13182ef
MK
294.BR write (2)
295may be interleaved with
296.BR write (2)s
297by other process;
2adb3bd6
MK
298the
299.BR write (2)
c13182ef 300blocks until
2adb3bd6
MK
301.I n
302bytes have been written.
303.TP
304\fBO_NONBLOCK\fP enabled, \fIn\fP > \fBPIPE_BUF\fP
c13182ef 305If the pipe is full, then
2adb3bd6
MK
306.BR write (2)
307fails, with
308.I errno
309set to
310.BR EAGAIN .
311Otherwise, from 1 to
c13182ef 312.I n
2adb3bd6 313bytes may be written (i.e., a "partial write" may occur;
c13182ef 314the caller should check the return value from
2adb3bd6
MK
315.BR write (2)
316to see how many bytes were actually written),
317and these bytes may be interleaved with writes by other processes.
73d8cece 318.SS Open file status flags
c13182ef 319The only open file status flags that can be meaningfully applied to
48afe71d
MK
320a pipe or FIFO are
321.B O_NONBLOCK
c13182ef 322and
48afe71d 323.BR O_ASYNC .
a721e8b2 324.PP
48afe71d
MK
325Setting the
326.B O_ASYNC
c13182ef 327flag for the read end of a pipe causes a signal
48afe71d 328.RB ( SIGIO
98faa645
MK
329by default) to be generated when new input becomes available on the pipe.
330The target for delivery of signals must be set using the
48afe71d 331.BR fcntl (2)
98faa645
MK
332.B F_SETOWN
333command.
48afe71d 334On Linux,
c13182ef 335.B O_ASYNC
48afe71d 336is supported for pipes and FIFOs only since kernel 2.6.
73d8cece 337.SS Portability notes
c13182ef 338On some systems (but not Linux), pipes are bidirectional:
2adb3bd6 339data can be transmitted in both directions between the pipe ends.
a448fdd6 340POSIX.1 requires only unidirectional pipes.
c13182ef 341Portable applications should avoid reliance on
2adb3bd6 342bidirectional pipe semantics.
1cb4e899
MK
343.SS BUGS
344Before Linux 4.9, some bugs affected the handling of the
345.IR pipe-user-pages-soft
346and
347.IR pipe-user-pages-hard
348limits when using the
349.BR fcntl (2)
350.BR F_SETPIPE_SZ
351operation to change a pipe's capacity:
352.\" These bugs where remedied by a series of patches, in particular,
353.\" commit b0b91d18e2e97b741b294af9333824ecc3fadfd8 and
354.\" commit a005ca0e6813e1d796a7422a7e31d8b8d6555df1
355.IP (1) 5
1b9d5819 356When increasing the pipe capacity, the checks against the soft and
1cb4e899
MK
357hard limits were made against existing consumption,
358and excluded the memory required for the increased pipe capacity.
359The new increase in pipe capacity could then push the total
360memory used by the user for pipes (possibly far) over a limit.
361(This could also trigger the problem described next.)
a721e8b2 362.IP
1cb4e899
MK
363Starting with Linux 4.9,
364the limit checking includes the memory required for the new pipe capacity.
365.IP (2)
366The limit checks were performed even when the new pipe capacity was
367less than the existing pipe capacity.
368This could lead to problems if a user set a large pipe capacity,
369and then the limits were lowered, with the result that the user could
370no longer decrease the pipe capacity.
a721e8b2 371.IP
1cb4e899
MK
372Starting with Linux 4.9, checks against the limits
373are performed only when increasing a pipe's capacity;
374an unprivileged user can always decrease a pipe's capacity.
375.IP (3)
376The accounting and checking against the limits were done as follows:
a721e8b2 377.IP
1cb4e899
MK
378.RS
379.PD 0
380.IP (a) 4
381Test whether the user has exceeded the limit.
382.IP (b)
383Make the new pipe buffer allocation.
384.IP (c)
385Account new allocation against the limits.
386.PD
387.RE
388.IP
389This was racey.
390Multiple processes could pass point (a) simultaneously,
391and then allocate pipe buffers that were accounted for only in step (c),
392with the result that the user's pipe buffer
393allocation could be pushed over the limit.
a721e8b2 394.IP
1cb4e899
MK
395Starting with Linux 4.9,
396the accounting step is performed before doing the allocation,
397and the operation fails if the limit would be exceeded.
398.PP
399Before Linux 4.9, bugs similar to points (1) and (3) could also occur
400when the kernel allocated memory for a new pipe buffer;
401that is, when calling
402.BR pipe (2)
403and when opening a previously unopened FIFO.
47297adb 404.SH SEE ALSO
8a33c6e0 405.BR mkfifo (1),
2adb3bd6
MK
406.BR dup (2),
407.BR fcntl (2),
408.BR open (2),
409.BR pipe (2),
410.BR poll (2),
411.BR select (2),
412.BR socketpair (2),
6b1b0c98 413.BR splice (2),
2adb3bd6 414.BR stat (2),
7af84d64
MK
415.BR tee (2),
416.BR vmsplice (2),
2adb3bd6 417.BR mkfifo (3),
af5b2ef2
MK
418.BR epoll (7),
419.BR fifo (7)