pipe()--Create an Interprocess Channel


  Syntax
 #include <unistd.h>

 int pipe(int fildes[2]);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Yes

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.


Parameters

fildes[2]
(Output) An integer array of size 2 that will receive the pipe descriptors.

Authorities

None.


Return Value

0 pipe() was successful.
-1 pipe() was not successful. The errno variable is set to indicate the error.


Error Conditions

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]  

Usage Notes

  1. If this function is called by a thread executing one of the scan-related exit programs (or any of its created threads), the descriptors that are returned are scan descriptors. See Integrated File System Scan on Open Exit Programs and Integrated File System Scan on Close Exit Programs for more information. If a process is spawned, these scan descriptors are not inherited by the spawned process and therefore cannot be used in that spawned process. Therefore, in this case, the descriptors returned by pipe() function will only work within the same process.

Related Information


Example

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;
}


API introduced: V5R1
Top | UNIX-Type APIs | APIs by category