1PIPE(2) Linux Programmer's Manual PIPE(2)
2
3
4
6 pipe, pipe2 - create pipe
7
9 #include <unistd.h>
10
11 int pipe(int pipefd[2]);
12
13 #define _GNU_SOURCE /* See feature_test_macros(7) */
14 #include <fcntl.h> /* Definition of O_* constants */
15 #include <unistd.h>
16
17 int pipe2(int pipefd[2], int flags);
18
19 /* On Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64, pipe() has the
20 following prototype; see NOTES */
21
22 #include <unistd.h>
23
24 struct fd_pair {
25 long fd[2];
26 };
27 struct fd_pair pipe(void);
28
30 pipe() creates a pipe, a unidirectional data channel that can be used
31 for interprocess communication. The array pipefd is used to return two
32 file descriptors referring to the ends of the pipe. pipefd[0] refers
33 to the read end of the pipe. pipefd[1] refers to the write end of the
34 pipe. Data written to the write end of the pipe is buffered by the
35 kernel until it is read from the read end of the pipe. For further de‐
36 tails, see pipe(7).
37
38 If flags is 0, then pipe2() is the same as pipe(). The following val‐
39 ues can be bitwise ORed in flags to obtain different behavior:
40
41 O_CLOEXEC
42 Set the close-on-exec (FD_CLOEXEC) flag on the two new file de‐
43 scriptors. See the description of the same flag in open(2) for
44 reasons why this may be useful.
45
46 O_DIRECT (since Linux 3.4)
47 Create a pipe that performs I/O in "packet" mode. Each write(2)
48 to the pipe is dealt with as a separate packet, and read(2)s
49 from the pipe will read one packet at a time. Note the follow‐
50 ing points:
51
52 * Writes of greater than PIPE_BUF bytes (see pipe(7)) will be
53 split into multiple packets. The constant PIPE_BUF is de‐
54 fined in <limits.h>.
55
56 * If a read(2) specifies a buffer size that is smaller than the
57 next packet, then the requested number of bytes are read, and
58 the excess bytes in the packet are discarded. Specifying a
59 buffer size of PIPE_BUF will be sufficient to read the
60 largest possible packets (see the previous point).
61
62 * Zero-length packets are not supported. (A read(2) that spec‐
63 ifies a buffer size of zero is a no-op, and returns 0.)
64
65 Older kernels that do not support this flag will indicate this
66 via an EINVAL error.
67
68 Since Linux 4.5, it is possible to change the O_DIRECT setting
69 of a pipe file descriptor using fcntl(2).
70
71 O_NONBLOCK
72 Set the O_NONBLOCK file status flag on the open file descrip‐
73 tions referred to by the new file descriptors. Using this flag
74 saves extra calls to fcntl(2) to achieve the same result.
75
77 On success, zero is returned. On error, -1 is returned, errno is set
78 to indicate the error, and pipefd is left unchanged.
79
80 On Linux (and other systems), pipe() does not modify pipefd on failure.
81 A requirement standardizing this behavior was added in POSIX.1-2008
82 TC2. The Linux-specific pipe2() system call likewise does not modify
83 pipefd on failure.
84
86 EFAULT pipefd is not valid.
87
88 EINVAL (pipe2()) Invalid value in flags.
89
90 EMFILE The per-process limit on the number of open file descriptors has
91 been reached.
92
93 ENFILE The system-wide limit on the total number of open files has been
94 reached.
95
96 ENFILE The user hard limit on memory that can be allocated for pipes
97 has been reached and the caller is not privileged; see pipe(7).
98
100 pipe2() was added to Linux in version 2.6.27; glibc support is avail‐
101 able starting with version 2.9.
102
104 pipe(): POSIX.1-2001, POSIX.1-2008.
105
106 pipe2() is Linux-specific.
107
109 The System V ABI on some architectures allows the use of more than one
110 register for returning multiple values; several architectures (namely,
111 Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64) (ab)use this feature in
112 order to implement the pipe() system call in a functional manner: the
113 call doesn't take any arguments and returns a pair of file descriptors
114 as the return value on success. The glibc pipe() wrapper function
115 transparently deals with this. See syscall(2) for information regard‐
116 ing registers used for storing second file descriptor.
117
119 The following program creates a pipe, and then fork(2)s to create a
120 child process; the child inherits a duplicate set of file descriptors
121 that refer to the same pipe. After the fork(2), each process closes
122 the file descriptors that it doesn't need for the pipe (see pipe(7)).
123 The parent then writes the string contained in the program's command-
124 line argument to the pipe, and the child reads this string a byte at a
125 time from the pipe and echoes it on standard output.
126
127 Program source
128 #include <sys/types.h>
129 #include <sys/wait.h>
130 #include <stdio.h>
131 #include <stdlib.h>
132 #include <unistd.h>
133 #include <string.h>
134
135 int
136 main(int argc, char *argv[])
137 {
138 int pipefd[2];
139 pid_t cpid;
140 char buf;
141
142 if (argc != 2) {
143 fprintf(stderr, "Usage: %s <string>\n", argv[0]);
144 exit(EXIT_FAILURE);
145 }
146
147 if (pipe(pipefd) == -1) {
148 perror("pipe");
149 exit(EXIT_FAILURE);
150 }
151
152 cpid = fork();
153 if (cpid == -1) {
154 perror("fork");
155 exit(EXIT_FAILURE);
156 }
157
158 if (cpid == 0) { /* Child reads from pipe */
159 close(pipefd[1]); /* Close unused write end */
160
161 while (read(pipefd[0], &buf, 1) > 0)
162 write(STDOUT_FILENO, &buf, 1);
163
164 write(STDOUT_FILENO, "\n", 1);
165 close(pipefd[0]);
166 _exit(EXIT_SUCCESS);
167
168 } else { /* Parent writes argv[1] to pipe */
169 close(pipefd[0]); /* Close unused read end */
170 write(pipefd[1], argv[1], strlen(argv[1]));
171 close(pipefd[1]); /* Reader will see EOF */
172 wait(NULL); /* Wait for child */
173 exit(EXIT_SUCCESS);
174 }
175 }
176
178 fork(2), read(2), socketpair(2), splice(2), tee(2), vmsplice(2),
179 write(2), popen(3), pipe(7)
180
182 This page is part of release 5.13 of the Linux man-pages project. A
183 description of the project, information about reporting bugs, and the
184 latest version of this page, can be found at
185 https://www.kernel.org/doc/man-pages/.
186
187
188
189Linux 2021-03-22 PIPE(2)