pipe2() — Create a pipe

Standards

Standards / Extensions C or C++ Dependencies
z/OS® UNIX both  

Format

#define _XPLATFORM_SOURCE
#define _POSIX_SOURCE             /* Definition of O_* constants */
#include <fcntl.h>                /* Definition of O_* constants */
#include <unistd.h>

int pipe2(int pipefd[2], int flags);

General description

pipe2() creates a pipe, which is a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors that refer to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data that is written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe.

If flags is 0, pipe2() is the same as pipe(). The following values can be bitwise ORed in flags to obtain different behavior:

O_CLOEXEC
Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors.
O_DIRECT
Create a pipe that performs I/O in "packet" mode. Each write to the pipe is dealt with as a separate packet. Read one packet at a time from the pipe.
Notes:
  • Start of changeThe maximum number of bytes that can be written atomically when writing to a pipe can be determined by using pathconf().End of change
  • If a read specifies a buffer size that is smaller than the next packet, the requested number of bytes are read, and the excess bytes in the packet are discarded. Specifying a buffer size of PIPE_BUF are sufficient to read the largest possible packets.
  • Zero-length packets are not supported. (A read that specifies a buffer size of zero is a no-op, and returns 0.)
O_NONBLOCK
Set the O_NONBLOCK file status flag on the open file descriptions that are referred to by the new file descriptors. Using this flag saves extra calls to fcntl() to achieve the same result.

Returned value

If successful, pipe2() returns 0.

If unsuccessful, pipe2() returns -1 and sets errno to one of the following values:
Error Code
Description
EFAULT
pipefd is invalid.
EINVAL
Invalid value in flags.
EMFILE
The process reaches the maximum number of file descriptors that it can have open.
ENFILE
The maximum number of file descriptors that can be open is reached.

Related information