本文主要是介绍(P6)epoll:epoll的两种触发模式,select/poll/epoll对比,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1.epoll的两种触发模式
- 2.select/poll/epoll对比
1.epoll的两种触发模式
-
两种触发模式
Level-Triggered
Edge-Triggered -
如果采用Level-Triggered,那什么时候关注EPOLLOUT事件?会不会造成busy-loop?
-
epoll的LT和ET模式对比
LT:电平触发
(1)EPOLLIN事件
什么时候触发呢?
内核中的socket接收缓冲区为空,可以理解为低电平,不会触发EPOLLIN事件;
内核中的socket接收缓冲区不为空,可以理解为高电平,即使缓冲区的数据没有读完,仍然会触发EPOLLIN事件,直到读完为止(2)EPOLLOUT事件
内核中的socket发送缓冲区不满,可以理解为高电平
内核中的socket发送缓冲区满,可以理解为低电平(3)LT是高电平触发ET:边沿触发
低电平->高电平,会触发
高电平->低电平,会触发
- epoll LT
- epoll ET
2.select/poll/epoll对比
-
原理对比
-
一个进程所能打开的最大连接数对比
-
FD剧增后带来的IO效率问题的对比
-
消息传递方式的对比
-
eg:src\05\echosrv_poll.cpp
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
#include <poll.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>#include <vector>
#include <iostream>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)typedef std::vector<struct pollfd> PollFdList;int main(void)
{signal(SIGPIPE, SIG_IGN);signal(SIGCHLD, SIG_IGN);int idlefd = open("/dev/null", O_RDONLY | O_CLOEXEC);int listenfd;//if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)if ((listenfd = socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP)) < 0)ERR_EXIT("socket");struct sockaddr_in servaddr;memset(&servaddr, 0, sizeof(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_port = htons(5188);servaddr.sin_addr.s_addr = htonl(INADDR_ANY);int on = 1;if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)ERR_EXIT("setsockopt");if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)ERR_EXIT("bind");if (listen(listenfd, SOMAXCONN) < 0)ERR_EXIT("listen");struct pollfd pfd;pfd.fd = listenfd;pfd.events = POLLIN;PollFdList pollfds;pollfds.push_back(pfd);int nready;struct sockaddr_in peeraddr;socklen_t peerlen;int connfd;while (1){//&*pollfds.begin()是一个输入输出参数,可以看成数组或者向量//若有1万个fd,假设只要3个fd产生了事件,也需要遍历整个数组&*pollfds.begin()//此外,每次调用poll的时候,需要将数组中相关的事件拷贝到内核的链表,内核才知道你这次查询关注的是哪些socket,哪些事件nready = poll(&*pollfds.begin(), pollfds.size(), -1);if (nready == -1){if (errno == EINTR)continue;ERR_EXIT("poll");}if (nready == 0) // nothing happendedcontinue;if (pollfds[0].revents & POLLIN){peerlen = sizeof(peeraddr);connfd = accept4(listenfd, (struct sockaddr*)&peeraddr,&peerlen, SOCK_NONBLOCK | SOCK_CLOEXEC);/* if (connfd == -1)ERR_EXIT("accept4");
*/if (connfd == -1){if (errno == EMFILE){close(idlefd);idlefd = accept(listenfd, NULL, NULL);close(idlefd);idlefd = open("/dev/null", O_RDONLY | O_CLOEXEC);continue;}elseERR_EXIT("accept4");}pfd.fd = connfd;pfd.events = POLLIN;pfd.revents = 0;pollfds.push_back(pfd);--nready;// 连接成功std::cout<<"ip="<<inet_ntoa(peeraddr.sin_addr)<<" port="<<ntohs(peeraddr.sin_port)<<std::endl;if (nready == 0)continue;}//std::cout<<pollfds.size()<<std::endl;//std::cout<<nready<<std::endl;//也需要遍历整个数组&*pollfds.begin()for (PollFdList::iterator it=pollfds.begin()+1;it != pollfds.end() && nready >0; ++it){if (it->revents & POLLIN){--nready;connfd = it->fd;char buf[1024] = {0};int ret = read(connfd, buf, 1024);if (ret == -1)ERR_EXIT("read");if (ret == 0){std::cout<<"client close"<<std::endl;it = pollfds.erase(it);--it;close(connfd);continue;}std::cout<<buf;write(connfd, buf, strlen(buf));}}}return 0;
}
- src\05\echosrv_epoll.cpp
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/epoll.h>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>#include <vector>
#include <algorithm>
#include <iostream>typedef std::vector<struct epoll_event> EventList;#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)int main(void)
{signal(SIGPIPE, SIG_IGN);signal(SIGCHLD, SIG_IGN);int idlefd = open("/dev/null", O_RDONLY | O_CLOEXEC);int listenfd;//if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)if ((listenfd = socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP)) < 0)ERR_EXIT("socket");struct sockaddr_in servaddr;memset(&servaddr, 0, sizeof(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_port = htons(5188);servaddr.sin_addr.s_addr = htonl(INADDR_ANY);int on = 1;if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)ERR_EXIT("setsockopt");if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)ERR_EXIT("bind");if (listen(listenfd, SOMAXCONN) < 0)ERR_EXIT("listen");std::vector<int> clients;int epollfd; //epoll_create1可以认为内部开辟了一个共享内存,用来存放感兴趣的socket的一些事件//epoll_create(能够处理的fd的个数,这个数随便填,系统资源能管多少fd,他就有多少,就能处理多大的并发)epollfd = epoll_create1(EPOLL_CLOEXEC);//man epoll_create,epoll_create1=epoll_create+fcntl+EPOLL_CLOEXEC//下面相当于对共享内存进行操作,在内核态进行操作的struct epoll_event event;event.data.fd = listenfd;event.events = EPOLLIN/* |默认是LT模式。 EPOLLET*/;//epoll_ctl把他添加到共享内存,若没有epoll_create1和epoll_ctl,则相当于操作一个数组epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event);//将listenfd添加到epollfd进行关注,以及所关注的事件添加到epollfd进行管理//把感兴趣的事件放到数组里面//初始状态里面要有关注的事件EventList events(16);//初始化一个事件列表struct sockaddr_in peeraddr;socklen_t peerlen;int connfd;int nready;while (1){//&*events.begin()第一个元素的地址//static_cast<int>(events.size()):enents大小//-1:表示等待//返回的事件都放在&*events.begin(),这仅仅是个输出参数,不需要传递关注的事件。关注的事件由epoll_ctl来传递了,由epollfd来管理,//而poll是一个输入输出参数,要把里面的数据拷贝到内核,就多了数据拷贝/*这块内容在poll中是应用层的数组&*events.begin()管理的,现在由内核取管理了,内核开辟了一个数据结构,将关注的事件放到这个数据结构中epollfd = epoll_create1(EPOLL_CLOEXEC);。。。epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event)所以,epoll的&*events.begin()只是一个输出参数,意味着:每次等待的时候,不需要每次把我们要关注的事件拷贝到内核,不需要每次从用户空间将数据拷贝到内核空间*///nready:返回的事件个数//若是poll的话,会把数组拷贝到内核,所以效率低下//返回的是活跃的fdnready = epoll_wait(epollfd, &*events.begin(), static_cast<int>(events.size()), -1);//相当于pollif (nready == -1){if (errno == EINTR)continue;ERR_EXIT("epoll_wait");}if (nready == 0) // nothing happendedcontinue;//返回的事件个数=16,说明空间不够了。就成倍扩大if ((size_t)nready == events.size())events.resize(events.size()*2);for (int i = 0; i < nready; ++i){//这里的events都是活跃的fdif (events[i].data.fd == listenfd){peerlen = sizeof(peeraddr);connfd = ::accept4(listenfd, (struct sockaddr*)&peeraddr,&peerlen, SOCK_NONBLOCK | SOCK_CLOEXEC);if (connfd == -1){if (errno == EMFILE){close(idlefd);idlefd = accept(listenfd, NULL, NULL);close(idlefd);idlefd = open("/dev/null", O_RDONLY | O_CLOEXEC);continue;}elseERR_EXIT("accept4");}std::cout<<"ip="<<inet_ntoa(peeraddr.sin_addr)<<" port="<<ntohs(peeraddr.sin_port)<<std::endl;clients.push_back(connfd);event.data.fd = connfd;event.events = EPOLLIN/* | EPOLLET*/;epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &event);//将connfd加入关注EPOLL_CTL_ADD}//处理完listenfd,就处理connfdelse if (events[i].events & EPOLLIN){connfd = events[i].data.fd;if (connfd < 0)continue;char buf[1024] = {0};int ret = read(connfd, buf, 1024);if (ret == -1)ERR_EXIT("read");if (ret == 0)//对方关闭{std::cout<<"client close"<<std::endl;close(connfd);event = events[i];epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);//剔除connfd,下次不关注clients.erase(std::remove(clients.begin(), clients.end(), connfd), clients.end());continue;}std::cout<<buf;write(connfd, buf, strlen(buf));}}}return 0;
}
这篇关于(P6)epoll:epoll的两种触发模式,select/poll/epoll对比的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!