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
14       #include <unistd.h>
15
16       int pipe2(int pipefd[2], int flags);
17

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

VERSIONS

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

CONFORMING TO

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

EXAMPLE

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

SEE ALSO

117       fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7)
118

COLOPHON

120       This  page  is  part of release 3.22 of the Linux man-pages project.  A
121       description of the project, and information about reporting  bugs,  can
122       be found at http://www.kernel.org/doc/man-pages/.
123
124
125
126Linux                             2008-11-04                           PIPE(2)
Impressum