401 lines
11 KiB
HTML
401 lines
11 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of PIPE</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>PIPE</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
pipe, pipe2 - create pipe
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
/* On Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64; see NOTES */
|
|
<B>struct fd_pair {</B>
|
|
<B> long fd[2];</B>
|
|
<B>};</B>
|
|
<B>struct fd_pair pipe();</B>
|
|
|
|
/* On all other architectures */
|
|
<B>int pipe(int </B><I>pipefd</I><B>[2]);</B>
|
|
|
|
<B>#define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
|
|
<B>#include <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>></B> /* Obtain O_* constant definitions */
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<B>int pipe2(int </B><I>pipefd</I><B>[2], int </B><I>flags</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<B>pipe</B>()
|
|
|
|
creates a pipe, a unidirectional data channel that
|
|
can be used for interprocess communication.
|
|
The array
|
|
<I>pipefd</I>
|
|
|
|
is used to return two file descriptors referring to the ends of the pipe.
|
|
<I>pipefd[0]</I>
|
|
|
|
refers to the read end of the pipe.
|
|
<I>pipefd[1]</I>
|
|
|
|
refers to the write end of the pipe.
|
|
Data written to the write end of the pipe is buffered by the kernel
|
|
until it is read from the read end of the pipe.
|
|
For further details, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pipe">pipe</A></B>(7).
|
|
|
|
<P>
|
|
|
|
If
|
|
<I>flags</I>
|
|
|
|
is 0, then
|
|
<B>pipe2</B>()
|
|
|
|
is the same as
|
|
<B>pipe</B>().
|
|
|
|
The following values can be bitwise ORed in
|
|
<I>flags</I>
|
|
|
|
to obtain different behavior:
|
|
<DL COMPACT>
|
|
<DT id="1"><B>O_CLOEXEC</B>
|
|
|
|
<DD>
|
|
Set the close-on-exec
|
|
(<B>FD_CLOEXEC</B>)
|
|
|
|
flag on the two new file descriptors.
|
|
See the description of the same flag in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
|
|
|
|
for reasons why this may be useful.
|
|
<DT id="2"><B>O_DIRECT</B> (since Linux 3.4)
|
|
|
|
<DD>
|
|
|
|
Create a pipe that performs I/O in "packet" mode.
|
|
Each
|
|
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)
|
|
|
|
to the pipe is dealt with as a separate packet, and
|
|
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)s
|
|
|
|
from the pipe will read one packet at a time.
|
|
Note the following points:
|
|
<DL COMPACT><DT id="3"><DD>
|
|
<DL COMPACT>
|
|
<DT id="4">*<DD>
|
|
Writes of greater than
|
|
<B>PIPE_BUF</B>
|
|
|
|
bytes (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pipe">pipe</A></B>(7))
|
|
|
|
will be split into multiple packets.
|
|
The constant
|
|
<B>PIPE_BUF</B>
|
|
|
|
is defined in
|
|
<I><<A HREF="file:///usr/include/limits.h">limits.h</A>></I>.
|
|
|
|
<DT id="5">*<DD>
|
|
If a
|
|
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
|
|
|
|
specifies a buffer size that is smaller than the next packet,
|
|
then the requested number of bytes are read,
|
|
and the excess bytes in the packet are discarded.
|
|
Specifying a buffer size of
|
|
<B>PIPE_BUF</B>
|
|
|
|
will be sufficient to read the largest possible packets
|
|
(see the previous point).
|
|
<DT id="6">*<DD>
|
|
Zero-length packets are not supported.
|
|
(A
|
|
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
|
|
|
|
that specifies a buffer size of zero is a no-op, and returns 0.)
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="7"><DD>
|
|
Older kernels that do not support this flag will indicate this via an
|
|
<B>EINVAL</B>
|
|
|
|
error.
|
|
<DT id="8"><DD>
|
|
Since Linux 4.5,
|
|
|
|
|
|
it is possible to change the
|
|
<B>O_DIRECT</B>
|
|
|
|
setting of a pipe file descriptor using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2).
|
|
|
|
<DT id="9"><B>O_NONBLOCK</B>
|
|
|
|
<DD>
|
|
Set the
|
|
<B>O_NONBLOCK</B>
|
|
|
|
file status flag on the open file descriptions
|
|
referred to by the new file descriptors.
|
|
Using this flag saves extra calls to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
|
|
|
|
to achieve the same result.
|
|
</DL>
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, zero is returned.
|
|
On error, -1 is returned,
|
|
<I>errno</I>
|
|
|
|
is set appropriately, and
|
|
<I>pipefd</I>
|
|
|
|
is left unchanged.
|
|
<P>
|
|
|
|
On Linux (and other systems),
|
|
<B>pipe</B>()
|
|
|
|
does not modify
|
|
<I>pipefd</I>
|
|
|
|
on failure.
|
|
A requirement standardizing this behavior was added in POSIX.1-2016.
|
|
|
|
The Linux-specific
|
|
<B>pipe2</B>()
|
|
|
|
system call
|
|
likewise does not modify
|
|
<I>pipefd</I>
|
|
|
|
on failure.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="10"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
<I>pipefd</I>
|
|
|
|
is not valid.
|
|
<DT id="11"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
(<B>pipe2</B>())
|
|
|
|
Invalid value in
|
|
<I>flags</I>.
|
|
|
|
<DT id="12"><B>EMFILE</B>
|
|
|
|
<DD>
|
|
The per-process limit on the number of open file descriptors has been reached.
|
|
<DT id="13"><B>ENFILE</B>
|
|
|
|
<DD>
|
|
The system-wide limit on the total number of open files has been reached.
|
|
<DT id="14"><B>ENFILE</B>
|
|
|
|
<DD>
|
|
The user hard limit on memory that can be allocated for pipes
|
|
has been reached and the caller is not privileged; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pipe">pipe</A></B>(7).
|
|
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>pipe2</B>()
|
|
|
|
was added to Linux in version 2.6.27;
|
|
glibc support is available starting with
|
|
version 2.9.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
|
|
|
|
The SystemV ABI on some architectures allows the use of more than one register
|
|
for returning multiple values; several architectures
|
|
(namely, Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64)
|
|
(ab)use this feature in order to implement the
|
|
<B>pipe</B>()
|
|
|
|
system call in a functional manner:
|
|
the call doesn't take any arguments and returns
|
|
a pair of file descriptors as the return value on success.
|
|
The glibc
|
|
<B>pipe</B>()
|
|
|
|
wrapper function transparently deals with this.
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A></B>(2)
|
|
|
|
for information regarding registers used for storing second file descriptor.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>pipe</B>():
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<P>
|
|
|
|
<B>pipe2</B>()
|
|
|
|
is Linux-specific.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
|
|
The following program creates a pipe, and then
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)s
|
|
|
|
to create a child process;
|
|
the child inherits a duplicate set of file
|
|
descriptors that refer to the same pipe.
|
|
After the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
each process closes the file descriptors that it doesn't need for the pipe
|
|
(see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pipe">pipe</A></B>(7)).
|
|
|
|
The parent then writes the string contained in the program's
|
|
command-line argument to the pipe,
|
|
and the child reads this string a byte at a time from the pipe
|
|
and echoes it on standard output.
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/wait.h">sys/wait.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int pipefd[2];
|
|
<BR> pid_t cpid;
|
|
<BR> char buf;
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s <string>\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (pipe(pipefd) == -1) {
|
|
<BR> perror("pipe");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> cpid = fork();
|
|
<BR> if (cpid == -1) {
|
|
<BR> perror("fork");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (cpid == 0) { /* Child reads from pipe */
|
|
<BR> close(pipefd[1]); /* Close unused write end */
|
|
<P>
|
|
<BR> while (read(pipefd[0], &buf, 1) > 0)
|
|
<BR> write(STDOUT_FILENO, &buf, 1);
|
|
<P>
|
|
<BR> write(STDOUT_FILENO, "\n", 1);
|
|
<BR> close(pipefd[0]);
|
|
<BR> _exit(EXIT_SUCCESS);
|
|
<P>
|
|
<BR> } else { /* Parent writes argv[1] to pipe */
|
|
<BR> close(pipefd[0]); /* Close unused read end */
|
|
<BR> write(pipefd[1], argv[1], strlen(argv[1]));
|
|
<BR> close(pipefd[1]); /* Reader will see EOF */
|
|
<BR> wait(NULL); /* Wait for child */
|
|
<BR> exit(EXIT_SUCCESS);
|
|
<BR> }
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+socketpair">socketpair</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+tee">tee</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+vmsplice">vmsplice</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+popen">popen</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pipe">pipe</A></B>(7)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="15"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="16"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="17"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="18"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="19"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="20"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="21"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="22"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="23"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="24"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="25"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="26"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:33 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|