Blame manual/pipe.texi

Packit 6c4009
@node Pipes and FIFOs, Sockets, File System Interface, Top
Packit 6c4009
@c %MENU% A simple interprocess communication mechanism
Packit 6c4009
@chapter Pipes and FIFOs
Packit 6c4009
Packit 6c4009
@cindex pipe
Packit 6c4009
A @dfn{pipe} is a mechanism for interprocess communication; data written
Packit 6c4009
to the pipe by one process can be read by another process.  The data is
Packit 6c4009
handled in a first-in, first-out (FIFO) order.  The pipe has no name; it
Packit 6c4009
is created for one use and both ends must be inherited from the single
Packit 6c4009
process which created the pipe.
Packit 6c4009
Packit 6c4009
@cindex FIFO special file
Packit 6c4009
A @dfn{FIFO special file} is similar to a pipe, but instead of being an
Packit 6c4009
anonymous, temporary connection, a FIFO has a name or names like any
Packit 6c4009
other file.  Processes open the FIFO by name in order to communicate
Packit 6c4009
through it.
Packit 6c4009
Packit 6c4009
A pipe or FIFO has to be open at both ends simultaneously.  If you read
Packit 6c4009
from a pipe or FIFO file that doesn't have any processes writing to it
Packit 6c4009
(perhaps because they have all closed the file, or exited), the read
Packit 6c4009
returns end-of-file.  Writing to a pipe or FIFO that doesn't have a
Packit 6c4009
reading process is treated as an error condition; it generates a
Packit 6c4009
@code{SIGPIPE} signal, and fails with error code @code{EPIPE} if the
Packit 6c4009
signal is handled or blocked.
Packit 6c4009
Packit 6c4009
Neither pipes nor FIFO special files allow file positioning.  Both
Packit 6c4009
reading and writing operations happen sequentially; reading from the
Packit 6c4009
beginning of the file and writing at the end.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Creating a Pipe::             Making a pipe with the @code{pipe} function.
Packit 6c4009
* Pipe to a Subprocess::        Using a pipe to communicate with a
Packit 6c4009
				 child process.
Packit 6c4009
* FIFO Special Files::          Making a FIFO special file.
Packit 6c4009
* Pipe Atomicity::		When pipe (or FIFO) I/O is atomic.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Creating a Pipe
Packit 6c4009
@section Creating a Pipe
Packit 6c4009
@cindex creating a pipe
Packit 6c4009
@cindex opening a pipe
Packit 6c4009
@cindex interprocess communication, with pipes
Packit 6c4009
Packit 6c4009
The primitive for creating a pipe is the @code{pipe} function.  This
Packit 6c4009
creates both the reading and writing ends of the pipe.  It is not very
Packit 6c4009
useful for a single process to use a pipe to talk to itself.  In typical
Packit 6c4009
use, a process creates a pipe just before it forks one or more child
Packit 6c4009
processes (@pxref{Creating a Process}).  The pipe is then used for
Packit 6c4009
communication either between the parent or child processes, or between
Packit 6c4009
two sibling processes.
Packit 6c4009
Packit 6c4009
The @code{pipe} function is declared in the header file
Packit 6c4009
@file{unistd.h}.
Packit 6c4009
@pindex unistd.h
Packit 6c4009
Packit 6c4009
@deftypefun int pipe (int @var{filedes}@t{[2]})
Packit 6c4009
@standards{POSIX.1, unistd.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
Packit 6c4009
@c On Linux, syscall pipe2.  On HURD, call socketpair.
Packit 6c4009
The @code{pipe} function creates a pipe and puts the file descriptors
Packit 6c4009
for the reading and writing ends of the pipe (respectively) into
Packit 6c4009
@code{@var{filedes}[0]} and @code{@var{filedes}[1]}.
Packit 6c4009
Packit 6c4009
An easy way to remember that the input end comes first is that file
Packit 6c4009
descriptor @code{0} is standard input, and file descriptor @code{1} is
Packit 6c4009
standard output.
Packit 6c4009
Packit 6c4009
If successful, @code{pipe} returns a value of @code{0}.  On failure,
Packit 6c4009
@code{-1} is returned.  The following @code{errno} error conditions are
Packit 6c4009
defined for this function:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EMFILE
Packit 6c4009
The process has too many files open.
Packit 6c4009
Packit 6c4009
@item ENFILE
Packit 6c4009
There are too many open files in the entire system.  @xref{Error Codes},
Packit 6c4009
for more information about @code{ENFILE}.  This error never occurs on
Packit 6c4009
@gnuhurdsystems{}.
Packit 6c4009
@end table
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Here is an example of a simple program that creates a pipe.  This program
Packit 6c4009
uses the @code{fork} function (@pxref{Creating a Process}) to create
Packit 6c4009
a child process.  The parent process writes data to the pipe, which is
Packit 6c4009
read by the child process.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include pipe.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Pipe to a Subprocess
Packit 6c4009
@section Pipe to a Subprocess
Packit 6c4009
@cindex creating a pipe to a subprocess
Packit 6c4009
@cindex pipe to a subprocess
Packit 6c4009
@cindex filtering i/o through subprocess
Packit 6c4009
Packit 6c4009
A common use of pipes is to send data to or receive data from a program
Packit 6c4009
being run as a subprocess.  One way of doing this is by using a combination of
Packit 6c4009
@code{pipe} (to create the pipe), @code{fork} (to create the subprocess),
Packit 6c4009
@code{dup2} (to force the subprocess to use the pipe as its standard input
Packit 6c4009
or output channel), and @code{exec} (to execute the new program).  Or,
Packit 6c4009
you can use @code{popen} and @code{pclose}.
Packit 6c4009
Packit 6c4009
The advantage of using @code{popen} and @code{pclose} is that the
Packit 6c4009
interface is much simpler and easier to use.  But it doesn't offer as
Packit 6c4009
much flexibility as using the low-level functions directly.
Packit 6c4009
Packit 6c4009
@deftypefun {FILE *} popen (const char *@var{command}, const char *@var{mode})
Packit 6c4009
@standards{POSIX.2, stdio.h}
Packit 6c4009
@standards{SVID, stdio.h}
Packit 6c4009
@standards{BSD, stdio.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
@c popen @ascuheap @asucorrupt @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c  malloc dup @ascuheap @acsmem
Packit 6c4009
@c  _IO_init ok
Packit 6c4009
@c   _IO_no_init ok
Packit 6c4009
@c    _IO_old_init ok
Packit 6c4009
@c     _IO_lock_init ok
Packit 6c4009
@c  _IO_new_file_init @asucorrupt @acucorrupt @aculock @acsfd
Packit 6c4009
@c   _IO_link_in @asucorrupt @acucorrupt @aculock @acsfd
Packit 6c4009
@c     the linked list is guarded by a recursive lock;
Packit 6c4009
@c     it may get corrupted with async signals and cancellation
Packit 6c4009
@c    _IO_lock_lock dup @aculock
Packit 6c4009
@c    _IO_flockfile dup @aculock
Packit 6c4009
@c    _IO_funlockfile dup @aculock
Packit 6c4009
@c    _IO_lock_unlock dup @aculock
Packit 6c4009
@c  _IO_new_proc_open @asucorrupt @acucorrupt @aculock @acsfd
Packit 6c4009
@c    the linked list is guarded by a recursive lock;
Packit 6c4009
 @c   it may get corrupted with async signals and cancellation
Packit 6c4009
@c   _IO_file_is_open ok
Packit 6c4009
@c   pipe2 dup @acsfd
Packit 6c4009
@c   pipe dup @acsfd
Packit 6c4009
@c   _IO_fork=fork @aculock
Packit 6c4009
@c   _IO_close=close_not_cancel dup @acsfd
Packit 6c4009
@c   fcntl dup ok
Packit 6c4009
@c   _IO_lock_lock @aculock
Packit 6c4009
@c   _IO_lock_unlock @aculock
Packit 6c4009
@c   _IO_mask_flags ok [no @mtasurace:stream, nearly but sufficiently exclusive access]
Packit 6c4009
@c  _IO_un_link @asucorrupt @acucorrupt @aculock @acsfd
Packit 6c4009
@c    the linked list is guarded by a recursive lock;
Packit 6c4009
@c    it may get corrupted with async signals and cancellation
Packit 6c4009
@c   _IO_lock_lock dup @aculock
Packit 6c4009
@c   _IO_flockfile dup @aculock
Packit 6c4009
@c   _IO_funlockfile dup @aculock
Packit 6c4009
@c   _IO_lock_unlock dup @aculock
Packit 6c4009
@c  free dup @ascuheap @acsmem
Packit 6c4009
The @code{popen} function is closely related to the @code{system}
Packit 6c4009
function; see @ref{Running a Command}.  It executes the shell command
Packit 6c4009
@var{command} as a subprocess.  However, instead of waiting for the
Packit 6c4009
command to complete, it creates a pipe to the subprocess and returns a
Packit 6c4009
stream that corresponds to that pipe.
Packit 6c4009
Packit 6c4009
If you specify a @var{mode} argument of @code{"r"}, you can read from the
Packit 6c4009
stream to retrieve data from the standard output channel of the subprocess.
Packit 6c4009
The subprocess inherits its standard input channel from the parent process.
Packit 6c4009
Packit 6c4009
Similarly, if you specify a @var{mode} argument of @code{"w"}, you can
Packit 6c4009
write to the stream to send data to the standard input channel of the
Packit 6c4009
subprocess.  The subprocess inherits its standard output channel from
Packit 6c4009
the parent process.
Packit 6c4009
Packit 6c4009
In the event of an error @code{popen} returns a null pointer.  This
Packit 6c4009
might happen if the pipe or stream cannot be created, if the subprocess
Packit 6c4009
cannot be forked, or if the program cannot be executed.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int pclose (FILE *@var{stream})
Packit 6c4009
@standards{POSIX.2, stdio.h}
Packit 6c4009
@standards{SVID, stdio.h}
Packit 6c4009
@standards{BSD, stdio.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @ascuplugin{} @asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
@c Although the stream cannot be used after the call, even in case of
Packit 6c4009
@c async cancellation, because the stream must not be used after pclose
Packit 6c4009
@c is called, other stdio linked lists and their locks may be left in
Packit 6c4009
@c corrupt states; that's where the corrupt and lock annotations come
Packit 6c4009
@c from.
Packit 6c4009
@c
Packit 6c4009
@c pclose @ascuheap @ascuplugin @asucorrupt @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c  _IO_new_fclose @ascuheap @ascuplugin @asucorrupt @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   _IO_un_link dup @asucorrupt @acucorrupt @aculock @acsfd
Packit 6c4009
@c   _IO_acquire_lock dup @aculock
Packit 6c4009
@c    _IO_flockfile dup @aculock
Packit 6c4009
@c   _IO_file_close_it @ascuheap @ascuplugin @asucorrupt @aculock @acucorrupt @acsfd @acsmem
Packit 6c4009
@c    _IO_file_is_open dup ok
Packit 6c4009
@c    _IO_do_flush @asucorrupt @ascuplugin @acucorrupt
Packit 6c4009
@c     _IO_do_write @asucorrupt @acucorrupt
Packit 6c4009
@c      new_do_write @asucorrupt @acucorrupt
Packit 6c4009
@c       _IO_SYSSEEK ok
Packit 6c4009
@c        lseek64 dup ok
Packit 6c4009
@c       _IO_SYSWRITE ok
Packit 6c4009
@c        write_not_cancel dup ok
Packit 6c4009
@c        write dup ok
Packit 6c4009
@c       _IO_adjust_column ok
Packit 6c4009
@c       _IO_setg dup @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
Packit 6c4009
@c     _IO_wdo_write @asucorrupt @ascuplugin @acucorrupt
Packit 6c4009
@c      _IO_new_do_write=_IO_do_write dup @asucorrupt @acucorrupt
Packit 6c4009
@c      *cc->__codecvt_do_out @ascuplugin
Packit 6c4009
@c      _IO_wsetg dup @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
Packit 6c4009
@c    _IO_unsave_markers @ascuheap @asucorrupt @acucorrupt @acsmem
Packit 6c4009
@c     _IO_have_backup dup ok
Packit 6c4009
@c     _IO_free_backup_area dup @ascuheap @asucorrupt @acucorrupt @acsmem
Packit 6c4009
@c    _IO_SYSCLOSE @aculock @acucorrupt @acsfd
Packit 6c4009
@c     _IO_lock_lock dup @aculock
Packit 6c4009
@c     _IO_close=close_not_cancel dup @acsfd
Packit 6c4009
@c     _IO_lock_unlock dup @aculock
Packit 6c4009
@c     _IO_waitpid=waitpid_not_cancel dup ok
Packit 6c4009
@c    _IO_have_wbackup ok
Packit 6c4009
@c    _IO_free_wbackup_area @ascuheap @asucorrupt @acucorrupt @acsmem
Packit 6c4009
@c     _IO_in_backup dup ok
Packit 6c4009
@c     _IO_switch_to_main_wget_area @asucorrupt @acucorrupt
Packit 6c4009
@c     free dup @ascuheap @acsmem
Packit 6c4009
@c    _IO_wsetb @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
Packit 6c4009
@c    _IO_wsetg @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
Packit 6c4009
@c    _IO_wsetp @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
Packit 6c4009
@c    _IO_setb @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
Packit 6c4009
@c    _IO_setg @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
Packit 6c4009
@c    _IO_setp @asucorrupt @acucorrupt [no @mtasurace:stream, locked]
Packit 6c4009
@c    _IO_un_link dup @asucorrupt @acucorrupt @aculock @acsfd
Packit 6c4009
@c   _IO_release_lock dup @aculock
Packit 6c4009
@c    _IO_funlockfile dup @aculock
Packit 6c4009
@c   _IO_FINISH @ascuheap @ascuplugin @asucorrupt @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c    _IO_new_file_finish @ascuheap @ascuplugin @asucorrupt @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c     _IO_file_is_open dup ok
Packit 6c4009
@c     _IO_do_flush dup @ascuplugin @asucorrupt @acucorrupt
Packit 6c4009
@c     _IO_SYSCLOSE dup @aculock @acucorrupt @acsfd
Packit 6c4009
@c     _IO_default_finish @ascuheap @asucorrupt @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c      FREE_BUF @acsmem
Packit 6c4009
@c       munmap dup @acsmem
Packit 6c4009
@c      free dup @ascuheap @acsmem
Packit 6c4009
@c      _IO_un_link dup @asucorrupt @acucorrupt @aculock @acsfd
Packit 6c4009
@c      _IO_lock_fini ok
Packit 6c4009
@c       libc_lock_fini_recursive ok
Packit 6c4009
@c   libc_lock_lock dup @asulock @aculock
Packit 6c4009
@c   gconv_release_step ok
Packit 6c4009
@c   libc_lock_unlock dup @asulock @aculock
Packit 6c4009
@c   _IO_have_backup ok
Packit 6c4009
@c   _IO_free_backup_area @ascuheap @asucorrupt @acucorrupt @acsmem
Packit 6c4009
@c    _IO_in_backup ok
Packit 6c4009
@c    _IO_switch_to_main_get_area @asucorrupt @acucorrupt
Packit 6c4009
@c    free dup @ascuheap @acsmem
Packit 6c4009
@c   free dup @ascuheap @acsmem
Packit 6c4009
The @code{pclose} function is used to close a stream created by @code{popen}.
Packit 6c4009
It waits for the child process to terminate and returns its status value,
Packit 6c4009
as for the @code{system} function.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Here is an example showing how to use @code{popen} and @code{pclose} to
Packit 6c4009
filter output through another program, in this case the paging program
Packit 6c4009
@code{more}.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include popen.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node FIFO Special Files
Packit 6c4009
@section FIFO Special Files
Packit 6c4009
@cindex creating a FIFO special file
Packit 6c4009
@cindex interprocess communication, with FIFO
Packit 6c4009
Packit 6c4009
A FIFO special file is similar to a pipe, except that it is created in a
Packit 6c4009
different way.  Instead of being an anonymous communications channel, a
Packit 6c4009
FIFO special file is entered into the file system by calling
Packit 6c4009
@code{mkfifo}.
Packit 6c4009
Packit 6c4009
Once you have created a FIFO special file in this way, any process can
Packit 6c4009
open it for reading or writing, in the same way as an ordinary file.
Packit 6c4009
However, it has to be open at both ends simultaneously before you can
Packit 6c4009
proceed to do any input or output operations on it.  Opening a FIFO for
Packit 6c4009
reading normally blocks until some other process opens the same FIFO for
Packit 6c4009
writing, and vice versa.
Packit 6c4009
Packit 6c4009
The @code{mkfifo} function is declared in the header file
Packit 6c4009
@file{sys/stat.h}.
Packit 6c4009
@pindex sys/stat.h
Packit 6c4009
Packit 6c4009
@deftypefun int mkfifo (const char *@var{filename}, mode_t @var{mode})
Packit 6c4009
@standards{POSIX.1, sys/stat.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c On generic Posix, calls xmknod.
Packit 6c4009
The @code{mkfifo} function makes a FIFO special file with name
Packit 6c4009
@var{filename}.  The @var{mode} argument is used to set the file's
Packit 6c4009
permissions; see @ref{Setting Permissions}.
Packit 6c4009
Packit 6c4009
The normal, successful return value from @code{mkfifo} is @code{0}.  In
Packit 6c4009
the case of an error, @code{-1} is returned.  In addition to the usual
Packit 6c4009
file name errors (@pxref{File Name Errors}), the following
Packit 6c4009
@code{errno} error conditions are defined for this function:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EEXIST
Packit 6c4009
The named file already exists.
Packit 6c4009
Packit 6c4009
@item ENOSPC
Packit 6c4009
The directory or file system cannot be extended.
Packit 6c4009
Packit 6c4009
@item EROFS
Packit 6c4009
The directory that would contain the file resides on a read-only file
Packit 6c4009
system.
Packit 6c4009
@end table
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Pipe Atomicity
Packit 6c4009
@section Atomicity of Pipe I/O
Packit 6c4009
Packit 6c4009
Reading or writing pipe data is @dfn{atomic} if the size of data written
Packit 6c4009
is not greater than @code{PIPE_BUF}.  This means that the data transfer
Packit 6c4009
seems to be an instantaneous unit, in that nothing else in the system
Packit 6c4009
can observe a state in which it is partially complete.  Atomic I/O may
Packit 6c4009
not begin right away (it may need to wait for buffer space or for data),
Packit 6c4009
but once it does begin it finishes immediately.
Packit 6c4009
Packit 6c4009
Reading or writing a larger amount of data may not be atomic; for
Packit 6c4009
example, output data from other processes sharing the descriptor may be
Packit 6c4009
interspersed.  Also, once @code{PIPE_BUF} characters have been written,
Packit 6c4009
further writes will block until some characters are read.
Packit 6c4009
Packit 6c4009
@xref{Limits for Files}, for information about the @code{PIPE_BUF}
Packit 6c4009
parameter.