]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/pipe.2
getpid.2, pipe.2, abort.3, daemon.3, pthread_yield.3, stdio.3, sysconf.3, tty.4,...
[thirdparty/man-pages.git] / man2 / pipe.2
1 .\" Copyright (C) 2005, 2008, Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" (A few fragments remain from an earlier (1992) version by
3 .\" Drew Eckhardt <drew@cs.colorado.edu>.)
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified by Michael Haardt <michael@moria.de>
28 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
31 .\" Modified 2005, mtk: added an example program
32 .\" Modified 2008-01-09, mtk: rewrote DESCRIPTION; minor additions
33 .\" to EXAMPLE text.
34 .\" 2008-10-10, mtk: add description of pipe2()
35 .\"
36 .TH PIPE 2 2017-11-26 "Linux" "Linux Programmer's Manual"
37 .SH NAME
38 pipe, pipe2 \- create pipe
39 .SH SYNOPSIS
40 .nf
41 .B #include <unistd.h>
42 .PP
43 .BI "int pipe(int " pipefd "[2]);"
44
45 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
46 .BR "#include <fcntl.h>" " /* Obtain O_* constant definitions */
47 .B #include <unistd.h>
48 .PP
49 .BI "int pipe2(int " pipefd "[2], int " flags );
50 .fi
51 .SH DESCRIPTION
52 .BR pipe ()
53 creates a pipe, a unidirectional data channel that
54 can be used for interprocess communication.
55 The array
56 .IR pipefd
57 is used to return two file descriptors referring to the ends of the pipe.
58 .IR pipefd[0]
59 refers to the read end of the pipe.
60 .IR pipefd[1]
61 refers to the write end of the pipe.
62 Data written to the write end of the pipe is buffered by the kernel
63 until it is read from the read end of the pipe.
64 For further details, see
65 .BR pipe (7).
66 .PP
67 If
68 .IR flags
69 is 0, then
70 .BR pipe2 ()
71 is the same as
72 .BR pipe ().
73 The following values can be bitwise ORed in
74 .IR flags
75 to obtain different behavior:
76 .TP
77 .B O_CLOEXEC
78 Set the close-on-exec
79 .RB ( FD_CLOEXEC )
80 flag on the two new file descriptors.
81 See the description of the same flag in
82 .BR open (2)
83 for reasons why this may be useful.
84 .TP
85 .BR O_DIRECT " (since Linux 3.4)"
86 .\" commit 9883035ae7edef3ec62ad215611cb8e17d6a1a5d
87 Create a pipe that performs I/O in "packet" mode.
88 Each
89 .BR write (2)
90 to the pipe is dealt with as a separate packet, and
91 .BR read (2)s
92 from the pipe will read one packet at a time.
93 Note the following points:
94 .RS
95 .IP * 3
96 Writes of greater than
97 .BR PIPE_BUF
98 bytes (see
99 .BR pipe (7))
100 will be split into multiple packets.
101 The constant
102 .BR PIPE_BUF
103 is defined in
104 .IR <limits.h> .
105 .IP *
106 If a
107 .BR read (2)
108 specifies a buffer size that is smaller than the next packet,
109 then the requested number of bytes are read,
110 and the excess bytes in the packet are discarded.
111 Specifying a buffer size of
112 .BR PIPE_BUF
113 will be sufficient to read the largest possible packets
114 (see the previous point).
115 .IP *
116 Zero-length packets are not supported.
117 (A
118 .BR read (2)
119 that specifies a buffer size of zero is a no-op, and returns 0.)
120 .RE
121 .IP
122 Older kernels that do not support this flag will indicate this via an
123 .B EINVAL
124 error.
125 .IP
126 Since Linux 4.5,
127 .\" commit 0dbf5f20652108106cb822ad7662c786baaa03ff
128 .\" FIXME . But, it is not possible to specify O_DIRECT when opening a FIFO
129 it is possible to change the
130 .B O_DIRECT
131 setting of a pipe file descriptor using
132 .BR fcntl (2).
133 .TP
134 .B O_NONBLOCK
135 Set the
136 .BR O_NONBLOCK
137 file status flag on the two new open file descriptions.
138 Using this flag saves extra calls to
139 .BR fcntl (2)
140 to achieve the same result.
141 .SH RETURN VALUE
142 On success, zero is returned.
143 On error, \-1 is returned, and
144 .I errno
145 is set appropriately.
146 .PP
147 On Linux (and other systems),
148 .BR pipe ()
149 does not modify
150 .I pipefd
151 on failure.
152 A requirement standardizing this behavior was added in POSIX.1-2016.
153 .\" http://austingroupbugs.net/view.php?id=467
154 The Linux-specific
155 .BR pipe2 ()
156 system call
157 likewise does not modify
158 .I pipefd
159 on failure.
160 .SH ERRORS
161 .TP
162 .B EFAULT
163 .I pipefd
164 is not valid.
165 .TP
166 .B EINVAL
167 .RB ( pipe2 ())
168 Invalid value in
169 .IR flags .
170 .TP
171 .B EMFILE
172 The per-process limit on the number of open file descriptors has been reached.
173 .TP
174 .B ENFILE
175 The system-wide limit on the total number of open files has been reached.
176 .TP
177 .B ENFILE
178 The user hard limit on memory that can be allocated for pipes
179 has been reached and the caller is not privileged; see
180 .BR pipe (7).
181 .SH VERSIONS
182 .BR pipe2 ()
183 was added to Linux in version 2.6.27;
184 glibc support is available starting with
185 version 2.9.
186 .SH CONFORMING TO
187 .BR pipe ():
188 POSIX.1-2001, POSIX.1-2008.
189 .PP
190 .BR pipe2 ()
191 is Linux-specific.
192 .SH EXAMPLE
193 .\" fork.2 refers to this example program.
194 The following program creates a pipe, and then
195 .BR fork (2)s
196 to create a child process;
197 the child inherits a duplicate set of file
198 descriptors that refer to the same pipe.
199 After the
200 .BR fork (2),
201 each process closes the file descriptors that it doesn't need for the pipe
202 (see
203 .BR pipe (7)).
204 The parent then writes the string contained in the program's
205 command-line argument to the pipe,
206 and the child reads this string a byte at a time from the pipe
207 and echoes it on standard output.
208 .SS Program source
209 .EX
210 #include <sys/types.h>
211 #include <sys/wait.h>
212 #include <stdio.h>
213 #include <stdlib.h>
214 #include <unistd.h>
215 #include <string.h>
216
217 int
218 main(int argc, char *argv[])
219 {
220 int pipefd[2];
221 pid_t cpid;
222 char buf;
223
224 if (argc != 2) {
225 fprintf(stderr, "Usage: %s <string>\\n", argv[0]);
226 exit(EXIT_FAILURE);
227 }
228
229 if (pipe(pipefd) == \-1) {
230 perror("pipe");
231 exit(EXIT_FAILURE);
232 }
233
234 cpid = fork();
235 if (cpid == \-1) {
236 perror("fork");
237 exit(EXIT_FAILURE);
238 }
239
240 if (cpid == 0) { /* Child reads from pipe */
241 close(pipefd[1]); /* Close unused write end */
242
243 while (read(pipefd[0], &buf, 1) > 0)
244 write(STDOUT_FILENO, &buf, 1);
245
246 write(STDOUT_FILENO, "\\n", 1);
247 close(pipefd[0]);
248 _exit(EXIT_SUCCESS);
249
250 } else { /* Parent writes argv[1] to pipe */
251 close(pipefd[0]); /* Close unused read end */
252 write(pipefd[1], argv[1], strlen(argv[1]));
253 close(pipefd[1]); /* Reader will see EOF */
254 wait(NULL); /* Wait for child */
255 exit(EXIT_SUCCESS);
256 }
257 }
258 .EE
259 .SH SEE ALSO
260 .BR fork (2),
261 .BR read (2),
262 .BR socketpair (2),
263 .BR splice (2),
264 .BR tee (2),
265 .BR vmsplice (2),
266 .BR write (2),
267 .BR popen (3),
268 .BR pipe (7)