失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > epoll示例(边沿触发)

epoll示例(边沿触发)

时间:2021-11-22 21:55:18

相关推荐

epoll示例(边沿触发)

#include<stdio.h>#include<unistd.h>#include<sys/epoll.h>struct t_connection{int fd;void *(*call)(t_connection*);};void* f(t_connection* c){char buf[1024];read(c->fd, buf, 1);//这里每次取一个字符后,epoll_wait不会被触发printf("recv: %c\n", buf[0]);}int main(){int epfd, nfds;struct epoll_event ev;struct epoll_event events[5];t_connection connection;connection.fd=STDIN_FILENO;connection.call = f;epfd = epoll_create(1);ev.data.ptr = (void*) &connection;ev.events = EPOLLIN | EPOLLET;epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev);for(;;){nfds = epoll_wait(epfd, events, 5, -1);for(int i = 0; i < nfds; ++i){t_connection *c =(t_connection*) events[i].data.ptr;c->call(c);}}}

如果觉得《epoll示例(边沿触发)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。