]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/pipe.7
pipe.7: Document /proc files controlling memory usage by pipes
[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.\"
b8efb414 25.TH PIPE 7 2016-10-08 "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.
2adb3bd6 37
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.
2adb3bd6 47
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.
2adb3bd6 71
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.
85
48afe71d 86The communication channel provided by a pipe is a
c13182ef 87.IR "byte stream" :
48afe71d
MK
88there is no concept of message boundaries.
89
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
MK
115are delivered when appropriate.
116
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
MK
131so that a writing process does not remain blocked.
132
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).
2adb3bd6 135Since Linux 2.6.11, the pipe capacity is 65536 bytes.
640a438d
MK
136Since Linux 2.6.35, the default pipe capacity is 65536 bytes,
137but the capacity can be queried and set using the
91d4de66
EDB
138.BR fcntl (2)
139.BR F_GETPIPE_SZ
140and
141.BR F_SETPIPE_SZ
142operations.
143See
144.BR fcntl (2)
145for more information.
2293a55f
MK
146
147The following
148.BR ioctl (2)
149operation, which can be applied to a file descriptor
150that refers to either end of a pipe,
151places a count of the number of unread bytes in the pipe in the
152.I int
153buffer pointed to by the final argument of the call:
154
155 ioctl(fd, FIONREAD, &nbytes);
156
157The
158.B FIONREAD
159operation is not specified in any standard,
160but is provided on many implementations.
787dd4ad 161.\"
81c4577e
VN
162.SS /proc files
163On Linux, the following files control how much memory can be used for pipes:
164.TP
165.IR /proc/sys/fs/pipe-max-pages " (since Linux 2.6.34)"
166.\" commit b492e95be0ae672922f4734acf3f5d35c30be948
167Deprecated since Linux 2.6.35.
168.TP
169.IR /proc/sys/fs/pipe-max-size " (since Linux 2.6.35)"
170.\" commit ff9da691c0498ff81fdd014e7a0731dab2337dac
171The maximum size (in bytes) of individual pipes created or set by users
172without the
173.B CAP_SYS_RESOURCE
174capability.
175.TP
176.IR /proc/sys/fs/pipe-user-pages-hard " (since Linux 4.4)"
177.\" commit 759c01142a5d0f364a462346168a56de28a80f52
178The combined maximum size (in pages) of all pipes created or set by a
179single user without both the
180.B CAP_SYS_RESOURCE
181and
182.B CAP_SYS_ADMIN
183capabilities.
184.TP
185.IR /proc/sys/fs/pipe-user-pages-soft " (since Linux 4.4)"
186.\" commit 759c01142a5d0f364a462346168a56de28a80f52
187The combined size (in pages) of all pipes created or set by a single
188user (without both the
189.B CAP_SYS_RESOURCE
190and
191.B CAP_SYS_ADMIN
192capabilities) after which individual pipes will be limited to 1 page.
193.\"
2adb3bd6 194.SS PIPE_BUF
3330e739 195POSIX.1 says that
2adb3bd6 196.BR write (2)s
c13182ef
MK
197of less than
198.B PIPE_BUF
199bytes must be atomic: the output data is written to the pipe as a
2adb3bd6
MK
200contiguous sequence.
201Writes of more than
202.B PIPE_BUF
24b74457 203bytes may be nonatomic: the kernel may interleave the data
2adb3bd6 204with data written by other processes.
3330e739 205POSIX.1 requires
c13182ef
MK
206.B PIPE_BUF
207to be at least 512 bytes.
208(On Linux,
209.B PIPE_BUF
2adb3bd6 210is 4096 bytes.)
ff40dbb3 211The precise semantics depend on whether the file descriptor is nonblocking
2adb3bd6 212.RB ( O_NONBLOCK ),
c13182ef 213whether there are multiple writers to the pipe, and on
2adb3bd6
MK
214.IR n ,
215the number of bytes to be written:
216.TP
217\fBO_NONBLOCK\fP disabled, \fIn\fP <= \fBPIPE_BUF\fP
218All
219.I n
220bytes are written atomically;
221.BR write (2)
222may block if there is not room for
223.I n
224bytes to be written immediately
225.TP
226\fBO_NONBLOCK\fP enabled, \fIn\fP <= \fBPIPE_BUF\fP
227If there is room to write
228.I n
229bytes to the pipe, then
230.BR write (2)
231succeeds immediately, writing all
232.I n
c13182ef 233bytes; otherwise
2adb3bd6
MK
234.BR write (2)
235fails, with
236.I errno
237set to
238.BR EAGAIN .
239.TP
240\fBO_NONBLOCK\fP disabled, \fIn\fP > \fBPIPE_BUF\fP
24b74457 241The write is nonatomic: the data given to
c13182ef
MK
242.BR write (2)
243may be interleaved with
244.BR write (2)s
245by other process;
2adb3bd6
MK
246the
247.BR write (2)
c13182ef 248blocks until
2adb3bd6
MK
249.I n
250bytes have been written.
251.TP
252\fBO_NONBLOCK\fP enabled, \fIn\fP > \fBPIPE_BUF\fP
c13182ef 253If the pipe is full, then
2adb3bd6
MK
254.BR write (2)
255fails, with
256.I errno
257set to
258.BR EAGAIN .
259Otherwise, from 1 to
c13182ef 260.I n
2adb3bd6 261bytes may be written (i.e., a "partial write" may occur;
c13182ef 262the caller should check the return value from
2adb3bd6
MK
263.BR write (2)
264to see how many bytes were actually written),
265and these bytes may be interleaved with writes by other processes.
73d8cece 266.SS Open file status flags
c13182ef 267The only open file status flags that can be meaningfully applied to
48afe71d
MK
268a pipe or FIFO are
269.B O_NONBLOCK
c13182ef 270and
48afe71d
MK
271.BR O_ASYNC .
272
273Setting the
274.B O_ASYNC
c13182ef 275flag for the read end of a pipe causes a signal
48afe71d 276.RB ( SIGIO
98faa645
MK
277by default) to be generated when new input becomes available on the pipe.
278The target for delivery of signals must be set using the
48afe71d 279.BR fcntl (2)
98faa645
MK
280.B F_SETOWN
281command.
48afe71d 282On Linux,
c13182ef 283.B O_ASYNC
48afe71d 284is supported for pipes and FIFOs only since kernel 2.6.
73d8cece 285.SS Portability notes
c13182ef 286On some systems (but not Linux), pipes are bidirectional:
2adb3bd6 287data can be transmitted in both directions between the pipe ends.
a448fdd6 288POSIX.1 requires only unidirectional pipes.
c13182ef 289Portable applications should avoid reliance on
2adb3bd6 290bidirectional pipe semantics.
47297adb 291.SH SEE ALSO
8a33c6e0 292.BR mkfifo (1),
2adb3bd6
MK
293.BR dup (2),
294.BR fcntl (2),
295.BR open (2),
296.BR pipe (2),
297.BR poll (2),
298.BR select (2),
299.BR socketpair (2),
6b1b0c98 300.BR splice (2),
2adb3bd6
MK
301.BR stat (2),
302.BR mkfifo (3),
af5b2ef2
MK
303.BR epoll (7),
304.BR fifo (7)