【问题标题】:check revents into struct pollfd检查 revents 到 struct pollfd
【发布时间】:2021-03-22 20:23:38
【问题描述】:

根据 man(2) 民意调查:

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

如果我在使用poll之后写if(! (fds.revents &1 ))是什么意思?

【问题讨论】:

  • 难道你不应该知道你为什么要写这样的东西吗?你是从其他地方得到这个表情的,还是这里发生了什么?
  • @AKX 我在另一个网站上看到了该代码,我想了解它是什么意思?
  • 也许你至少应该链接那个其他网站,这样我们就可以了解那里正在发生的事情。
  • 首先这意味着代码写得不好。应该使用poll.h 中的符号常量,而不是像1 这样的魔法值。如果与该位对应的事件根本没有被请求,或者如果它被请求但在poll 期间没有发生,则该条件为真。

标签: c linux system-calls polling poll-syscall


【解决方案1】:

根据man(2) poll确实...

revents 字段是一个输出参数,由内核填充 与实际发生的事件。返回的位 revents 可以包括 events 中指定的任何内容,或其中之一 POLLERR、POLLHUP 或 POLLNVAL 的值。 (这三个位是 在 events 字段中无意义,并将在 revents 中设置 只要相应的条件为真,字段。)

poll.h 将这些定义为

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

掌握了这些知识,

if(!(fds.revents & 1))

相同
if(!(fds.revents & POLLIN))

表示“如果“有数据要读取”位没有设置”,即“如果没有数据要读取”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多