]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/pipe.7
Added some .\" comments, and a FIXME.
[thirdparty/man-pages.git] / man7 / pipe.7
CommitLineData
2adb3bd6
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
c11b1abf 3.\" Copyright (C) 2005 Michael Kerrisk <mtk.manpages@gmail.com>
2adb3bd6
MK
4.\"
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
c13182ef 13.\"
2adb3bd6
MK
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
c13182ef
MK
17.\" the use of the information contained herein.
18.\"
2adb3bd6
MK
19.\" Formatted or processed versions of this manual, if unaccompanied by
20.\" the source, must acknowledge the copyright and authors of this work.
21.\"
d9343c5c 22.TH PIPE 7 2005-12-08 "Linux" "Linux Programmer's Manual"
2adb3bd6 23.SH NAME
3a20b4ca 24pipe \- overview of pipes and FIFOs
2adb3bd6 25.SH DESCRIPTION
c13182ef 26Pipes and FIFOs (also known as named pipes)
2adb3bd6 27provide a unidirectional interprocess communication channel.
c13182ef
MK
28A pipe has a
29.I read end
30and a
2adb3bd6
MK
31.IR "write end" .
32Data written to the write end of a pipe can be read
48afe71d 33from the read end of the pipe.
2adb3bd6 34
c13182ef 35A pipe is created using
2adb3bd6
MK
36.BR pipe (2),
37which creates a new pipe and returns two file descriptors,
c13182ef 38one referring to the read end of the pipe,
2adb3bd6 39the other referring to the write end.
c13182ef
MK
40Pipes can be used to create a communication channel between related
41processes; see
6d563954
MK
42.BR pipe (2)
43for an example.
2adb3bd6
MK
44
45A FIFO (short for First In First Out) has a name within the file
46system (created using
47.BR mkfifo (3)),
48and is opened using
c13182ef 49.BR open (2).
2adb3bd6 50Any process may open a FIFO, assuming the file permissions allow it.
c13182ef 51The read end is opened using the
2adb3bd6
MK
52.B O_RDONLY
53flag; the write end is opened using the
54.B O_WRONLY
55flag.
56See
af5b2ef2 57.BR fifo (7)
2adb3bd6 58for further details.
c13182ef 59.IR Note :
2adb3bd6 60although FIFOs have a pathname in the file system,
c13182ef 61I/O on FIFOs does not involve operations on the underlying device
48afe71d 62(if there is one).
2adb3bd6
MK
63.SS "I/O on Pipes and FIFOs"
64The only difference between pipes and FIFOs is the manner in which
c13182ef
MK
65they are created and opened.
66Once these tasks have been accomplished,
2adb3bd6 67I/O on pipes and FIFOs has exactly the same semantics.
2adb3bd6 68
2adb3bd6 69If a process attempts to read from an empty pipe, then
c13182ef 70.BR read (2)
2adb3bd6
MK
71will block until data is available.
72If a process attempts to write to a full pipe (see below), then
73.BR write (2)
74blocks until sufficient data has been read from the pipe
75to allow the write to complete.
c13182ef 76Non-blocking I/O is possible by using the
2adb3bd6 77.BR fcntl (2)
c13182ef 78.B F_SETFL
2adb3bd6
MK
79operation to enable the
80.B O_NONBLOCK
81open file status flag.
82
48afe71d 83The communication channel provided by a pipe is a
c13182ef 84.IR "byte stream" :
48afe71d
MK
85there is no concept of message boundaries.
86
c13182ef
MK
87If all file descriptors referring to the write end of a pipe
88have been closed, then an attempt to
2adb3bd6
MK
89.BR read (2)
90from the pipe will see end-of-file
91.RB ( read (2)
92will return 0).
c13182ef 93If all file descriptors referring to the read end of a pipe
2adb3bd6
MK
94have been closed, then a
95.BR write (2)
96will cause a
97.B SIGPIPE
98signal to be generated for the calling process.
99If the calling process is ignoring this signal, then
100.BR write (2)
101fails with the error
102.BR EPIPE .
c13182ef 103An application that uses
2adb3bd6
MK
104.BR pipe (2)
105and
106.BR fork (2)
c13182ef 107should use suitable
2adb3bd6
MK
108.BR close (2)
109calls to close unnecessary duplicate file descriptors;
c13182ef
MK
110this ensures that end-of-file and
111.BR SIGPIPE / EPIPE
2adb3bd6
MK
112are delivered when appropriate.
113
48afe71d 114It is not possible to apply
2adb3bd6 115.BR lseek (2)
48afe71d 116to a pipe.
2adb3bd6
MK
117.SS "Pipe Capacity"
118A pipe has a limited capacity.
119If the pipe is full, then a
120.BR write (2)
121will block or fail, depending on whether the
c13182ef 122.B O_NONBLOCK
2adb3bd6
MK
123flag is set (see below).
124Different implementations have different limits for the pipe capacity.
c13182ef
MK
125Applications should not rely on a particular capacity:
126an application should be designed so that a reading process consumes data
127as soon as it is available,
2adb3bd6
MK
128so that a writing process does not remain blocked.
129
c13182ef 130In Linux versions before 2.6.11, the capacity of a pipe was the same as
34ccb744 131the system page size (e.g., 4096 bytes on i386).
2adb3bd6
MK
132Since Linux 2.6.11, the pipe capacity is 65536 bytes.
133.SS PIPE_BUF
a7fadb55 134POSIX.1-2001 says that
2adb3bd6 135.BR write (2)s
c13182ef
MK
136of less than
137.B PIPE_BUF
138bytes must be atomic: the output data is written to the pipe as a
2adb3bd6
MK
139contiguous sequence.
140Writes of more than
141.B PIPE_BUF
c13182ef 142bytes may be non-atomic: the kernel may interleave the data
2adb3bd6 143with data written by other processes.
c13182ef
MK
144POSIX.1-2001 requires
145.B PIPE_BUF
146to be at least 512 bytes.
147(On Linux,
148.B PIPE_BUF
2adb3bd6
MK
149is 4096 bytes.)
150The precise semantics depend on whether the file descriptor is non-blocking
151.RB ( O_NONBLOCK ),
c13182ef 152whether there are multiple writers to the pipe, and on
2adb3bd6
MK
153.IR n ,
154the number of bytes to be written:
155.TP
156\fBO_NONBLOCK\fP disabled, \fIn\fP <= \fBPIPE_BUF\fP
157All
158.I n
159bytes are written atomically;
160.BR write (2)
161may block if there is not room for
162.I n
163bytes to be written immediately
164.TP
165\fBO_NONBLOCK\fP enabled, \fIn\fP <= \fBPIPE_BUF\fP
166If there is room to write
167.I n
168bytes to the pipe, then
169.BR write (2)
170succeeds immediately, writing all
171.I n
c13182ef 172bytes; otherwise
2adb3bd6
MK
173.BR write (2)
174fails, with
175.I errno
176set to
177.BR EAGAIN .
178.TP
179\fBO_NONBLOCK\fP disabled, \fIn\fP > \fBPIPE_BUF\fP
c13182ef
MK
180The write is non-atomic: the data given to
181.BR write (2)
182may be interleaved with
183.BR write (2)s
184by other process;
2adb3bd6
MK
185the
186.BR write (2)
c13182ef 187blocks until
2adb3bd6
MK
188.I n
189bytes have been written.
190.TP
191\fBO_NONBLOCK\fP enabled, \fIn\fP > \fBPIPE_BUF\fP
c13182ef 192If the pipe is full, then
2adb3bd6
MK
193.BR write (2)
194fails, with
195.I errno
196set to
197.BR EAGAIN .
198Otherwise, from 1 to
c13182ef 199.I n
2adb3bd6 200bytes may be written (i.e., a "partial write" may occur;
c13182ef 201the caller should check the return value from
2adb3bd6
MK
202.BR write (2)
203to see how many bytes were actually written),
204and these bytes may be interleaved with writes by other processes.
48afe71d 205.SS "Open File Status Flags"
c13182ef 206The only open file status flags that can be meaningfully applied to
48afe71d
MK
207a pipe or FIFO are
208.B O_NONBLOCK
c13182ef 209and
48afe71d
MK
210.BR O_ASYNC .
211
212Setting the
213.B O_ASYNC
c13182ef 214flag for the read end of a pipe causes a signal
48afe71d
MK
215.RB ( SIGIO
216by default) to be generated when new input becomes available on the pipe
217(see
218.BR fcntl (2)
219for details).
220On Linux,
c13182ef 221.B O_ASYNC
48afe71d 222is supported for pipes and FIFOs only since kernel 2.6.
2adb3bd6 223.SS "Portability notes"
c13182ef 224On some systems (but not Linux), pipes are bidirectional:
2adb3bd6 225data can be transmitted in both directions between the pipe ends.
a7fadb55 226According to POSIX.1-2001, pipes only need to be unidirectional.
c13182ef 227Portable applications should avoid reliance on
2adb3bd6
MK
228bidirectional pipe semantics.
229.SH "SEE ALSO"
230.BR dup (2),
231.BR fcntl (2),
232.BR open (2),
233.BR pipe (2),
234.BR poll (2),
235.BR select (2),
236.BR socketpair (2),
237.BR stat (2),
238.BR mkfifo (3),
af5b2ef2
MK
239.BR epoll (7),
240.BR fifo (7)