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       int pipe(int pipefd[2]);
12
13       #define _GNU_SOURCE             /* See feature_test_macros(7) */
14       #include <fcntl.h>              /* Obtain O_* constant definitions */
15       #include <unistd.h>
16
17       int pipe2(int pipefd[2], int flags);
18

DESCRIPTION

20       pipe()  creates  a pipe, a unidirectional data channel that can be used
21       for interprocess communication.  The array pipefd is used to return two
22       file  descriptors  referring to the ends of the pipe.  pipefd[0] refers
23       to the read end of the pipe.  pipefd[1] refers to the write end of  the
24       pipe.   Data  written  to  the write end of the pipe is buffered by the
25       kernel until it is read from the read end of  the  pipe.   For  further
26       details, see pipe(7).
27
28       If  flags is 0, then pipe2() is the same as pipe().  The following val‐
29       ues can be bitwise ORed in flags to obtain different behavior:
30
31       O_NONBLOCK  Set the O_NONBLOCK file status flag on  the  two  new  open
32                   file  descriptions.   Using  this flag saves extra calls to
33                   fcntl(2) to achieve the same result.
34
35       O_CLOEXEC   Set the close-on-exec (FD_CLOEXEC) flag on the two new file
36                   descriptors.   See  the  description  of  the  same flag in
37                   open(2) for reasons why this may be useful.
38

RETURN VALUE

40       On success, zero is returned.  On error, -1 is returned, and  errno  is
41       set appropriately.
42

ERRORS

44       EFAULT pipefd is not valid.
45
46       EINVAL (pipe2()) Invalid value in flags.
47
48       EMFILE Too many file descriptors are in use by the process.
49
50       ENFILE The  system  limit  on  the  total number of open files has been
51              reached.
52

VERSIONS

54       pipe2() was added to Linux in version 2.6.27; glibc support  is  avail‐
55       able starting with version 2.9.
56

CONFORMING TO

58       pipe(): POSIX.1-2001.
59
60       pipe2() is Linux-specific.
61

EXAMPLE

63       The  following  program  creates  a pipe, and then fork(2)s to create a
64       child process; the child inherits a duplicate set of  file  descriptors
65       that  refer  to  the same pipe.  After the fork(2), each process closes
66       the descriptors that it doesn't need for the pipe (see  pipe(7)).   The
67       parent  then  writes the string contained in the program's command-line
68       argument to the pipe, and the child reads this string a byte at a  time
69       from the pipe and echoes it on standard output.
70
71       #include <sys/wait.h>
72       #include <stdio.h>
73       #include <stdlib.h>
74       #include <unistd.h>
75       #include <string.h>
76
77       int
78       main(int argc, char *argv[])
79       {
80           int pipefd[2];
81           pid_t cpid;
82           char buf;
83
84           if (argc != 2) {
85            fprintf(stderr, "Usage: %s <string>\n", argv[0]);
86            exit(EXIT_FAILURE);
87           }
88
89           if (pipe(pipefd) == -1) {
90               perror("pipe");
91               exit(EXIT_FAILURE);
92           }
93
94           cpid = fork();
95           if (cpid == -1) {
96               perror("fork");
97               exit(EXIT_FAILURE);
98           }
99
100           if (cpid == 0) {    /* Child reads from pipe */
101               close(pipefd[1]);          /* Close unused write end */
102
103               while (read(pipefd[0], &buf, 1) > 0)
104                   write(STDOUT_FILENO, &buf, 1);
105
106               write(STDOUT_FILENO, "\n", 1);
107               close(pipefd[0]);
108               _exit(EXIT_SUCCESS);
109
110           } else {            /* Parent writes argv[1] to pipe */
111               close(pipefd[0]);          /* Close unused read end */
112               write(pipefd[1], argv[1], strlen(argv[1]));
113               close(pipefd[1]);          /* Reader will see EOF */
114               wait(NULL);                /* Wait for child */
115               exit(EXIT_SUCCESS);
116           }
117       }
118

SEE ALSO

120       fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7)
121

COLOPHON

123       This  page  is  part of release 3.53 of the Linux man-pages project.  A
124       description of the project, and information about reporting  bugs,  can
125       be found at http://www.kernel.org/doc/man-pages/.
126
127
128
129Linux                             2012-02-14                           PIPE(2)
Impressum