⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pipe.texi

📁 一个C源代码分析器
💻 TEXI
字号:
@node Pipes and FIFOs, Sockets, File System Interface, Top@chapter Pipes and FIFOs@cindex pipeA @dfn{pipe} is a mechanism for interprocess communication; data writtento the pipe by one process can be read by another process.  The data ishandled in a first-in, first-out (FIFO) order.  The pipe has no name; itis created for one use and both ends must be inherited from the singleprocess which created the pipe.@cindex FIFO special fileA @dfn{FIFO special file} is similar to a pipe, but instead of being ananonymous, temporary connection, a FIFO has a name or names like anyother file.  Processes open the FIFO by name in order to communicatethrough it.A pipe or FIFO has to be open at both ends simultaneously.  If you readfrom a pipe or FIFO file that doesn't have any processes writing to it(perhaps because they have all closed the file, or exited), the readreturns end-of-file.  Writing to a pipe or FIFO that doesn't have areading process is treated as an error condition; it generates a@code{SIGPIPE} signal, and fails with error code @code{EPIPE} if thesignal is handled or blocked.Neither pipes nor FIFO special files allow file positioning.  Bothreading and writing operations happen sequentially; reading from thebeginning of the file and writing at the end.@menu* Creating a Pipe::             Making a pipe with the @code{pipe} function.* Pipe to a Subprocess::        Using a pipe to communicate with a				 child process.* FIFO Special Files::          Making a FIFO special file.* Pipe Atomicity::		When pipe (or FIFO) I/O is atomic.@end menu@node Creating a Pipe@section Creating a Pipe@cindex creating a pipe@cindex opening a pipe@cindex interprocess communication, with pipesThe primitive for creating a pipe is the @code{pipe} function.  Thiscreates both the reading and writing ends of the pipe.  It is not veryuseful for a single process to use a pipe to talk to itself.  In typicaluse, a process creates a pipe just before it forks one or more childprocesses (@pxref{Creating a Process}).  The pipe is then used forcommunication either between the parent or child processes, or betweentwo sibling processes.The @code{pipe} function is declared in the header file@file{unistd.h}.@pindex unistd.h@comment unistd.h@comment POSIX.1@deftypefun int pipe (int @var{filedes}@t{[2]})The @code{pipe} function creates a pipe and puts the file descriptorsfor the reading and writing ends of the pipe (respectively) into@code{@var{filedes}[0]} and @code{@var{filedes}[1]}.An easy way to remember that the input end comes first is that filedescriptor @code{0} is standard input, and file descriptor @code{1} isstandard output.If successful, @code{pipe} returns a value of @code{0}.  On failure,@code{-1} is returned.  The following @code{errno} error conditions aredefined for this function:@table @code@item EMFILEThe process has too many files open.@item ENFILEThere are too many open files in the entire system.  @xref{Error Codes},for more information about @code{ENFILE}.@end table@end deftypefunHere is an example of a simple program that creates a pipe.  This programuses the @code{fork} function (@pxref{Creating a Process}) to createa child process.  The parent process writes data to the pipe, which isread by the child process.@smallexample@include pipe.c.texi@end smallexample@node Pipe to a Subprocess@section Pipe to a Subprocess@cindex creating a pipe to a subprocess@cindex pipe to a subprocess@cindex filtering i/o through subprocessA common use of pipes is to send data to or receive data from a programbeing run as subprocess.  One way of doing this is by using a combination of@code{pipe} (to create the pipe), @code{fork} (to create the subprocess),@code{dup2} (to force the subprocess to use the pipe as its standard inputor output channel), and @code{exec} (to execute the new program).  Or,you can use @code{popen} and @code{pclose}.The advantage of using @code{popen} and @code{pclose} is that theinterface is much simpler and easier to use.  But it doesn't offer asmuch flexibility as using the low-level functions directly.@comment stdio.h@comment POSIX.2, SVID, BSD@deftypefun {FILE *} popen (const char *@var{command}, const char *@var{mode})The @code{popen} function is closely related to the @code{system}function; see @ref{Running a Command}.  It executes the shell command@var{command} as a subprocess.  However, instead of waiting for thecommand to complete, it creates a pipe to the subprocess and returns astream that corresponds to that pipe.If you specify a @var{mode} argument of @code{"r"}, you can read from the stream to retrieve data from the standard output channel of the subprocess.The subprocess inherits its standard input channel from the parent process.Similarly, if you specify a @var{mode} argument of @code{"w"}, you canwrite to the stream to send data to the standard input channel of thesubprocess.  The subprocess inherits its standard output channel fromthe parent process.In the event of an error, @code{popen} returns a null pointer.  Thismight happen if the pipe or stream cannot be created, if the subprocesscannot be forked, or if the program cannot be executed.@end deftypefun@comment stdio.h@comment POSIX.2, SVID, BSD@deftypefun int pclose (FILE *@var{stream})The @code{pclose} function is used to close a stream created by @code{popen}.It waits for the child process to terminate and returns its status value,as for the @code{system} function.@end deftypefunHere is an example showing how to use @code{popen} and @code{pclose} tofilter output through another program, in this case the paging program@code{more}.@smallexample@include popen.c.texi@end smallexample@node FIFO Special Files@section FIFO Special Files@cindex creating a FIFO special file@cindex interprocess communication, with FIFOA FIFO special file is similar to a pipe, except that it is created in adifferent way.  Instead of being an anonymous communications channel, aFIFO special file is entered into the file system by calling@code{mkfifo}.Once you have created a FIFO special file in this way, any process canopen it for reading or writing, in the same way as an ordinary file.However, it has to be open at both ends simultaneously before you canproceed to do any input or output operations on it.  Opening a FIFO forreading normally blocks until some other process opens the same FIFO forwriting, and vice versa.The @code{mkfifo} function is declared in the header file@file{sys/stat.h}.@pindex sys/stat.h@comment sys/stat.h@comment POSIX.1@deftypefun int mkfifo (const char *@var{filename}, mode_t @var{mode})The @code{mkfifo} function makes a FIFO special file with name@var{filename}.  The @var{mode} argument is used to set the file'spermissions; see @ref{Setting Permissions}.The normal, successful return value from @code{mkfifo} is @code{0}.  Inthe case of an error, @code{-1} is returned.  In addition to the usualfile name syntax errors (@pxref{File Name Errors}), the following@code{errno} error conditions are defined for this function:@table @code@item EEXISTThe named file already exists.@item ENOSPCThe directory or file system cannot be extended.@item EROFSThe directory that would contain the file resides on a read-only filesystem.@end table@end deftypefun@node Pipe Atomicity@section Atomicity of Pipe I/OReading or writing pipe data is @dfn{atomic} if the size of data writtenis less than @code{PIPE_BUF}.  This means that the data transfer seemsto be an instantaneous unit, in that nothing else in the system canobserve a state in which it is partially complete.  Atomic I/O may notbegin right away (it may need to wait for buffer space or for data), butonce it does begin, it finishes immediately.Reading or writing a larger amount of data may not be atomic; forexample, output data from other processes sharing the descriptor may beinterspersed.  Also, once @code{PIPE_BUF} characters have been written,further writes will block until some characters are read.@xref{Limits for Files}, for information about the @code{PIPE_BUF}parameter.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -