Name

pipe - create a pipe

Library

libc.lib

Synopsis

  #include <unistd.h>
  int pipe (int *fildes);

Return values

The pipe function returns the value 0 if successful; otherwise the value -1 is returned and errno is set to indicate the error.

Detailed description

The pipe system call creates a pipe, which is an object allowing bidirectional data flow, and allocates a pair of file descriptors.

By convention, the first descriptor is normally used as the read end of the pipe, and the second is normally the write end, so that data written to fildes[1] appears on (i.e., can be read from) fildes[0]. This allows the output of one thread to be sent to another thread: the source’s standard output is set up to be the write end of the pipe, and the sink’s standard input is set up to be the read end of the pipe. The pipe itself persists until all its associated descriptors are closed.

A pipe that has had an end closed is considered widowed. Writing on such a pipe causes the writing process to fail and errno is set to EPIPE Widowing a pipe is the only way to deliver end-of-file to a reader: after the reader consumes any buffered data, reading a widowed pipe returns a zero count.


Examples

#include <unistd.h>
#include <stdio.h>

         
int main(void)
{
    int fds[2];
    if (pipe(fds) == -1) {
       printf("Pipe creation failed\n");
    }
    /* fds[0] - opened for read */
    /* fds[1] - opened for write */
    close(fds[0]);
    close(fds[1]);
    return 0;
}

         


Errors

The pipe system call will fail if:
[EMFILE]
  Too many descriptors are active.
[ENFILE]
  The system file table is full.

See also

read, write

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top