Search code examples
clinuxsystem-callspollingpoll-syscall

check revents into struct pollfd


According to man(2) poll:

int poll(struct pollfd *fds, nfds_t nfds, int timeout);
struct pollfd {
    int   fd;         /* file descriptor */
    short events;     /* requested events */
    short revents;    /* returned events */
};

If I write if(! (fds.revents &1 )) after using poll What does it mean?


Solution

  • According to man(2) poll indeed...

    The field revents is an output parameter, filled by the kernel with the events that actually occurred. The bits returned in revents can include any of those specified in events, or one of the values POLLERR, POLLHUP, or POLLNVAL. (These three bits are meaningless in the events field, and will be set in the revents field whenever the corresponding condition is true.)

    poll.h defines those as

    #define POLLIN      0x001       /* There is data to read.  */
    #define POLLPRI     0x002       /* There is urgent data to read.  */
    #define POLLOUT     0x004       /* Writing now will not block.  */
    // etc...
    

    so armed with this knowledge,

    if(!(fds.revents & 1))
    

    is the same as

    if(!(fds.revents & POLLIN))
    

    which means "if the "there is data to read" bit is not set", i.e. "if there is no data to read".