environment/getpipecapacity.c

///////////////////////////////////////////////////////////////////////////////
// Filename: getpipecapacity.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: get capacity of a pipe
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/03/02 03:20:31 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////


// Feature test switches ///////////////////////////// Feature test switches //

#ifndef hpux
#define USE_SELECT 1
#else
#define USE_POLL 1
#endif



// System headers /////////////////////////////////////////// System headers //

#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>

#ifdef USE_POLL
#include <poll.h>
// #include <stropts.h>
#endif




// Local headers ///////////////////////////////////////////// Local headers //

#include "../common.h"



// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */



// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //
    /* NONE */



// Main /////////////////////////////////////////////////////////////// Main //

int
main(int argc, char *argv[])
{
    error.set_program_name(argv[0]);    

    int i, n, fd[2];

#ifdef USE_SELECT
    fd_set writeset;
    struct timeval  tv;

    if (pipe(fd) < 0)
        error.system("pipe error");

    FD_ZERO(&writeset);

    for (n = 0; ; n++)
    {
        // write 1 byte at a time until pipe is full 

        FD_SET(fd[1], &writeset);

        tv.tv_sec = tv.tv_usec = 0;     // don't wait at all 

        if ( (i = select(fd[1]+1, NULL, &writeset, NULL, &tv)) < 0)
            error.system("select error");
        else if (i == 0)
            break;

        if (write(fd[1], "X", 1) != 1)
            error.system("write error");
    }
#endif


#ifdef USE_POLL
    struct pollfd fdarray[1];

    if (pipe(fd) < 0)
        error.system("pipe error");

    for (n = 0; true; n++)
    {
        fdarray[0].fd = fd[1];
        fdarray[0].events = POLLOUT;

        if ( (i = poll(fdarray, 1, 0)) < 0)
            error.system("poll error");
        else if (i == 0)
            break;

        if (write(fd[1], "X", 1) != 1)
            error.system("write error");
    }
#endif


    cout << "pipe capacity = " << n << endl;

    exit(EXIT_SUCCESS);
}