@Go to the ‰ŠP�”›first@�”˜, ‰ŠP�”›previous@�”˜, ‰ŠP�”›next@�”˜, ‰ŠP�”›last@�”˜ section, ‰ŠP�”›table of contents@�”˜.
Œ

ˆ‰SEC175Št�”›Pipes and FIFOs

‰IDX897Š@�”˜A Bpipe@ is a mechanism for interprocess communication; data written to the pipe by one process can be read by another process. The data is handled in a first-in, first-out (FIFO) order. The pipe has no name; it is created for one use and both ends must be inherited from the single process which created the pipe.
‰IDX898Š@�”˜A BFIFO special file@ is similar to a pipe, but instead of being an anonymous, temporary connection, a FIFO has a name or names like any other file. Processes open the FIFO by name in order to communicate through it.
A pipe or FIFO has to be open at both ends simultaneously. If you read from 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 read returns end-of-file. Writing to a pipe or FIFO that doesn't have a reading process is treated as an error condition; it generates a HSIGPIPE@ signal, and fails with error code HEPIPE@ if the signal is handled or blocked.
Neither pipes nor FIFO special files allow file positioning. Both reading and writing operations happen sequentially; reading from the beginning of the file and writing at the end.
ˆ‰SEC176Št�”›Creating a Pipe

‰IDX899Š‰IDX900Š‰IDX901Š
@�”˜The primitive for creating a pipe is the Hpipe@ function. This creates both the reading and writing ends of the pipe. It is not very useful for a single process to use a pipe to talk to itself. In typical use, a process creates a pipe just before it forks one or more child processes (see section ‰ŠP�”›Creating a Process@�”˜). The pipe is then used for communication either between the parent or child processes, or between two sibling processes.
The Hpipe@ function is declared in the header file H`unistd.h'@. ‰IDX902Š

A�”˜Function:@ int Dpipe@ B(int BfiledesH[2]@)
Â‰IDX903Š@�”˜The Hpipe@ function creates a pipe and puts the file descriptors for the reading and writing ends of the pipe (respectively) into JfiledesH[0]@ and JfiledesH[1]@.
An easy way to remember that the input end comes first is that file descriptor H0@ is standard input, and file descriptor H1@ is standard output.
If successful, Hpipe@ returns a value of H0@. On failure, H-1@ is returned. The following Herrno@ error conditions are defined for this function:
HEMFILE
Ä@The process has too many files open.
ÂHENFILE
Ä@There are too many open files in the entire system. See section ‰ŠP�”›Error Codes@�”˜, for more information about HENFILE@. This error never occurs in the GNU system.Â
À

Here is an example of a simple program that creates a pipe. This program uses the Hfork@ function (see section ‰ŠP�”›Creating a Process@�”˜) to create a child process. The parent process writes data to the pipe, which is read by the child process. #include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

/* Read characters from the pipe and echo them to Hstdout@. */

void
read_from_pipe (int file)
{
FILE *stream;
int c;
stream = fdopen (file, "r");
while ((c = fgetc (stream)) != EOF)
putchar (c);
fclose (stream);
}

/* Write some random text to the pipe. */

void
write_to_pipe (int file)
{
FILE *stream;
stream = fdopen (file, "w");
fprintf (stream, "hello, world!\n");
fprintf (stream, "goodbye, world!\n");
fclose (stream);
}

int
main (void)
{
pid_t pid;
int mypipe[2];

/* Create the pipe. */
if (pipe (mypipe))
{
fprintf (stderr, "Pipe failed.\n");
return EXIT_FAILURE;
}

/* Create the child process. */
pid = fork ();
if (pid == (pid_t) 0)
{
/* This is the child process. */
read_from_pipe (mypipe[0]);
return EXIT_SUCCESS;
}
else if (pid < (pid_t) 0)
{
/* The fork failed. */
fprintf (stderr, "Fork failed.\n");
return EXIT_FAILURE;
}
else
{
/* This is the parent process. */
write_to_pipe (mypipe[1]);
return EXIT_SUCCESS;
}
}
ˆ‰SEC177Št�”›Pipe to a Subprocess

‰IDX904Š‰IDX905Š‰IDX906Š
@�”˜A common use of pipes is to send data to or receive data from a program being run as subprocess. One way of doing this is by using a combination of Hpipe@ (to create the pipe), Hfork@ (to create the subprocess), Hdup2@ (to force the subprocess to use the pipe as its standard input or output channel), and Hexec@ (to execute the new program). Or, you can use Hpopen@ and Hpclose@.
The advantage of using Hpopen@ and Hpclose@ is that the interface is much simpler and easier to use. But it doesn't offer as much flexibility as using the low-level functions directly.

AFunction:@ FILE * Dpopen@ B(const char *Bcommand@, const char *Bmode@)
Â‰IDX907Š@�”˜The Hpopen@ function is closely related to the Hsystem@ function; see section ‰ŠP�”›Running a Command@�”˜. It executes the shell command Bcommand@ as a subprocess. However, instead of waiting for the command to complete, it creates a pipe to the subprocess and returns a stream that corresponds to that pipe.
If you specify a Bmode@ argument of H"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 Bmode@ argument of H"w"@, you can write to the stream to send data to the standard input channel of the subprocess. The subprocess inherits its standard output channel from the parent process.
In the event of an error, Hpopen@ returns a null pointer. This might happen if the pipe or stream cannot be created, if the subprocess cannot be forked, or if the program cannot be executed.À

AFunction:@ int Dpclose@ B(FILE *Bstream@)
Â‰IDX908Š@�”˜The Hpclose@ function is used to close a stream created by Hpopen@. It waits for the child process to terminate and returns its status value, as for the Hsystem@ function.À

Here is an example showing how to use Hpopen@ and Hpclose@ to filter output through another program, in this case the paging program Hmore@. #include <stdio.h>
#include <stdlib.h>

void
write_data (FILE * stream)
{
int i;
for (i = 0; i < 100; i++)
fprintf (stream, "%d\n", i);
if (ferror (stream))
{
fprintf (stderr, "Output to stream failed.\n");
exit (EXIT_FAILURE);
}
}

int
main (void)
{
FILE *output;

output = popen ("more", "w");
if (!output)
{
fprintf (stderr, "Could not run more.\n");
return EXIT_FAILURE;
}
write_data (output);
pclose (output);
return EXIT_SUCCESS;
}
ˆ‰SEC178Št�”›FIFO Special Files

‰IDX909Š‰IDX910Š
@�”˜A FIFO special file is similar to a pipe, except that it is created in a different way. Instead of being an anonymous communications channel, a FIFO special file is entered into the file system by calling Hmkfifo@.
Once you have created a FIFO special file in this way, any process can open 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 can proceed to do any input or output operations on it. Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.
The Hmkfifo@ function is declared in the header file H`sys/stat.h'@. ‰IDX911Š

A�”˜Function:@ int Dmkfifo@ B(const char *Bfilename@, mode_t Bmode@)
Â‰IDX912Š@�”˜The Hmkfifo@ function makes a FIFO special file with name Bfilename@. The Bmode@ argument is used to set the file's permissions; see section ‰ŠP�”›Assigning File Permissions@�”˜.
The normal, successful return value from Hmkfifo@ is H0@. In the case of an error, H-1@ is returned. In addition to the usual file name errors (see section ‰ŠP�”›File Name Errors@�”˜), the following Herrno@ error conditions are defined for this function:
HEEXIST
Ä@The named file already exists.
ÂHENOSPC
Ä@The directory or file system cannot be extended.
ÂHEROFS
Ä@The directory that would contain the file resides on a read-only file system.Â
À

ˆ‰SEC179Št�”›Atomicity of Pipe I/O

@�”˜Reading or writing pipe data is Batomic@ if the size of data written is not greater than HPIPE_BUF@. This means that the data transfer seems to be an instantaneous unit, in that nothing else in the system can observe a state in which it is partially complete. Atomic I/O may not begin right away (it may need to wait for buffer space or for data), but once it does begin, it finishes immediately.
Reading or writing a larger amount of data may not be atomic; for example, output data from other processes sharing the descriptor may be interspersed. Also, once HPIPE_BUF@ characters have been written, further writes will block until some characters are read.
See section ‰ŠP�”›Limits on File System Capacity@�”˜, for information about the HPIPE_BUF@ parameter.
Œ
Go to the ‰ŠP�”›first@�”˜, ‰ŠP�”›previous@�”˜, ‰ŠP�”›next@�”˜, ‰ŠP�”›last@�”˜ section, ‰ŠP�”›table of contents@�”˜.