{"id":4895,"date":"2022-12-20T18:36:56","date_gmt":"2022-12-20T21:36:56","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/pipe-man3p\/"},"modified":"2022-12-20T18:36:56","modified_gmt":"2022-12-20T21:36:56","slug":"pipe-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/pipe-man3p\/","title":{"rendered":"PIPE (man3p)"},"content":{"rendered":"<h1 align=\"center\">PIPE<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">pipe \u2014 create an interprocess channel<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <unistd.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int pipe(int <i>fildes<\/i>[2]);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pipe<\/i>() function shall create a pipe and place two file descriptors, one each into the arguments <i>fildes<\/i>[0] and <i>fildes<\/i>[1], that refer to the open file descriptions for the read and write ends of the pipe. The file descriptors shall be allocated as described in <i>Section 2.14<\/i>, <i>File Descriptor Allocation<\/i>. The O_NONBLOCK and FD_CLOEXEC flags shall be clear on both file descriptors. (The <i>fcntl<\/i>() function can be used to set both these flags.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Data can be written to the file descriptor <i>fildes<\/i>[1] and read from the file descriptor <i>fildes<\/i>[0]. A read on the file descriptor <i>fildes<\/i>[0] shall access data written to the file descriptor <i>fildes<\/i>[1] on a first-in-first-out basis. It is unspecified whether <i>fildes<\/i>[0] is also open for writing and whether <i>fildes<\/i>[1] is also open for reading.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A process has the pipe open for reading (correspondingly writing) if it has a file descriptor open that refers to the read end, <i>fildes<\/i>[0] (write end, <i>fildes<\/i>[1]).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The pipe\u2019s user ID shall be set to the effective user ID of the calling process.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The pipe\u2019s group ID shall be set to the effective group ID of the calling process.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, <i>pipe<\/i>() shall mark for update the last data access, last data modification, and last file status change timestamps of the pipe.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, 0 shall be returned; otherwise, \u22121 shall be returned and <i>errno<\/i> set to indicate the error, no file descriptors shall be allocated and the contents of <i>fildes<\/i> shall be left unmodified.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pipe<\/i>() function shall fail if:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EMFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>All, or all but one, of the file descriptors available to the process are currently open.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The number of simultaneously open files in the system would exceed a system-imposed limit.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i><b>Using a Pipe to Pass Data Between a Parent Process and a Child Process<\/b><\/i> <br \/> 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 <i>fork<\/i>() the two processes close the unused ends of the pipe before they commence transferring data.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <stdlib.h> <br \/> #include <unistd.h> <br \/> &#8230;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int fildes[2]; <br \/> const int BSIZE = 100; <br \/> char buf[BSIZE]; <br \/> ssize_t nbytes; <br \/> int status;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">status = pipe(fildes); <br \/> if (status == -1 ) { <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var an error occurred bodies\/ usr\/ <br \/> &#8230; <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">switch (fork()) { <br \/> case -1: \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Handle error bodies\/ usr\/ <br \/> break;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">case 0: \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Child &#8211; reads from pipe bodies\/ usr\/ <br \/> close(fildes[1]); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Write end is unused bodies\/ usr\/ <br \/> nbytes = read(fildes[0], buf, BSIZE); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Get data from pipe bodies\/ usr\/ <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var At this point, a further read would see end-of-file &#8230; bodies\/ usr\/ <br \/> close(fildes[0]); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Finished with pipe bodies\/ usr\/ <br \/> exit(EXIT_SUCCESS);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">default: \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Parent &#8211; writes to pipe bodies\/ usr\/ <br \/> close(fildes[0]); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Read end is unused bodies\/ usr\/ <br \/> write(fildes[1], &#8220;Hello worldn&#8221;, 12); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Write data on pipe bodies\/ usr\/ <br \/> close(fildes[1]); \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Child will see EOF bodies\/ usr\/ <br \/> exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The wording carefully avoids using the verb \u2018\u2018to open\u2019\u2019 in order to avoid any implication of use of <i>open<\/i>(); see also <i>write<\/i>().<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>Section 2.14<\/i>, <i>File Descriptor Allocation<\/i>, <i>fcntl<\/i>(), <i>read<\/i>(), <i>write<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><fcntl.h><\/b>, <b><unistd.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; 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 .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  pipe \u2014 create an interprocess channel <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[1594,1036],"class_list":["post-4895","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-man3p","tag-pipe"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4895"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4895\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}