OSDN Git Service

Update perkamon to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man7 / pipe.7
1 .\" Copyright (C) 2005 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH PIPE 7 2014-07-08 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 pipe \- overview of pipes and FIFOs
28 .SH DESCRIPTION
29 Pipes and FIFOs (also known as named pipes)
30 provide a unidirectional interprocess communication channel.
31 A pipe has a
32 .I read end
33 and a
34 .IR "write end" .
35 Data written to the write end of a pipe can be read
36 from the read end of the pipe.
37
38 A pipe is created using
39 .BR pipe (2),
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
44 processes; see
45 .BR pipe (2)
46 for an example.
47
48 A FIFO (short for First In First Out) has a name within the filesystem
49 (created using
50 .BR mkfifo (3)),
51 and is opened using
52 .BR open (2).
53 Any process may open a FIFO, assuming the file permissions allow it.
54 The read end is opened using the
55 .B O_RDONLY
56 flag; the write end is opened using the
57 .B O_WRONLY
58 flag.
59 See
60 .BR fifo (7)
61 for further details.
62 .IR Note :
63 although FIFOs have a pathname in the filesystem,
64 I/O on FIFOs does not involve operations on the underlying device
65 (if there is one).
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.
71
72 If a process attempts to read from an empty pipe, then
73 .BR read (2)
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.
79 Nonblocking I/O is possible by using the
80 .BR fcntl (2)
81 .B F_SETFL
82 operation to enable the
83 .B O_NONBLOCK
84 open file status flag.
85
86 The communication channel provided by a pipe is a
87 .IR "byte stream" :
88 there is no concept of message boundaries.
89
90 If all file descriptors referring to the write end of a pipe
91 have been closed, then an attempt to
92 .BR read (2)
93 from the pipe will see end-of-file
94 .RB ( read (2)
95 will return 0).
96 If all file descriptors referring to the read end of a pipe
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 .
106 An application that uses
107 .BR pipe (2)
108 and
109 .BR fork (2)
110 should use suitable
111 .BR close (2)
112 calls to close unnecessary duplicate file descriptors;
113 this ensures that end-of-file and
114 .BR SIGPIPE / EPIPE
115 are delivered when appropriate.
116
117 It is not possible to apply
118 .BR lseek (2)
119 to a pipe.
120 .SS Pipe capacity
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
125 .B O_NONBLOCK
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.
132
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 65536 bytes.
136 Since Linux 2.6.35, the default pipe capacity is 65536 bytes,
137 but the capacity can be queried and set using the
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.
146
147 .SS PIPE_BUF
148 POSIX.1-2001 says that
149 .BR write (2)s
150 of less than
151 .B PIPE_BUF
152 bytes must be atomic: the output data is written to the pipe as a
153 contiguous sequence.
154 Writes of more than
155 .B PIPE_BUF
156 bytes may be nonatomic: the kernel may interleave the data
157 with data written by other processes.
158 POSIX.1-2001 requires
159 .B PIPE_BUF
160 to be at least 512 bytes.
161 (On Linux,
162 .B PIPE_BUF
163 is 4096 bytes.)
164 The precise semantics depend on whether the file descriptor is nonblocking
165 .RB ( O_NONBLOCK ),
166 whether there are multiple writers to the pipe, and on
167 .IR n ,
168 the number of bytes to be written:
169 .TP
170 \fBO_NONBLOCK\fP disabled, \fIn\fP <= \fBPIPE_BUF\fP
171 All
172 .I n
173 bytes are written atomically;
174 .BR write (2)
175 may block if there is not room for
176 .I n
177 bytes to be written immediately
178 .TP
179 \fBO_NONBLOCK\fP enabled, \fIn\fP <= \fBPIPE_BUF\fP
180 If there is room to write
181 .I n
182 bytes to the pipe, then
183 .BR write (2)
184 succeeds immediately, writing all
185 .I n
186 bytes; otherwise
187 .BR write (2)
188 fails, with
189 .I errno
190 set to
191 .BR EAGAIN .
192 .TP
193 \fBO_NONBLOCK\fP disabled, \fIn\fP > \fBPIPE_BUF\fP
194 The write is nonatomic: the data given to
195 .BR write (2)
196 may be interleaved with
197 .BR write (2)s
198 by other process;
199 the
200 .BR write (2)
201 blocks until
202 .I n
203 bytes have been written.
204 .TP
205 \fBO_NONBLOCK\fP enabled, \fIn\fP > \fBPIPE_BUF\fP
206 If the pipe is full, then
207 .BR write (2)
208 fails, with
209 .I errno
210 set to
211 .BR EAGAIN .
212 Otherwise, from 1 to
213 .I n
214 bytes may be written (i.e., a "partial write" may occur;
215 the caller should check the return value from
216 .BR write (2)
217 to see how many bytes were actually written),
218 and these bytes may be interleaved with writes by other processes.
219 .SS Open file status flags
220 The only open file status flags that can be meaningfully applied to
221 a pipe or FIFO are
222 .B O_NONBLOCK
223 and
224 .BR O_ASYNC .
225
226 Setting the
227 .B O_ASYNC
228 flag for the read end of a pipe causes a signal
229 .RB ( SIGIO
230 by default) to be generated when new input becomes available on the pipe
231 (see
232 .BR fcntl (2)
233 for details).
234 On Linux,
235 .B O_ASYNC
236 is supported for pipes and FIFOs only since kernel 2.6.
237 .SS Portability notes
238 On some systems (but not Linux), pipes are bidirectional:
239 data can be transmitted in both directions between the pipe ends.
240 According to POSIX.1-2001, pipes only need to be unidirectional.
241 Portable applications should avoid reliance on
242 bidirectional pipe semantics.
243 .SH SEE ALSO
244 .BR dup (2),
245 .BR fcntl (2),
246 .BR open (2),
247 .BR pipe (2),
248 .BR poll (2),
249 .BR select (2),
250 .BR socketpair (2),
251 .BR stat (2),
252 .BR mkfifo (3),
253 .BR epoll (7),
254 .BR fifo (7)
255 .SH COLOPHON
256 This page is part of release 3.78 of the Linux
257 .I man-pages
258 project.
259 A description of the project,
260 information about reporting bugs,
261 and the latest version of this page,
262 can be found at
263 \%http://www.kernel.org/doc/man\-pages/.