0

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?

MicrosoctCprog
  • 460
  • 1
  • 3
  • 23
  • 1
    Shouldn't you _know_ why you're writing something like that? Did you get that expression from somewhere else, or what's happening here? – AKX Mar 22 '21 at 20:26
  • @AKX I saw that code in another site and I want to understand what does it mean please? – MicrosoctCprog Mar 22 '21 at 20:28
  • Maybe you should at least link that other site, then, so we have more context of what's happening there. – AKX Mar 22 '21 at 20:28
  • 1
    Firstly it means the code is poorly written. Should use the symbolic constants in `poll.h` rather than magic values like `1`. That condition is true if the event corresponding to that bit was either not requested at all or if it was requested but did not occur during the `poll`. – kaylum Mar 22 '21 at 20:30

1 Answers1

3

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".

AKX
  • 152,115
  • 15
  • 115
  • 172