1PIPE(2)                    Linux Programmer's Manual                   PIPE(2)
2
3
4

NAME

6       pipe, pipe2 - create pipe
7

SYNOPSIS

9       #include <unistd.h>
10
11       /* On Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64; see NOTES */
12       struct fd_pair {
13           long fd[2];
14       };
15       struct fd_pair pipe();
16
17       /* On all other architectures */
18       int pipe(int pipefd[2]);
19
20       #define _GNU_SOURCE             /* See feature_test_macros(7) */
21       #include <fcntl.h>              /* Obtain O_* constant definitions */
22       #include <unistd.h>
23
24       int pipe2(int pipefd[2], int flags);
25

DESCRIPTION

27       pipe()  creates  a pipe, a unidirectional data channel that can be used
28       for interprocess communication.  The array pipefd is used to return two
29       file  descriptors  referring to the ends of the pipe.  pipefd[0] refers
30       to the read end of the pipe.  pipefd[1] refers to the write end of  the
31       pipe.   Data  written  to  the write end of the pipe is buffered by the
32       kernel until it is read from the read end of the pipe.  For further de‐
33       tails, see pipe(7).
34
35       If  flags is 0, then pipe2() is the same as pipe().  The following val‐
36       ues can be bitwise ORed in flags to obtain different behavior:
37
38       O_CLOEXEC
39              Set the close-on-exec (FD_CLOEXEC) flag on the two new file  de‐
40              scriptors.   See the description of the same flag in open(2) for
41              reasons why this may be useful.
42
43       O_DIRECT (since Linux 3.4)
44              Create a pipe that performs I/O in "packet" mode.  Each write(2)
45              to  the  pipe  is  dealt with as a separate packet, and read(2)s
46              from the pipe will read one packet at a time.  Note the  follow‐
47              ing points:
48
49              *  Writes  of  greater than PIPE_BUF bytes (see pipe(7)) will be
50                 split into multiple packets.  The constant  PIPE_BUF  is  de‐
51                 fined in <limits.h>.
52
53              *  If a read(2) specifies a buffer size that is smaller than the
54                 next packet, then the requested number of bytes are read, and
55                 the  excess  bytes in the packet are discarded.  Specifying a
56                 buffer size of  PIPE_BUF  will  be  sufficient  to  read  the
57                 largest possible packets (see the previous point).
58
59              *  Zero-length packets are not supported.  (A read(2) that spec‐
60                 ifies a buffer size of zero is a no-op, and returns 0.)
61
62              Older kernels that do not support this flag will  indicate  this
63              via an EINVAL error.
64
65              Since  Linux  4.5, it is possible to change the O_DIRECT setting
66              of a pipe file descriptor using fcntl(2).
67
68       O_NONBLOCK
69              Set the O_NONBLOCK file status flag on the  open  file  descrip‐
70              tions  referred to by the new file descriptors.  Using this flag
71              saves extra calls to fcntl(2) to achieve the same result.
72

RETURN VALUE

74       On success, zero is returned.  On error, -1 is returned, errno  is  set
75       appropriately, and pipefd is left unchanged.
76
77       On Linux (and other systems), pipe() does not modify pipefd on failure.
78       A requirement standardizing this behavior  was  added  in  POSIX.1-2008
79       TC2.   The  Linux-specific pipe2() system call likewise does not modify
80       pipefd on failure.
81

ERRORS

83       EFAULT pipefd is not valid.
84
85       EINVAL (pipe2()) Invalid value in flags.
86
87       EMFILE The per-process limit on the number of open file descriptors has
88              been reached.
89
90       ENFILE The system-wide limit on the total number of open files has been
91              reached.
92
93       ENFILE The user hard limit on memory that can be  allocated  for  pipes
94              has been reached and the caller is not privileged; see pipe(7).
95

VERSIONS

97       pipe2()  was  added to Linux in version 2.6.27; glibc support is avail‐
98       able starting with version 2.9.
99

CONFORMING TO

101       pipe(): POSIX.1-2001, POSIX.1-2008.
102
103       pipe2() is Linux-specific.
104

NOTES

106       The System V ABI on some architectures allows the use of more than  one
107       register  for returning multiple values; several architectures (namely,
108       Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64) (ab)use this feature  in
109       order  to  implement the pipe() system call in a functional manner: the
110       call doesn't take any arguments and returns a pair of file  descriptors
111       as  the  return  value  on  success.  The glibc pipe() wrapper function
112       transparently deals with this.  See syscall(2) for information  regard‐
113       ing registers used for storing second file descriptor.
114

EXAMPLES

116       The  following  program  creates  a pipe, and then fork(2)s to create a
117       child process; the child inherits a duplicate set of  file  descriptors
118       that  refer  to  the same pipe.  After the fork(2), each process closes
119       the file descriptors that it doesn't need for the pipe  (see  pipe(7)).
120       The  parent  then writes the string contained in the program's command-
121       line argument to the pipe, and the child reads this string a byte at  a
122       time from the pipe and echoes it on standard output.
123
124   Program source
125       #include <sys/types.h>
126       #include <sys/wait.h>
127       #include <stdio.h>
128       #include <stdlib.h>
129       #include <unistd.h>
130       #include <string.h>
131
132       int
133       main(int argc, char *argv[])
134       {
135           int pipefd[2];
136           pid_t cpid;
137           char buf;
138
139           if (argc != 2) {
140               fprintf(stderr, "Usage: %s <string>\n", argv[0]);
141               exit(EXIT_FAILURE);
142           }
143
144           if (pipe(pipefd) == -1) {
145               perror("pipe");
146               exit(EXIT_FAILURE);
147           }
148
149           cpid = fork();
150           if (cpid == -1) {
151               perror("fork");
152               exit(EXIT_FAILURE);
153           }
154
155           if (cpid == 0) {    /* Child reads from pipe */
156               close(pipefd[1]);          /* Close unused write end */
157
158               while (read(pipefd[0], &buf, 1) > 0)
159                   write(STDOUT_FILENO, &buf, 1);
160
161               write(STDOUT_FILENO, "\n", 1);
162               close(pipefd[0]);
163               _exit(EXIT_SUCCESS);
164
165           } else {            /* Parent writes argv[1] to pipe */
166               close(pipefd[0]);          /* Close unused read end */
167               write(pipefd[1], argv[1], strlen(argv[1]));
168               close(pipefd[1]);          /* Reader will see EOF */
169               wait(NULL);                /* Wait for child */
170               exit(EXIT_SUCCESS);
171           }
172       }
173

SEE ALSO

175       fork(2),   read(2),   socketpair(2),  splice(2),  tee(2),  vmsplice(2),
176       write(2), popen(3), pipe(7)
177

COLOPHON

179       This page is part of release 5.10 of the Linux  man-pages  project.   A
180       description  of  the project, information about reporting bugs, and the
181       latest    version    of    this    page,    can     be     found     at
182       https://www.kernel.org/doc/man-pages/.
183
184
185
186Linux                             2020-06-09                           PIPE(2)
Impressum