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 <stdio.h>
72       #include <stdlib.h>
73       #include <unistd.h>
74       #include <string.h>
75
76       int
77       main(int argc, char *argv[])
78       {
79           int pipefd[2];
80           pid_t cpid;
81           char buf;
82
83           if (argc != 2) {
84            fprintf(stderr, "Usage: %s <string>\n", argv[0]);
85            exit(EXIT_FAILURE);
86           }
87
88           if (pipe(pipefd) == -1) {
89               perror("pipe");
90               exit(EXIT_FAILURE);
91           }
92
93           cpid = fork();
94           if (cpid == -1) {
95               perror("fork");
96               exit(EXIT_FAILURE);
97           }
98
99           if (cpid == 0) {    /* Child reads from pipe */
100               close(pipefd[1]);          /* Close unused write end */
101
102               while (read(pipefd[0], &buf, 1) > 0)
103                   write(STDOUT_FILENO, &buf, 1);
104
105               write(STDOUT_FILENO, "\n", 1);
106               close(pipefd[0]);
107               _exit(EXIT_SUCCESS);
108
109           } else {            /* Parent writes argv[1] to pipe */
110               close(pipefd[0]);          /* Close unused read end */
111               write(pipefd[1], argv[1], strlen(argv[1]));
112               close(pipefd[1]);          /* Reader will see EOF */
113               wait(NULL);                /* Wait for child */
114               exit(EXIT_SUCCESS);
115           }
116       }
117

SEE ALSO

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

COLOPHON

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