【问题标题】:Significance of parameters in epoll_event structure (epoll)epoll_event结构(epoll)中参数的意义
【发布时间】:2014-08-28 16:00:16
【问题描述】:

我正在使用 epoll_ctl() 和 epoll_wait() 系统调用。

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);

struct epoll_event {
    uint32_t events; /* epoll events (bit mask) */
    epoll_data_t data; /* User data */
};


typedef union epoll_data {
     enter code here`void *ptr; /* Pointer to user-defined data */  
     int fd; /* File descriptor */  
     uint32_t u32; /* 32-bit integer */  
     uint64_t u64; /* 64-bit integer */  
} epoll_data_t;

在使用epoll_ctl的时候,我可以使用union epoll_data来指定fd。一种方法是在“fd”成员中指定它。另一种方法是在我自己的结构中指定它,“ptr”成员将指向该结构。

“u32”和“u64”有什么用?

我浏览了内核系统调用实现,发现如下:
1、epoll_ctl初始化epoll_event并存储(某种RB树格式)
2. fd准备好后,epoll_wait返回epoll_ctl中填写的epoll_event。在此之后,我可以识别准备就绪的 fd。我不明白“u32”和“u64”的目的。

【问题讨论】:

    标签: linux linux-kernel epoll


    【解决方案1】:

    预定义的联合是为了方便使用,您通常只会使用其中之一:

    • 如果要存储指针,请使用 ptr
    • 如果要存储套接字描述符,请使用 fd
    • 如果要存储通用/不透明的 32/64 位数字,请使用 u32/u64

    实际上,epoll_data 是与套接字事件关联的 64 位数据,供您存储任何内容以查找事件处理程序,因为 epoll_wait 仅返回两件事:事件和关联的 epoll_data。

    【讨论】:

    • 在最新内核__u64 user_data;987654321@的io_uring机制中实现了类似的思路
    【解决方案2】:

    epoll_data 的字段没有任何预定义的含义;您的应用程序指定了一个含义。

    任何字段都可用于存储应用程序识别事件所需的信息。 (u32u64 可能对数组索引等有用。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多