#include <unistd.h> int pipe(int fildes[2]);Service Program Name: QP0LLIB1
The pipe() function creates a data pipe and places two file descriptors, one each into the arguments fildes[0] and fildes[1], that refer to the open file descriptions for the read and write ends of the pipe, respectively. Their integer values will be the two lowest available at the time of the pipe() call. The O_NONBLOCK and FD_CLOEXEC flags will be clear on both descriptors. NOTE: these flags can, however, be set by the fcntl() function.
Data can be written to the file descriptor fildes[1] and read from file descriptor fildes[0]. A read on the file descriptor fildes[0] will access data written to the file descriptor fildes[1] on a first-in-first-out basis. File descriptor fildes[0] is open for reading only. File descriptor fildes[1] is open for writing only.
The pipe() function is often used with the spawn() function to allow the parent and child processes to send data to each other.
Upon successful completion, pipe() will update the access time, change time, and modification time of the pipe.
None.
0 | pipe() was successful. |
-1 | pipe() was not successful. The errno variable is set to indicate the error. |
If pipe() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Error condition | Additional information |
---|---|
[EFAULT] | |
[EMFILE] | |
[ENFILE] | |
[ENOMEM] | |
[EUNKNOWN] |
See Code disclaimer information for information pertaining to code examples.
The following example creates a pipe, writes 10 bytes of data to the pipe, and then reads those 10 bytes of data from the pipe.
#include <stdio.h> #include <unistd.h> #include <string.h> void main() { int fildes[2]; int rc; char writeData[10]; char readData[10]; int bytesWritten; int bytesRead; memset(writeData,'A',10); if (-1 == pipe(fildes)) { perror("pipe error"); return; } if (-1 == (bytesWritten = write(fildes[1], writeData, 10))) { perror("write error"); } else { printf("wrote %d bytes\n",bytesWritten); if (-1 == (bytesRead = read(fildes[0], readData, 10))) { perror("read error"); } else { printf("read %d bytes\n",bytesRead); } } close(fildes[0]); close(fildes[1]); return; }
Top | UNIX-Type APIs | APIs by category |