企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 2.4 epoll API 2.4.1 创建EPOLL ```cpp /** * @param size 告诉内核监听的数目 * * @returns 返回一个epoll句柄(即一个文件描述符) */ int epoll_create(int size); ``` ```cpp int epfd = epoll_create(1000); ``` ![](https://img.kancloud.cn/2c/7f/2c7f01f0a35c532df60ab2d79957c22d_390x209.png) 2.4.2 控制EPOLL ```cpp /** * @param epfd 用epoll_create所创建的epoll句柄 * @param op 表示对epoll监控描述符控制的动作 * * EPOLL_CTL_ADD(注册新的fd到epfd) * EPOLL_CTL_MOD(修改已经注册的fd的监听事件) * EPOLL_CTL_DEL(epfd删除一个fd) * * @param fd 需要监听的文件描述符 * @param event 告诉内核需要监听的事件 * * @returns 成功返回0,失败返回-1, errno查看错误信息 */ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); struct epoll_event { __uint32_t events; /* epoll 事件 */ epoll_data_t data; /* 用户传递的数据 */ } /* * events : {EPOLLIN, EPOLLOUT, EPOLLPRI, EPOLLHUP, EPOLLET, EPOLLONESHOT} */ typedef union epoll_data { void *ptr; int fd; uint32_t u32; uint64_t u64; } epoll_data_t; ``` ```cpp struct epoll_event new_event; new_event.events = EPOLLIN | EPOLLOUT; new_event.data.fd = 5; epoll_ctl(epfd, EPOLL_CTL_ADD, 5, &new_event); ``` ![](https://img.kancloud.cn/c9/c5/c9c503da4b406d4944cdb01f6e2464e2_757x342.png) 2.4.3 等待EPOLL ```cpp /** * * @param epfd 用epoll_create所创建的epoll句柄 * @param event 从内核得到的事件集合 * @param maxevents 告知内核这个events有多大, * 注意: 值 不能大于创建epoll_create()时的size. * @param timeout 超时时间 * -1: 永久阻塞 * 0: 立即返回,非阻塞 * >0: 指定微秒 * * @returns 成功: 有多少文件描述符就绪,时间到时返回0 * 失败: -1, errno 查看错误 */ int epoll_wait(int epfd, struct epoll_event *event, int maxevents, int timeout); ``` ```cpp struct epoll_event my_event[1000]; int event_cnt = epoll_wait(epfd, my_event, 1000, -1); ``` ![](https://img.kancloud.cn/e3/20/e320d522c4a14cd13ef573e7fec8acc6_756x418.png) 2.4.4 epoll编程框架 ```cpp //创建 epoll int epfd = epoll_crete(1000); //将 listen_fd 添加进 epoll 中 epoll_ctl(epfd, EPOLL_CTL_ADD, listen_fd,&listen_event); while (1) { //阻塞等待 epoll 中 的fd 触发 int active_cnt = epoll_wait(epfd, events, 1000, -1); for (i = 0 ; i < active_cnt; i++) { if (evnets[i].data.fd == listen_fd) { //accept. 并且将新accept 的fd 加进epoll中. } else if (events[i].events & EPOLLIN) { //对此fd 进行读操作 } else if (events[i].events & EPOLLOUT) { //对此fd 进行写操作 } } } ```