PIPE(3POSIX)
π pipe(3POSIX) β create an interprocess channel π
okay bestie, welcome to the POSIX Programmer's Manual, specifically the page for pipe in section 3POSIX π«Ά
β οΈ real talk first: this page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not be implemented on Linux at all. not to be dramatic but. yeah π
β¨ PROLOG β¨
so like. this is the POSIX spec page. Linux might do its own thing. check the Linux page. or it might not exist on Linux. that's the whole prolog, we move π
πΈ NAME πΈ
pipe β create an interprocess channel π
π SYNOPSIS π
#include <unistd.h>
int pipe(int fildes[2]);
π DESCRIPTION π
okay so here's the tea β the pipe() function shall create a pipe and place two file descriptors β one each into the arguments fildes[0] and fildes[1] β that refer to the open file descriptions for the read and write ends of the pipe. iconic behavior honestly.
those file descriptors shall be allocated as described in Section 2.14, File Descriptor Allocation. and listen: the O_NONBLOCK and FD_CLOEXEC flags shall be clear on both file descriptors. (you can use the fcntl() function to set both of those flags yourself, iykyk β¨)
Data can be written to the file descriptor fildes[1] and read from the file descriptor fildes[0]. A read on fildes[0] shall access data written to fildes[1] on a first-in-first-out basis. no cutting the line bestie π«
It is unspecified whether fildes[0] is also open for writing and whether fildes[1] is also open for reading. so don't come for me about it, the spec said what it said π
A process has the pipe open for reading (correspondingly writing) if it has a file descriptor open that refers to the read end, fildes[0] (write end, fildes[1]). π¦
- The pipe's user ID shall be set to the effective user ID of the calling process.
- The pipe's group ID shall be set to the effective group ID of the calling process.
Upon successful completion, pipe() shall mark for update the last data access, last data modification, and last file status change timestamps of the pipe. timestamps getting their moment, we love to see it β
π RETURN VALUE π
Upon successful completion, 0 shall be returned. otherwise, -1 shall be returned and errno set to indicate the error β and in that case no file descriptors shall be allocated and the contents of fildes shall be left unmodified. so if it fails, it fails clean. periodt π
π ERRORS π
The pipe() function shall fail if:
EMFILEβ All, or all but one, of the file descriptors available to the process are currently open. you ran out bestie, it's giving overflowing πENFILEβ The number of simultaneously open files in the system would exceed a system-imposed limit. that's not even your fault, that's the whole system's problem π«Ά
The following sections are informative. (so like, helpful but not the law, mwah π)
π EXAMPLES π
Using a Pipe to Pass Data Between a Parent Process and a Child Process
The following example demonstrates the use of a pipe to transfer data between a parent process and a child process. Error handling is excluded, but otherwise this code demonstrates good practice when using pipes: after the fork() the two processes close the unused ends of the pipe before they commence transferring data. that's the move, that's the etiquette, do that β¨
#include <stdlib.h>
#include <unistd.h>
...
int fildes[2];
const int BSIZE = 100;
char buf[BSIZE];
ssize_t nbytes;
int status;
status = pipe(fildes);
if (status == -1 ) {
/* an error occurred */
...
}
switch (fork()) {
case -1: /* Handle error */
break;
case 0: /* Child - reads from pipe */
close(fildes[1]); /* Write end is unused */
nbytes = read(fildes[0], buf, BSIZE); /* Get data from pipe */
/* At this point, a further read would see end-of-file ... */
close(fildes[0]); /* Finished with pipe */
exit(EXIT_SUCCESS);
default: /* Parent - writes to pipe */
close(fildes[0]); /* Read end is unused */
write(fildes[1], "Hello world\n", 12); /* Write data on pipe */
close(fildes[1]); /* Child will see EOF */
exit(EXIT_SUCCESS);
}
π APPLICATION USAGE π
None. nothing to add, she's self-explanatory π
π§ RATIONALE π§
The wording carefully avoids using the verb ``to open'' in order to avoid any implication of use of open(); see also write(). the spec writers were being so precise with it and honestly? respect π«‘
β FUTURE DIRECTIONS β
None. the future is... nothing. no notes. π
π₯ SEE ALSO π₯
- Section 2.14, File Descriptor Allocation
fcntl()read()write()- The Base Definitions volume of POSIX.1β2017:
<fcntl.h>,<unistd.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page bestie, we ate and left no crumbs π β¨
this lil page came to you from IEEE/The Open Group, dated 2017, and it lives under the name PIPE(3POSIX) π byeeee, go close your unused pipe ends π«Ά
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms