1 .\" Copyright (C) 2005 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH PIPE 7 2021-08-27 "Linux" "Linux Programmer's Manual"
27 pipe \- overview of pipes and FIFOs
29 Pipes and FIFOs (also known as named pipes)
30 provide a unidirectional interprocess communication channel.
35 Data written to the write end of a pipe can be read
36 from the read end of the pipe.
38 A pipe is created using
40 which creates a new pipe and returns two file descriptors,
41 one referring to the read end of the pipe,
42 the other referring to the write end.
43 Pipes can be used to create a communication channel between related
48 A FIFO (short for First In First Out) has a name within the filesystem
53 Any process may open a FIFO, assuming the file permissions allow it.
54 The read end is opened using the
56 flag; the write end is opened using the
63 although FIFOs have a pathname in the filesystem,
64 I/O on FIFOs does not involve operations on the underlying device
66 .SS I/O on pipes and FIFOs
67 The only difference between pipes and FIFOs is the manner in which
68 they are created and opened.
69 Once these tasks have been accomplished,
70 I/O on pipes and FIFOs has exactly the same semantics.
72 If a process attempts to read from an empty pipe, then
74 will block until data is available.
75 If a process attempts to write to a full pipe (see below), then
77 blocks until sufficient data has been read from the pipe
78 to allow the write to complete.
79 Nonblocking I/O is possible by using the
82 operation to enable the
84 open file status flag.
86 The communication channel provided by a pipe is a
88 there is no concept of message boundaries.
90 If all file descriptors referring to the write end of a pipe
91 have been closed, then an attempt to
93 from the pipe will see end-of-file
96 If all file descriptors referring to the read end of a pipe
97 have been closed, then a
101 signal to be generated for the calling process.
102 If the calling process is ignoring this signal, then
106 An application that uses
112 calls to close unnecessary duplicate file descriptors;
113 this ensures that end-of-file and
115 are delivered when appropriate.
117 It is not possible to apply
121 A pipe has a limited capacity.
122 If the pipe is full, then a
124 will block or fail, depending on whether the
126 flag is set (see below).
127 Different implementations have different limits for the pipe capacity.
128 Applications should not rely on a particular capacity:
129 an application should be designed so that a reading process consumes data
130 as soon as it is available,
131 so that a writing process does not remain blocked.
133 In Linux versions before 2.6.11, the capacity of a pipe was the same as
134 the system page size (e.g., 4096 bytes on i386).
135 Since 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).
137 Since Linux 2.6.35, the default pipe capacity is 16 pages,
138 but the capacity can be queried and set using the
146 for more information.
150 operation, which can be applied to a file descriptor
151 that refers to either end of a pipe,
152 places a count of the number of unread bytes in the pipe in the
154 buffer pointed to by the final argument of the call:
156 ioctl(fd, FIONREAD, &nbytes);
160 operation is not specified in any standard,
161 but is provided on many implementations.
164 On Linux, the following files control how much memory can be used for pipes:
166 .IR /proc/sys/fs/pipe\-max\-pages " (only in Linux 2.6.34)"
167 .\" commit b492e95be0ae672922f4734acf3f5d35c30be948
168 An upper limit, in pages, on the capacity that an unprivileged user
174 The default value for this limit is 16 times the default pipe capacity
175 (see above); the lower limit is two pages.
177 This interface was removed in Linux 2.6.35, in favor of
178 .IR /proc/sys/fs/pipe\-max\-size .
180 .IR /proc/sys/fs/pipe\-max\-size " (since Linux 2.6.35)"
181 .\" commit ff9da691c0498ff81fdd014e7a0731dab2337dac
182 The 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
188 The value assigned to this file may be rounded upward,
189 to reflect the value actually employed for a convenient implementation.
190 To determine the rounded-up value,
191 display the contents of this file after assigning a value to it.
193 The default value for this file is 1048576 (1\ MiB).
194 The minimum value that can be assigned to this file is the system page size.
195 Attempts to set a limit less than the page size cause
197 to fail with the error
201 .\" commit 086e774a57fba4695f14383c0818994c0b31da7c
202 the value on this file also acts as a ceiling on the default capacity
203 of a new pipe or newly opened FIFO.
205 .IR /proc/sys/fs/pipe\-user\-pages\-hard " (since Linux 4.5)"
206 .\" commit 759c01142a5d0f364a462346168a56de28a80f52
207 The hard limit on the total size (in pages) of all pipes created or set by
208 a single unprivileged user (i.e., one with neither the
213 So long as the total number of pages allocated to pipe buffers
214 for this user is at this limit,
215 attempts to create new pipes will be denied,
216 and attempts to increase a pipe's capacity will be denied.
218 When the value of this limit is zero (which is the default),
219 no 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).
223 .IR /proc/sys/fs/pipe\-user\-pages\-soft " (since Linux 4.5)"
224 .\" commit 759c01142a5d0f364a462346168a56de28a80f52
225 The soft limit on the total size (in pages) of all pipes created or set by
226 a single unprivileged user (i.e., one with neither the
231 So long as the total number of pages allocated to pipe buffers
232 for this user is at this limit,
233 individual pipes created by a user will be limited to one page,
234 and attempts to increase a pipe's capacity will be denied.
236 When the value of this limit is zero, no soft limit is applied.
237 The default value for this file is 16384,
238 which permits creating up to 1024 pipes with the default capacity.
240 Before Linux 4.9, some bugs affected the handling of the
241 .IR pipe\-user\-pages\-soft
243 .IR pipe\-user\-pages\-hard
247 POSIX.1 says that writes of less than
249 bytes must be atomic: the output data is written to the pipe as a
253 bytes may be nonatomic: the kernel may interleave the data
254 with data written by other processes.
257 to be at least 512 bytes.
261 The precise semantics depend on whether the file descriptor is nonblocking
263 whether there are multiple writers to the pipe, and on
265 the number of bytes to be written:
267 \fBO_NONBLOCK\fP disabled, \fIn\fP <= \fBPIPE_BUF\fP
270 bytes are written atomically;
272 may block if there is not room for
274 bytes to be written immediately
276 \fBO_NONBLOCK\fP enabled, \fIn\fP <= \fBPIPE_BUF\fP
277 If there is room to write
279 bytes to the pipe, then
281 succeeds immediately, writing all
290 \fBO_NONBLOCK\fP disabled, \fIn\fP > \fBPIPE_BUF\fP
291 The write is nonatomic: the data given to
293 may be interleaved with
300 bytes have been written.
302 \fBO_NONBLOCK\fP enabled, \fIn\fP > \fBPIPE_BUF\fP
303 If the pipe is full, then
311 bytes may be written (i.e., a "partial write" may occur;
312 the caller should check the return value from
314 to see how many bytes were actually written),
315 and these bytes may be interleaved with writes by other processes.
316 .SS Open file status flags
317 The only open file status flags that can be meaningfully applied to
325 flag for the read end of a pipe causes a signal
327 by default) to be generated when new input becomes available on the pipe.
328 The target for delivery of signals must be set using the
334 is supported for pipes and FIFOs only since kernel 2.6.
335 .SS Portability notes
336 On some systems (but not Linux), pipes are bidirectional:
337 data can be transmitted in both directions between the pipe ends.
338 POSIX.1 requires only unidirectional pipes.
339 Portable applications should avoid reliance on
340 bidirectional pipe semantics.
342 Before Linux 4.9, some bugs affected the handling of the
343 .IR pipe\-user\-pages\-soft
345 .IR pipe\-user\-pages\-hard
346 limits when using the
349 operation to change a pipe's capacity:
350 .\" These bugs where remedied by a series of patches, in particular,
351 .\" commit b0b91d18e2e97b741b294af9333824ecc3fadfd8 and
352 .\" commit a005ca0e6813e1d796a7422a7e31d8b8d6555df1
354 When increasing the pipe capacity, the checks against the soft and
355 hard limits were made against existing consumption,
356 and excluded the memory required for the increased pipe capacity.
357 The new increase in pipe capacity could then push the total
358 memory used by the user for pipes (possibly far) over a limit.
359 (This could also trigger the problem described next.)
361 Starting with Linux 4.9,
362 the limit checking includes the memory required for the new pipe capacity.
364 The limit checks were performed even when the new pipe capacity was
365 less than the existing pipe capacity.
366 This could lead to problems if a user set a large pipe capacity,
367 and then the limits were lowered, with the result that the user could
368 no longer decrease the pipe capacity.
370 Starting with Linux 4.9, checks against the limits
371 are performed only when increasing a pipe's capacity;
372 an unprivileged user can always decrease a pipe's capacity.
374 The accounting and checking against the limits were done as follows:
379 Test whether the user has exceeded the limit.
381 Make the new pipe buffer allocation.
383 Account new allocation against the limits.
388 Multiple processes could pass point (a) simultaneously,
389 and then allocate pipe buffers that were accounted for only in step (c),
390 with the result that the user's pipe buffer
391 allocation could be pushed over the limit.
393 Starting with Linux 4.9,
394 the accounting step is performed before doing the allocation,
395 and the operation fails if the limit would be exceeded.
397 Before Linux 4.9, bugs similar to points (1) and (3) could also occur
398 when the kernel allocated memory for a new pipe buffer;
399 that is, when calling
401 and when opening a previously unopened FIFO.