1PIPE(3P)                   POSIX Programmer's Manual                  PIPE(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       pipe — create an interprocess channel
13

SYNOPSIS

15       #include <unistd.h>
16
17       int pipe(int fildes[2]);
18

DESCRIPTION

20       The pipe() function shall create a pipe and place two file descriptors,
21       one  each into the arguments fildes[0] and fildes[1], that refer to the
22       open file descriptions for the read and write ends  of  the  pipe.  The
23       file  descriptors shall be allocated as described in Section 2.14, File
24       Descriptor Allocation.  The O_NONBLOCK and FD_CLOEXEC  flags  shall  be
25       clear  on  both  file descriptors. (The fcntl() function can be used to
26       set both these flags.)
27
28       Data can be written to the file descriptor fildes[1] and read from  the
29       file  descriptor  fildes[0].   A  read on the file descriptor fildes[0]
30       shall access data written to the file descriptor fildes[1] on a  first-
31       in-first-out  basis.  It  is unspecified whether fildes[0] is also open
32       for writing and whether fildes[1] is also open for reading.
33
34       A process has the pipe open for reading (correspondingly writing) if it
35       has  a  file  descriptor  open  that  refers to the read end, fildes[0]
36       (write end, fildes[1]).
37
38       The pipe's user ID shall be set to the effective user ID of the calling
39       process.
40
41       The pipe's group ID shall be set to the effective group ID of the call‐
42       ing process.
43
44       Upon successful completion, pipe() shall mark for update the last  data
45       access,  last data modification, and last file status change timestamps
46       of the pipe.
47

RETURN VALUE

49       Upon successful completion, 0 shall be returned; otherwise, -1 shall be
50       returned and errno set to indicate the error, no file descriptors shall
51       be allocated and the contents of fildes shall be left unmodified.
52

ERRORS

54       The pipe() function shall fail if:
55
56       EMFILE All, or all but one, of the file descriptors  available  to  the
57              process are currently open.
58
59       ENFILE The  number  of  simultaneously  open  files in the system would
60              exceed a system-imposed limit.
61
62       The following sections are informative.
63

EXAMPLES

65   Using a Pipe to Pass Data Between a Parent Process and a Child Process
66       The following example demonstrates the use of a pipe to  transfer  data
67       between  a  parent  process  and  a  child  process.  Error handling is
68       excluded, but otherwise this code demonstrates good practice when using
69       pipes:  after the fork() the two processes close the unused ends of the
70       pipe before they commence transferring data.
71
72
73           #include <stdlib.h>
74           #include <unistd.h>
75           ...
76
77           int fildes[2];
78           const int BSIZE = 100;
79           char buf[BSIZE];
80           ssize_t nbytes;
81           int status;
82
83           status = pipe(fildes);
84           if (status == -1 ) {
85               /* an error occurred */
86               ...
87           }
88
89           switch (fork()) {
90           case -1: /* Handle error */
91               break;
92
93           case 0:  /* Child - reads from pipe */
94               close(fildes[1]);                       /* Write end is unused */
95               nbytes = read(fildes[0], buf, BSIZE);   /* Get data from pipe */
96               /* At this point, a further read would see end-of-file ... */
97               close(fildes[0]);                       /* Finished with pipe */
98               exit(EXIT_SUCCESS);
99
100           default:  /* Parent - writes to pipe */
101               close(fildes[0]);                       /* Read end is unused */
102               write(fildes[1], "Hello world\n", 12);  /* Write data on pipe */
103               close(fildes[1]);                       /* Child will see EOF */
104               exit(EXIT_SUCCESS);
105           }
106

APPLICATION USAGE

108       None.
109

RATIONALE

111       The wording carefully avoids using the verb ``to  open''  in  order  to
112       avoid any implication of use of open(); see also write().
113

FUTURE DIRECTIONS

115       None.
116

SEE ALSO

118       Section 2.14, File Descriptor Allocation, fcntl(), read(), write()
119
120       The Base Definitions volume of POSIX.1‐2017, <fcntl.h>, <unistd.h>
121
123       Portions  of  this text are reprinted and reproduced in electronic form
124       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
125       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
126       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
127       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
128       event of any discrepancy between this version and the original IEEE and
129       The  Open Group Standard, the original IEEE and The Open Group Standard
130       is the referee document. The original Standard can be  obtained  online
131       at http://www.opengroup.org/unix/online.html .
132
133       Any  typographical  or  formatting  errors that appear in this page are
134       most likely to have been introduced during the conversion of the source
135       files  to  man page format. To report such errors, see https://www.ker
136       nel.org/doc/man-pages/reporting_bugs.html .
137
138
139
140IEEE/The Open Group                  2017                             PIPE(3P)
Impressum