嘉兴做微网站的公司,北京朝阳区公司,王野天津音乐广播电台图片,做网站编辑累吗目录基本使用方法step1:创建epollfdstep2:将fd绑定到epollfdstep3:调用epoll_wait检测事件epoll_wait与poll、select区别所在水平触发与边缘触发基本使用方法
step1:创建epollfd
创建一个epollfd#xff0c;若epoll_create调用成功#xff0c;则返回一个非负值的epollfd若epoll_create调用成功则返回一个非负值的epollfd否则返回-1
/* Creates an epoll instance. Returns an fd for the new instance.The size parameter is a hint specifying the number of filedescriptors to be associated with the new instance. The fdreturned by epoll_create() should be closed with close(). */
extern int epoll_create (int __size) __THROW;step2:将fd绑定到epollfd
有了epollfd之后我们有三种需求 1、将需要检测事件的其他fd绑定到这个epollfd上 2、修改一个已经绑定到epollfd的fd的事件类型 3、在不需要的时候将fd从epollfd上解绑 都需要依托函数epoll_ctl完成
/* Manipulate an epoll instance epfd. Returns 0 in case of success,-1 in case of error ( the errno variable will contain thespecific error code ) The op parameter is one of the EPOLL_CTL_*constants defined above. The fd parameter is the target of theoperation. The event parameter describes which events the calleris interested in and any associated user data. */
extern int epoll_ctl (int __epfd, int __op, int __fd,struct epoll_event *__event) __THROW;__epfd:即epollfd __op:操作类型有三种EPOLL_CTL_ADD、EPOLL_CTL_MOD、EPOLL_CTL_DEL。分别对应着在epollfd上添加、修改、移除fd当为EPOLL_CTL_DEL时__event参数忽略置NULL __fd:需要备操作的fd __event:一个epoll_event结构体的地址 具体结构如下
// 在64位操作系统下大小为8 byte
typedef union epoll_data
{void *ptr;int fd;uint32_t u32;uint64_t u64;
} epoll_data_t;struct epoll_event
{uint32_t events; /* 需要检测的fd事件标志 */epoll_data_t data; /* 用户自定义的数据*/
} __EPOLL_PACKED;返回值 调用成功返回0 调用失败返回-1通过errno错误码可以获取具体的错误原因。
step3:调用epoll_wait检测事件
/* Wait for events on an epoll instance epfd. Returns the number oftriggered events returned in events buffer. Or -1 in case oferror with the errno variable set to the specific error code. Theevents parameter is a buffer that will contain triggeredevents. The maxevents is the maximum number of events to bereturned ( usually size of events ). The timeout parameterspecifies the maximum wait time in milliseconds (-1 infinite).This function is a cancellation point and therefore not marked with__THROW. */
extern int epoll_wait (int __epfd, struct epoll_event *__events,int __maxevents, int __timeout);
__events:一个epoll_event结构数组的首地址是一个输出参数在函数调用成功后在events中存放的是与就绪事件相关的epoll_event结构体数组。 __maxevents数组元素个数 __timeout超时时间单位为ms 返回值调用成功返回有事件的fd数量若返回0表示超时。若返回-1表示调用失败。 使用示例如下 while (true) {epoll_event epollEvents[1024];int n epoll_wait(epollfd, epollEvents, 1024, 1000);if (n 0) {if (errno EINTR) {// 被信号中断 重试continue;} else {// 出错 退出break;}} else if (n 0) {// 超时继续重试continue;} else {// 处理事件for (size_t i 0; i n; i) {if (epollEvents[i].events EPOLLIN) {// 处理可读事件} else if (epollEvents[i].events EPOLLOUT) {// 处理可写事件} else if (epollEvents[i].events EPOLLERR) {// 处理出错事件}}}}epoll_wait与poll、select区别所在
在第二讲中演示了select的基本使用方式C网络编程快速入门二Linux下使用select演示简单服务端程序 select和epoll底层机制一样所以这里只看select。 可以发现调用完select之后需要在原来的clientfds数组中遍历然后加条件判断是否是有事件的。 而epoll_wait调用完之后是直接返回一个筛选过后的有事件的events数组。 所以 在fd数量比较多但是某段时间内的就绪事件fd数量较少时epoll_wait函数更加高效。 也就是epoll模型更适合用在socket连接数量较大而活跃的连接较少的情景下
水平触发与边缘触发
epoll具有两种模式边缘触发模式Edge TriggerET和水平触发模式Level TriggerLT。 区别在于 1、LT一个事件只要有就会一直触发 2、ET一个事件从无到有才会触发 以socket读事件为例 水平模式下只要socket上有未读完的数据就会一直产生EPOLLIN事件。 边缘模式下socket上每新来一次数据就会触发一次如果上一次触发后未将socket上的数据读完也不会再触发除非再新来一次数据。 以socket写事件为例 水平模式下只要socket上TCP窗口一直不饱和就会一直触发EPOLLOUT事件。 边缘模式下只有TCP窗口由不饱和变成饱和 或者 再一次变成不饱和才会触发EPOLLOUT事件。 这对于编程的启示是 1、对于非阻塞socket如果epoll使用边缘模式检测事件可读那么一旦触发一定要一次性把socket上数据收取干净即循环调用recv函数直到recv出错
bool recvEtMode()
{// 每次只收取256个字节char buf[256];while (true) {int nRecv ::recv(clientfd, buf, 256, 0);if (nRecv -1) {if (errno EWOULDBLOCK) {return true;} else if (errno EINTR) {continue;} else {return false;}}else if (nRecv 0) {// 对端关闭了socketreturn false;} else {inputBuffer.add(buf, (size_t)nRecv);}}return true;
}2、如果是水平模式可以根据业务一次性收取固定字节数 下面总结一下两者在编码上需要注意的地方 1、LT模式下读事件触发后可以按需收取想要的字节数不用把本次数据收取干净 ET模式下读事件必须把数据收取干净因为我们不一定再有机会收取数据了。 2、LT模式下不需要写事件时一定要及时移除避免不必要地触发且浪费CPU资源。 ET模式下写事件触发后如果还需要下一次的写事件触发来驱动任务例如发送上次剩余的数据则我们需要继续注册一次检测可写事件 3、LT会导致多次触发ET优点是触发次数少