]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/pipe.7
epoll.4 is now epoll.7
[thirdparty/man-pages.git] / man7 / pipe.7
CommitLineData
2adb3bd6
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" Copyright (C) 2005 Michael Kerrisk <mtk-manpages@gmx.net>
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.
13.\"
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
17.\" the use of the information contained herein.
18.\"
19.\" Formatted or processed versions of this manual, if unaccompanied by
20.\" the source, must acknowledge the copyright and authors of this work.
21.\"
22.TH PIPE 7 2005-12-08 "Linux 2.6.15" "Linux Programmer's Manual"
23.SH NAME
3a20b4ca 24pipe \- overview of pipes and FIFOs
2adb3bd6
MK
25.SH DESCRIPTION
26Pipes and FIFOs (also known as named pipes)
27provide a unidirectional interprocess communication channel.
28A pipe has a
29.I read end
30and a
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
MK
34
35A pipe is created using
36.BR pipe (2),
37which creates a new pipe and returns two file descriptors,
38one referring to the read end of the pipe,
39the other referring to the write end.
6d563954
MK
40Pipes can be used to create a communication channel between related
41processes; see
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
49.BR open (2).
50Any process may open a FIFO, assuming the file permissions allow it.
51The read end is opened using the
52.B O_RDONLY
53flag; the write end is opened using the
54.B O_WRONLY
55flag.
56See
57.BR fifo (4)
58for further details.
59.IR Note :
60although FIFOs have a pathname in the file system,
48afe71d
MK
61I/O on FIFOs does not involve operations on the underlying device
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
65they are created and opened.
66Once these tasks have been accomplished,
67I/O on pipes and FIFOs has exactly the same semantics.
2adb3bd6 68
2adb3bd6
MK
69If a process attempts to read from an empty pipe, then
70.BR read (2)
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.
76Non-blocking I/O is possible by using the
77.BR fcntl (2)
78.B F_SETFL
79operation to enable the
80.B O_NONBLOCK
81open file status flag.
82
48afe71d
MK
83The communication channel provided by a pipe is a
84.IR "byte stream" :
85there is no concept of message boundaries.
86
2adb3bd6
MK
87If all file descriptors referring to the write end of a pipe
88have been closed, then an attempt to
89.BR read (2)
90from the pipe will see end-of-file
91.RB ( read (2)
92will return 0).
93If all file descriptors referring to the read end of a pipe
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 .
103An application that uses
104.BR pipe (2)
105and
106.BR fork (2)
107should use suitable
108.BR close (2)
109calls to close unnecessary duplicate file descriptors;
110this ensures that end-of-file and
111.BR SIGPIPE / EPIPE
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
122.B O_NONBLOCK
123flag is set (see below).
124Different implementations have different limits for the pipe capacity.
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,
128so that a writing process does not remain blocked.
129
130In Linux versions before 2.6.11, the capacity of a pipe was the same as
131the system page size (e.g., 4096 bytes on x86).
132Since Linux 2.6.11, the pipe capacity is 65536 bytes.
133.SS PIPE_BUF
134POSIX.1 says that
135.BR write (2)s
136of less than
137.B PIPE_BUF
138bytes must be atomic: the output data is written to the pipe as a
139contiguous sequence.
140Writes of more than
141.B PIPE_BUF
142bytes may be non-atomic: the kernel may interleave the data
143with data written by other processes.
144POSIX.1 requires
145.B PIPE_BUF
146to be at least 512 bytes. (On Linux,
147.B PIPE_BUF
148is 4096 bytes.)
149The precise semantics depend on whether the file descriptor is non-blocking
150.RB ( O_NONBLOCK ),
151whether there are multiple writers to the pipe, and on
152.IR n ,
153the number of bytes to be written:
154.TP
155\fBO_NONBLOCK\fP disabled, \fIn\fP <= \fBPIPE_BUF\fP
156All
157.I n
158bytes are written atomically;
159.BR write (2)
160may block if there is not room for
161.I n
162bytes to be written immediately
163.TP
164\fBO_NONBLOCK\fP enabled, \fIn\fP <= \fBPIPE_BUF\fP
165If there is room to write
166.I n
167bytes to the pipe, then
168.BR write (2)
169succeeds immediately, writing all
170.I n
171bytes; otherwise
172.BR write (2)
173fails, with
174.I errno
175set to
176.BR EAGAIN .
177.TP
178\fBO_NONBLOCK\fP disabled, \fIn\fP > \fBPIPE_BUF\fP
179The write is non-atomic: the data given to
180.BR write (2)
181may be interleaved with
182.BR write (2)s
183by other process;
184the
185.BR write (2)
186blocks until
187.I n
188bytes have been written.
189.TP
190\fBO_NONBLOCK\fP enabled, \fIn\fP > \fBPIPE_BUF\fP
191If the pipe is full, then
192.BR write (2)
193fails, with
194.I errno
195set to
196.BR EAGAIN .
197Otherwise, from 1 to
198.I n
199bytes may be written (i.e., a "partial write" may occur;
200the caller should check the return value from
201.BR write (2)
202to see how many bytes were actually written),
203and these bytes may be interleaved with writes by other processes.
48afe71d
MK
204.SS "Open File Status Flags"
205The only open file status flags that can be meaningfully applied to
206a pipe or FIFO are
207.B O_NONBLOCK
208and
209.BR O_ASYNC .
210
211Setting the
212.B O_ASYNC
213flag for the read end of a pipe causes a signal
214.RB ( SIGIO
215by default) to be generated when new input becomes available on the pipe
216(see
217.BR fcntl (2)
218for details).
219On Linux,
220.B O_ASYNC
221is supported for pipes and FIFOs only since kernel 2.6.
2adb3bd6
MK
222.SS "Portability notes"
223On some systems (but not Linux), pipes are bidirectional:
224data can be transmitted in both directions between the pipe ends.
225According to POSIX.1, pipes only need to be unidirectional.
226Portable applications should avoid reliance on
227bidirectional pipe semantics.
228.SH "SEE ALSO"
229.BR dup (2),
230.BR fcntl (2),
231.BR open (2),
232.BR pipe (2),
233.BR poll (2),
234.BR select (2),
235.BR socketpair (2),
236.BR stat (2),
237.BR mkfifo (3),
238.BR fifo (4),
2315114c 239.BR epoll (7)