poll
Prototype:
#include <sys/poll.h>
int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
General Description: Similar to select(), this call waits on any one of several I/O channels for changes. Instead of using macros for managing and controlling the descriptor list, the programmer uses structure entries.
Return Value: If less than zero, an error occurred, and a zero returned means call timed out. Otherwise, the call returns the number of descriptor records than changed.
Parameters
udfs This is an array of pollfd structures. Each record tracks a different file descriptor.struct pollfd{int fd; /* file descriptor */short events; /* requested events */short revents; /* returned events */};The fd field is the file descriptor to check. The events and revents fields indicate the events to check and the events that occurred, respectively. The bit-values available are:POLLIN - There is data to readPOLLPRI - There is urgent data to readPOLLOUT - Writing now will not blockPOLLERR - Error conditionPOLLHUP - Hung upPOLLNVAL - Invalid request: fd not openPOLLRDNORM - Normal read (Linux only)POLLRDBAND - Read out-of-band (Linux only)POLLWRNORM - Normal write (Linux only)POLLWRBAND - Write out-of-band (Linux only)POLLMSG - ??? (Linux only)
nfds The number of records to check during the call.
timeout The timeout in milliseconds. If timeout is negative, the call waits forever.
Possible Errors
ENOMEM There was no space to allocate file descriptor tables.
EFAULT The array given as argument was not contained in the calling program's address space.
EINTR A signal occurred before any requested event.
Examples
int fd_count=0;
struct pollfd fds[MAXFDs];
fds[fd_count].fd = socket(PF_INET, SOCK_STREAM, 0);
fds[fd_count++].events = POLLIN;
for (;;)
{
    if ( poll(fds, fd_count, TIMEOUT_MS) > 0 )
    {   int i;
        if ( (fds[0].revents & POLLIN) != 0 )
        {
            fds[fd_count].events = POLLIN | POLLHUP;
            fds[fd_count++].fd = accept(fds[0].fd, 0, 0);
        }
        for ( i = 1; i < fd_count; i++ )
        {
            if ( (fds[i].revents & POLLHUP) != 0 )
            {
                close(fds[i].fd);
                /*** Move up FDs to fill empty slot ***/
                fd_count--;
            }
            else if ( (fds[i].revents & POLLIN) != 0 )
                /*** Read and process data ***/
        }
    }
}