epoll 多路复用 I/O工作流程

2020-09-04  本文已影响0人  wenfh2020

从业务逻辑上,了解一下 epoll 多路复用 I/O 的工作流程。

有兴趣了解 epoll 源码实现,可以参考: 《[epoll 源码走读] epoll 实现原理


1. epoll

epoll 是一个 Linux 系统的一个事件驱动。简单点来说,是一个针对系统文件的事件管理器,可以高效管理大量网络链接下的数据并发。研发人员根据业务需要,通过事件管理器,监控对应文件描述符的读写事件。(详细解析请参考: 百度百科


🔥 文章来源:《epoll 多路复用 I/O工作流程

1.1. 事件结构

// epoll.h
typedef union epoll_data {
  void *ptr;
  int fd;
  uint32_t u32;
  uint64_t u64;
} epoll_data_t;

struct epoll_event {
  uint32_t events;   // epoll 事件
  epoll_data_t data; // 用户数据
} __EPOLL_PACKED;

epoll 事件 描述
EPOLLIN 可读。
EPOLLOUT 可写。
EPOLLERR 该文件描述符发生错误。
EPOLLHUP 该文件描述符被挂断。

1.2. 操作接口

int epoll_create(int size);
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
op 操作事件 描述
EPOLL_CTL_ADD 注册新的 fd 到 epfd
EPOLL_CTL_MOD 修改已经注册的 fd 的监听事件
EPOLL_CTL_DEL 从 epfd 中删除一个 fd
int epoll_wait(int epfd, struct epoll_event* events, int maxevents. int timeout);

2. 服务器使用流程

epoll 使用流程

3. 参考

上一篇 下一篇

猜你喜欢

热点阅读