1PIPE(3P) POSIX Programmer's Manual PIPE(3P)
2
3
4
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
11
13 pipe — create an interprocess channel
14
16 #include <unistd.h>
17
18 int pipe(int fildes[2]);
19
21 The pipe() function shall create a pipe and place two file descriptors,
22 one each into the arguments fildes[0] and fildes[1], that refer to the
23 open file descriptions for the read and write ends of the pipe. Their
24 integer values shall be the two lowest available at the time of the
25 pipe() call. The O_NONBLOCK and FD_CLOEXEC flags shall be clear on both
26 file descriptors. (The fcntl() function can be used to set both these
27 flags.)
28
29 Data can be written to the file descriptor fildes[1] and read from the
30 file descriptor fildes[0]. A read on the file descriptor fildes[0]
31 shall access data written to the file descriptor fildes[1] on a first-
32 in-first-out basis. It is unspecified whether fildes[0] is also open
33 for writing and whether fildes[1] is also open for reading.
34
35 A process has the pipe open for reading (correspondingly writing) if it
36 has a file descriptor open that refers to the read end, fildes[0]
37 (write end, fildes[1]).
38
39 The pipe's user ID shall be set to the effective user ID of the calling
40 process.
41
42 The pipe's group ID shall be set to the effective group ID of the call‐
43 ing process.
44
45 Upon successful completion, pipe() shall mark for update the last data
46 access, last data modification, and last file status change timestamps
47 of the pipe.
48
50 Upon successful completion, 0 shall be returned; otherwise, −1 shall be
51 returned and errno set to indicate the error.
52
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
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 #include <stdlib.h>
73 #include <unistd.h>
74 ...
75
76 int fildes[2];
77 const int BSIZE = 100;
78 char buf[BSIZE];
79 ssize_t nbytes;
80 int status;
81
82 status = pipe(fildes);
83 if (status == −1 ) {
84 /* an error occurred */
85 ...
86 }
87
88 switch (fork()) {
89 case −1: /* Handle error */
90 break;
91
92 case 0: /* Child - reads from pipe */
93 close(fildes[1]); /* Write end is unused */
94 nbytes = read(fildes[0], buf, BSIZE); /* Get data from pipe */
95 /* At this point, a further read would see end of file ... */
96 close(fildes[0]); /* Finished with pipe */
97 exit(EXIT_SUCCESS);
98
99 default: /* Parent - writes to pipe */
100 close(fildes[0]); /* Read end is unused */
101 write(fildes[1], "Hello world\n", 12); /* Write data on pipe */
102 close(fildes[1]); /* Child will see EOF */
103 exit(EXIT_SUCCESS);
104 }
105
107 None.
108
110 The wording carefully avoids using the verb ``to open'' in order to
111 avoid any implication of use of open(); see also write().
112
114 None.
115
117 fcntl(), read(), write()
118
119 The Base Definitions volume of POSIX.1‐2008, <fcntl.h>, <unistd.h>
120
122 Portions of this text are reprinted and reproduced in electronic form
123 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
124 -- Portable Operating System Interface (POSIX), The Open Group Base
125 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
126 cal and Electronics Engineers, Inc and The Open Group. (This is
127 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) 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.unix.org/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 2013 PIPE(3P)