Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C Library Reference - <u>synopsis:</u> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
    #include <io.h>
    int _pipe( int *phandles, unsigned psize, int textmode );

Description:
    The _pipe function creates a pipe (an unnamed FIFO) and places a file
    descriptor for the read end of the pipe in phandles[0] and a file
    descriptor for the write end of the pipe in phandles[1].  Their integer
    values are the two lowest available at the time of the _pipe function
    call.  The  O_NONBLOCK flag is cleared for both file descriptors.  (The
    fcntl call can be used to set the  O_NONBLOCK flag.)

    Data can be written to file descriptor phandles[1] and read from file
    descriptor phandles[0].  A read on file descriptor phandles[0] returns
    the data written to phandles[1] on a first-in-first-out (FIFO) basis.

    This function is typically used to connect together standard utilities
    to act as filters, passing the write end of the pipe to the data
    producing process as its  STDOUT_FILENO and the read end of the pipe to
    the data consuming process as its  STDIN_FILENO.  (either via the
    traditional fork/dup2/exec or the more efficient spawn calls).

    If successful, _pipe marks for update the st_ftime, st_ctime, st_atime
    and st_mtime fields of the pipe for updating.

Returns:
    The _pipe function returns zero on success.  Otherwise, (-1) is returned
    and  errno is set to indicate the error.

Errors:
    When an error has occurred,  errno contains a value indicating the type
    of error that has been detected.  If any of the following conditions
    occur, the _pipe function shall return (-1) and set  errno to the
    corresponding value:

    Constant     Meaning

EMFILE
    The calling process does not have at least 2 unused file descriptors
    available.

ENFILE
    The number of simultaneously open files in the system would exceed the
    configured limit.

ENOSPC
    There is insufficient space available to allocate the pipe buffer.

EROFS
    The pipe pathname space is a read-only filesystem.


Example:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <fcntl.h>
    #include <io.h>
    #include <dos.h>
    #include <process.h>

    static int handles[2] = { 0, 0 };
    static int pid;

    create_pipe()
      {
        if( _pipe( (int *)&handles, 2048, _O_BINARY ) == -1 ) {
          perror( "create_pipe" );
          exit( EXIT_FAILURE );
        }
      }

    create_child( char *name )
      {
        char buff[10];

        itoa( handles[0], buff, 10 );
        pid = spawnl( P_NOWAIT, name,
                   "_pipe", buff, NULL );
        close( handles[0] );
        if( pid == -1 ) {
          perror( "create_child" );
          close( handles[1] );
          exit( EXIT_FAILURE );
        }
      }

    fill_pipe()
      {
        int i;
        int rc;

        for( i = 1; i <= 10; i++ ) {
          printf( "Child, what is 5 times %d\n", i );
          rc = write( handles[1], &i, sizeof( int ) );
          if( rc < sizeof( int ) ) {
            perror( "fill_pipe" );
            close( handles[1] );
            exit( EXIT_FAILURE );
          }
        }
        /* indicate that we are done */
        write( handles[1], &i, 1 );
        close( handles[1] );
      }

    empty_pipe( int in_pipe )
      {
        int i;
        int amt;

        for(;;) {
          amt = read( in_pipe, &i, sizeof( int ) );
          if( amt != sizeof( int ) ) break;
          printf( "Parent, 5 times %d is %d\n", i, 5*i );
        }
        if( amt == -1 ) {
          perror( "empty_pipe" );
          exit( EXIT_FAILURE );
        }
        close( in_pipe );
      }

    void main( int argc, char *argv[] )
      {
        if( argc <= 1 ) {
          /* we are the spawning process */
          create_pipe();
          create_child( argv[0] );
          fill_pipe();
        } else {
          /* we are the spawned process */
          empty_pipe( atoi( argv[1] ) );
        }
        exit( EXIT_SUCCESS );
      }

    produces the following:

    Child, what is 5 times 1
    Child, what is 5 times 2
    Parent, 5 times 1 is 5
    Parent, 5 times 2 is 10
    Child, what is 5 times 3
    Child, what is 5 times 4
    Parent, 5 times 3 is 15
    Parent, 5 times 4 is 20
    Child, what is 5 times 5
    Child, what is 5 times 6
    Parent, 5 times 5 is 25
    Parent, 5 times 6 is 30
    Child, what is 5 times 7
    Child, what is 5 times 8
    Parent, 5 times 7 is 35
    Parent, 5 times 8 is 40
    Child, what is 5 times 9
    Child, what is 5 times 10
    Parent, 5 times 9 is 45
    Parent, 5 times 10 is 50

Classification:
    WATCOM

Systems:
    Win32, OS/2 1.x(all), OS/2-32

See Also:
    open, _pclose, perror, _popen, read, write

See Also: _pclose read

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson