RAD Studio |  |

_pipe

Header File

io.h, fcntl.h

Category

Input/output Routines

Syntax

int _pipe(int *handles, unsigned int size, int mode);

Description

Creates a read/write pipe.

The _pipe function creates an anonymous pipe that can be used to pass information between processes. The pipe is opened for both reading and writing. Like a disk file, a pipe can be read from and written to, but it does not have a name or permanent storage associated with it; data written to and from the pipe exist only in a memory buffer managed by the operating system.

The read handle is returned to handles[0], and the write handle is returned to handles[1]. The program can use these handles in subsequent calls to read, write, dup, dup2, or close. When all pipe handles are closed, the pipe is destroyed.

The size of the internal pipe buffer is size. A recommended minimum value is 512 bytes.

The translation mode is specified by mode, as follows:

O_BINARY | The pipe is opened in binary mode
O_TEXT | The pipe is opened in text mode

If mode is zero, the translation mode is determined by the external variable _fmode.

Return Value

On success, _pipe returns 0 and returns the pipe handles to handles[0] and handles[1].

On error, it returns -1 and sets errno to one of the following values:

EMFILE | Too many open files
ENOMEM | Out of memory

Example

Copy Code

    /*
     There are two short programs here.  SEND spawns a child
     process, RECEIVE.  Each process holds one end of a
     pipe.  The parent transmits its command-line argument
     to the child, which prints the string and exits.
     IMPORTANT:  The parent process must be linked with
     the \32bit\fileinfo.obj file.  The code in fileinfo
     enables a parent to share handles with a child.
     Without this extra information, the child cannot use
     the handle it receives.
    */
    /* SEND */
    #include <fcntl.h>            // _pipe()
    #include <io.h>            // write()
    #include <process.h>          // spawnl() cwait()
    #include <stdio.h>          // puts() perror()
    #include <stdlib.h>          // itoa()
    #include <string.h>          // strlen()
    #define DECIMAL_RADIX 10      // for atoi()
    enum PIPE_HANDLES { IN, OUT };  // to index the array of handles
    int main(int argc, char *argv[])
    {
      int handles[2];           // in- and
    //outbound pipe handles
      char handleStr[10];        // a handle
    //stored as a string
      int pid;
      // system's ID for child process
      if (argc <= 1)
      {
        puts("No message to send.");
        return(1);
      }
      if (_pipe(handles, 256, O_TEXT) != 0)
      {
        perror("Cannot create the pipe");
        return(1);
      }
      // store handle as a string for passing on the command line
      itoa(handles[IN], handleStr, DECIMAL_RADIX);
      // create the child process, passing it the inbound pipe handle
         spawnl(P_NOWAIT, "receive.exe", "receive.exe", handleStr, NULL);
      // transmit the message
      write(handles[OUT], argv[1], strlen(argv[1])+1);
      // when done with the pipe, close both handles
      close(handles[IN]);
      close(handles[OUT]);
      // wait for the child to finish
      wait(NULL);
      return(0);
    }

Portability

POSIX | Win32 | ANSI C | ANSI C++
| + |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!