1.\" $NetBSD: pipe.2,v 1.33 2024/11/01 18:48:17 nia Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)pipe.2 8.1 (Berkeley) 6/4/93 31.\" 32.Dd November 1, 2024 33.Dt PIPE 2 34.Os 35.Sh NAME 36.Nm pipe , 37.Nm pipe2 38.Nd create descriptor pair for interprocess communication 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In unistd.h 43.Ft int 44.Fn pipe "int fildes[2]" 45.In fcntl.h 46.In unistd.h 47.Ft int 48.Fn pipe2 "int fildes[2]" "int flags" 49.Sh DESCRIPTION 50The 51.Fn pipe 52function 53creates a 54.Em pipe , 55which is an object allowing 56unidirectional data flow, 57and allocates a pair of file descriptors. 58The first descriptor connects to the 59.Em read end 60of the pipe, 61and the second connects to the 62.Em write end , 63so that data written to 64.Fa fildes[1] 65appears on (i.e., can be read from) 66.Fa fildes[0] . 67This allows the output of one program to be 68sent 69to another program: 70the source's standard output is set up to be 71the write end of the pipe, 72and the sink's standard input is set up to be 73the read end of the pipe. 74The pipe itself persists until all its associated descriptors are 75closed. 76.Pp 77A pipe whose read or write end has been closed is considered 78.Em widowed . 79Writing on such a pipe causes the writing process to receive 80a 81.Dv SIGPIPE 82signal. 83Widowing a pipe is the only way to deliver end-of-file to a reader: 84after the reader consumes any buffered data, reading a widowed pipe 85returns a zero count. 86.Pp 87The 88.Fn pipe2 89function 90behaves exactly like 91.Fn pipe 92only it allows extra 93.Fa flags 94to be set on the returned file descriptor. 95The following flags are valid: 96.Bl -tag -width O_NONBLOCK -offset indent 97.It Dv O_CLOEXEC 98Set the 99.Dq close-on-exec 100property. 101.It Dv O_NONBLOCK 102Sets non-blocking I/O. 103.It Dv O_NOSIGPIPE 104Return 105.Er EPIPE 106instead of raising 107.Dv SIGPIPE . 108.El 109.Sh RETURN VALUES 110On successful creation of the pipe, zero is returned. 111Otherwise, a value of \-1 is returned and the variable 112.Va errno 113set to indicate the 114error. 115.Sh ERRORS 116The 117.Fn pipe 118and 119.Fn pipe2 120calls will fail if: 121.Bl -tag -width Er 122.It Bq Er EFAULT 123The 124.Fa fildes 125buffer is in an invalid area of the process's address space. 126The reliable detection of this error cannot be guaranteed; when not 127detected, a signal may be delivered to the process, indicating an 128address violation. 129.It Bq Er EMFILE 130Too many descriptors are active. 131.It Bq Er ENFILE 132The system file table is full. 133.It Bq Er ENOMEM 134Not enough kernel memory to establish a pipe. 135.El 136.Pp 137.Fn pipe2 138will also fail if: 139.Bl -tag -width Er 140.It Bq Er EINVAL 141.Fa flags 142contains an invalid value. 143.El 144.Sh SEE ALSO 145.Xr sh 1 , 146.Xr fork 2 , 147.Xr read 2 , 148.Xr socketpair 2 , 149.Xr write 2 150.Sh STANDARDS 151The 152.Fn pipe 153function conforms to 154.St -p1003.1-90 . 155The 156.Fn pipe2 157function conforms to 158.St -p1003.1-2024 . 159.Sh HISTORY 160A 161.Fn pipe 162function call appeared in 163.At v3 . 164Since 165.At v4 , 166it allocates two distinct file descriptors. 167.Pp 168The 169.Fn pipe2 170function is inspired from Linux and appeared in 171.Nx 6.0 . 172