]>
Commit | Line | Data |
---|---|---|
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 | 27 | pipe \- overview of pipes and FIFOs |
2adb3bd6 | 28 | .SH DESCRIPTION |
c13182ef | 29 | Pipes and FIFOs (also known as named pipes) |
2adb3bd6 | 30 | provide a unidirectional interprocess communication channel. |
c13182ef MK |
31 | A pipe has a |
32 | .I read end | |
33 | and a | |
2adb3bd6 MK |
34 | .IR "write end" . |
35 | Data written to the write end of a pipe can be read | |
48afe71d | 36 | from the read end of the pipe. |
2adb3bd6 | 37 | |
c13182ef | 38 | A pipe is created using |
2adb3bd6 MK |
39 | .BR pipe (2), |
40 | which creates a new pipe and returns two file descriptors, | |
c13182ef | 41 | one referring to the read end of the pipe, |
2adb3bd6 | 42 | the other referring to the write end. |
c13182ef MK |
43 | Pipes can be used to create a communication channel between related |
44 | processes; see | |
6d563954 MK |
45 | .BR pipe (2) |
46 | for an example. | |
2adb3bd6 | 47 | |
28955f15 SP |
48 | A FIFO (short for First In First Out) has a name within the filesystem |
49 | (created using | |
2adb3bd6 MK |
50 | .BR mkfifo (3)), |
51 | and is opened using | |
c13182ef | 52 | .BR open (2). |
2adb3bd6 | 53 | Any process may open a FIFO, assuming the file permissions allow it. |
c13182ef | 54 | The read end is opened using the |
2adb3bd6 MK |
55 | .B O_RDONLY |
56 | flag; the write end is opened using the | |
57 | .B O_WRONLY | |
58 | flag. | |
59 | See | |
af5b2ef2 | 60 | .BR fifo (7) |
2adb3bd6 | 61 | for further details. |
c13182ef | 62 | .IR Note : |
9ee4a2b6 | 63 | although FIFOs have a pathname in the filesystem, |
c13182ef | 64 | I/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 | 67 | The only difference between pipes and FIFOs is the manner in which |
c13182ef MK |
68 | they are created and opened. |
69 | Once these tasks have been accomplished, | |
2adb3bd6 | 70 | I/O on pipes and FIFOs has exactly the same semantics. |
2adb3bd6 | 71 | |
2adb3bd6 | 72 | If a process attempts to read from an empty pipe, then |
c13182ef | 73 | .BR read (2) |
2adb3bd6 MK |
74 | will block until data is available. |
75 | If a process attempts to write to a full pipe (see below), then | |
76 | .BR write (2) | |
77 | blocks until sufficient data has been read from the pipe | |
78 | to allow the write to complete. | |
44732c9c | 79 | Nonblocking I/O is possible by using the |
2adb3bd6 | 80 | .BR fcntl (2) |
c13182ef | 81 | .B F_SETFL |
2adb3bd6 MK |
82 | operation to enable the |
83 | .B O_NONBLOCK | |
84 | open file status flag. | |
85 | ||
48afe71d | 86 | The communication channel provided by a pipe is a |
c13182ef | 87 | .IR "byte stream" : |
48afe71d MK |
88 | there is no concept of message boundaries. |
89 | ||
c13182ef MK |
90 | If all file descriptors referring to the write end of a pipe |
91 | have been closed, then an attempt to | |
2adb3bd6 MK |
92 | .BR read (2) |
93 | from the pipe will see end-of-file | |
94 | .RB ( read (2) | |
95 | will return 0). | |
c13182ef | 96 | If all file descriptors referring to the read end of a pipe |
2adb3bd6 MK |
97 | have been closed, then a |
98 | .BR write (2) | |
99 | will cause a | |
100 | .B SIGPIPE | |
101 | signal to be generated for the calling process. | |
102 | If the calling process is ignoring this signal, then | |
103 | .BR write (2) | |
104 | fails with the error | |
105 | .BR EPIPE . | |
c13182ef | 106 | An application that uses |
2adb3bd6 MK |
107 | .BR pipe (2) |
108 | and | |
109 | .BR fork (2) | |
c13182ef | 110 | should use suitable |
2adb3bd6 MK |
111 | .BR close (2) |
112 | calls to close unnecessary duplicate file descriptors; | |
c13182ef MK |
113 | this ensures that end-of-file and |
114 | .BR SIGPIPE / EPIPE | |
2adb3bd6 MK |
115 | are delivered when appropriate. |
116 | ||
48afe71d | 117 | It is not possible to apply |
2adb3bd6 | 118 | .BR lseek (2) |
48afe71d | 119 | to a pipe. |
73d8cece | 120 | .SS Pipe capacity |
2adb3bd6 MK |
121 | A pipe has a limited capacity. |
122 | If the pipe is full, then a | |
123 | .BR write (2) | |
124 | will block or fail, depending on whether the | |
c13182ef | 125 | .B O_NONBLOCK |
2adb3bd6 MK |
126 | flag is set (see below). |
127 | Different implementations have different limits for the pipe capacity. | |
c13182ef MK |
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, | |
2adb3bd6 MK |
131 | so that a writing process does not remain blocked. |
132 | ||
c13182ef | 133 | In Linux versions before 2.6.11, the capacity of a pipe was the same as |
34ccb744 | 134 | the system page size (e.g., 4096 bytes on i386). |
2adb3bd6 | 135 | Since Linux 2.6.11, the pipe capacity is 65536 bytes. |
640a438d MK |
136 | Since Linux 2.6.35, the default pipe capacity is 65536 bytes, |
137 | but the capacity can be queried and set using the | |
91d4de66 EDB |
138 | .BR fcntl (2) |
139 | .BR F_GETPIPE_SZ | |
140 | and | |
141 | .BR F_SETPIPE_SZ | |
142 | operations. | |
143 | See | |
144 | .BR fcntl (2) | |
145 | for more information. | |
2293a55f MK |
146 | |
147 | The following | |
148 | .BR ioctl (2) | |
149 | operation, which can be applied to a file descriptor | |
150 | that refers to either end of a pipe, | |
151 | places a count of the number of unread bytes in the pipe in the | |
152 | .I int | |
153 | buffer pointed to by the final argument of the call: | |
154 | ||
155 | ioctl(fd, FIONREAD, &nbytes); | |
156 | ||
157 | The | |
158 | .B FIONREAD | |
159 | operation is not specified in any standard, | |
160 | but is provided on many implementations. | |
787dd4ad | 161 | .\" |
2adb3bd6 | 162 | .SS PIPE_BUF |
3330e739 | 163 | POSIX.1 says that |
2adb3bd6 | 164 | .BR write (2)s |
c13182ef MK |
165 | of less than |
166 | .B PIPE_BUF | |
167 | bytes must be atomic: the output data is written to the pipe as a | |
2adb3bd6 MK |
168 | contiguous sequence. |
169 | Writes of more than | |
170 | .B PIPE_BUF | |
24b74457 | 171 | bytes may be nonatomic: the kernel may interleave the data |
2adb3bd6 | 172 | with data written by other processes. |
3330e739 | 173 | POSIX.1 requires |
c13182ef MK |
174 | .B PIPE_BUF |
175 | to be at least 512 bytes. | |
176 | (On Linux, | |
177 | .B PIPE_BUF | |
2adb3bd6 | 178 | is 4096 bytes.) |
ff40dbb3 | 179 | The precise semantics depend on whether the file descriptor is nonblocking |
2adb3bd6 | 180 | .RB ( O_NONBLOCK ), |
c13182ef | 181 | whether there are multiple writers to the pipe, and on |
2adb3bd6 MK |
182 | .IR n , |
183 | the number of bytes to be written: | |
184 | .TP | |
185 | \fBO_NONBLOCK\fP disabled, \fIn\fP <= \fBPIPE_BUF\fP | |
186 | All | |
187 | .I n | |
188 | bytes are written atomically; | |
189 | .BR write (2) | |
190 | may block if there is not room for | |
191 | .I n | |
192 | bytes to be written immediately | |
193 | .TP | |
194 | \fBO_NONBLOCK\fP enabled, \fIn\fP <= \fBPIPE_BUF\fP | |
195 | If there is room to write | |
196 | .I n | |
197 | bytes to the pipe, then | |
198 | .BR write (2) | |
199 | succeeds immediately, writing all | |
200 | .I n | |
c13182ef | 201 | bytes; otherwise |
2adb3bd6 MK |
202 | .BR write (2) |
203 | fails, with | |
204 | .I errno | |
205 | set to | |
206 | .BR EAGAIN . | |
207 | .TP | |
208 | \fBO_NONBLOCK\fP disabled, \fIn\fP > \fBPIPE_BUF\fP | |
24b74457 | 209 | The write is nonatomic: the data given to |
c13182ef MK |
210 | .BR write (2) |
211 | may be interleaved with | |
212 | .BR write (2)s | |
213 | by other process; | |
2adb3bd6 MK |
214 | the |
215 | .BR write (2) | |
c13182ef | 216 | blocks until |
2adb3bd6 MK |
217 | .I n |
218 | bytes have been written. | |
219 | .TP | |
220 | \fBO_NONBLOCK\fP enabled, \fIn\fP > \fBPIPE_BUF\fP | |
c13182ef | 221 | If the pipe is full, then |
2adb3bd6 MK |
222 | .BR write (2) |
223 | fails, with | |
224 | .I errno | |
225 | set to | |
226 | .BR EAGAIN . | |
227 | Otherwise, from 1 to | |
c13182ef | 228 | .I n |
2adb3bd6 | 229 | bytes may be written (i.e., a "partial write" may occur; |
c13182ef | 230 | the caller should check the return value from |
2adb3bd6 MK |
231 | .BR write (2) |
232 | to see how many bytes were actually written), | |
233 | and these bytes may be interleaved with writes by other processes. | |
73d8cece | 234 | .SS Open file status flags |
c13182ef | 235 | The only open file status flags that can be meaningfully applied to |
48afe71d MK |
236 | a pipe or FIFO are |
237 | .B O_NONBLOCK | |
c13182ef | 238 | and |
48afe71d MK |
239 | .BR O_ASYNC . |
240 | ||
241 | Setting the | |
242 | .B O_ASYNC | |
c13182ef | 243 | flag for the read end of a pipe causes a signal |
48afe71d | 244 | .RB ( SIGIO |
98faa645 MK |
245 | by default) to be generated when new input becomes available on the pipe. |
246 | The target for delivery of signals must be set using the | |
48afe71d | 247 | .BR fcntl (2) |
98faa645 MK |
248 | .B F_SETOWN |
249 | command. | |
48afe71d | 250 | On Linux, |
c13182ef | 251 | .B O_ASYNC |
48afe71d | 252 | is supported for pipes and FIFOs only since kernel 2.6. |
73d8cece | 253 | .SS Portability notes |
c13182ef | 254 | On some systems (but not Linux), pipes are bidirectional: |
2adb3bd6 | 255 | data can be transmitted in both directions between the pipe ends. |
a448fdd6 | 256 | POSIX.1 requires only unidirectional pipes. |
c13182ef | 257 | Portable applications should avoid reliance on |
2adb3bd6 | 258 | bidirectional pipe semantics. |
47297adb | 259 | .SH SEE ALSO |
8a33c6e0 | 260 | .BR mkfifo (1), |
2adb3bd6 MK |
261 | .BR dup (2), |
262 | .BR fcntl (2), | |
263 | .BR open (2), | |
264 | .BR pipe (2), | |
265 | .BR poll (2), | |
266 | .BR select (2), | |
267 | .BR socketpair (2), | |
6b1b0c98 | 268 | .BR splice (2), |
2adb3bd6 MK |
269 | .BR stat (2), |
270 | .BR mkfifo (3), | |
af5b2ef2 MK |
271 | .BR epoll (7), |
272 | .BR fifo (7) |