PIPE(3)                    POSIX Programmer's Manual                   PIPE(3)



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

NNAAMMEE
       pipe -- create an interprocess channel

SSYYNNOOPPSSIISS
       #include <unistd.h>

       int pipe(int _f_i_l_d_e_s[2]);

DDEESSCCRRIIPPTTIIOONN
       The _p_i_p_e() function shall create a pipe and place two file descriptors,
       one each into the arguments _f_i_l_d_e_s[0] and _f_i_l_d_e_s[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 _S_e_c_t_i_o_n _2_._1_4, _F_i_l_e
       _D_e_s_c_r_i_p_t_o_r _A_l_l_o_c_a_t_i_o_n.  The O_NONBLOCK and FD_CLOEXEC flags shall be
       clear on both file descriptors. (The _f_c_n_t_l() function can be used to
       set both these flags.)

       Data can be written to the file descriptor _f_i_l_d_e_s[1] and read from the
       file descriptor _f_i_l_d_e_s[0].  A read on the file descriptor _f_i_l_d_e_s[0]
       shall access data written to the file descriptor _f_i_l_d_e_s[1] on a first-
       in-first-out basis. It is unspecified whether _f_i_l_d_e_s[0] is also open
       for writing and whether _f_i_l_d_e_s[1] is also open for reading.

       A process has the pipe open for reading (correspondingly writing) if it
       has a file descriptor open that refers to the read end, _f_i_l_d_e_s[0]
       (write end, _f_i_l_d_e_s[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, _p_i_p_e() shall mark for update the last data
       access, last data modification, and last file status change timestamps
       of the pipe.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, 0 shall be returned; otherwise, -1 shall be
       returned and _e_r_r_n_o set to indicate the error, no file descriptors shall
       be allocated and the contents of _f_i_l_d_e_s shall be left unmodified.

EERRRROORRSS
       The _p_i_p_e() function shall fail if:

       EEMMFFIILLEE All, or all but one, of the file descriptors available to the
              process are currently open.

       EENNFFIILLEE The number of simultaneously open files in the system would
              exceed a system-imposed limit.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   UUssiinngg aa PPiippee ttoo PPaassss DDaattaa BBeettwweeeenn aa PPaarreenntt PPrroocceessss aanndd aa CChhiilldd PPrroocceessss
       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 _f_o_r_k() the two processes close the unused ends of the
       pipe before they commence transferring data.


           #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);
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       None.

RRAATTIIOONNAALLEE
       The wording carefully avoids using the verb ``to open'' in order to
       avoid any implication of use of _o_p_e_n(); see also _w_r_i_t_e().

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _S_e_c_t_i_o_n _2_._1_4, _F_i_l_e _D_e_s_c_r_i_p_t_o_r _A_l_l_o_c_a_t_i_o_n, _f_c_n_t_l(), _r_e_a_d(), _w_r_i_t_e()

       The Base Definitions volume of POSIX.1-2017, <<ffccnnttll..hh>>, <<uunniissttdd..hh>>

CCOOPPYYRRIIGGHHTT
       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 .



IEEE/The Open Group                  2017                              PIPE(3)
