LT和ET模式
epoll对文件描述符的操作有两种形式:LT(Level Trigger,电平触发)和ET(Edge Trigger,边沿触发)模式。LT模式是默认的工作模式,这种模式下epoll相当于一个效率较高的poll。当往epoll内核事件表中注册一个文件描述符上的EPOLLIN事件时,epoll将以ET模式来操作该文件描述符,ET模式是epoll的高效工作模式。
对于采用LT工作模式的文件描述符,当epoll_wait检测到其上有事件发生并将此事件通知应用程序后,应用程序可以不立即处理该事件。这样,当应用程序下一次调用epoll_wait时,epoll_wait还会再次向应用程序通告此事件,直到该事件被处理。而对于采用ET模式的文件描述符,当epoll_wait检测到其上有事件发生并将此事件通知给应用程序后,应用程序必须立即处理该事件,因为后续的epoll_wait调用将不再向应用程序通知这一事件。可见,ET模式在很大程度上降低了同一个epoll事件被反复触发的次数,因此效率要比LT模式高。
ser.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/epoll.h>
#define SIZE 100
int sockfd_create() //创建网络套接子
{
int sockfd=socket(AF_INET,SOCK_STREAM,0);
assert(sockfd!=-1);
struct sockaddr_in ser;
memset(&ser,0,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(7100);
ser.sin_addr.s_addr=inet_addr("127.0.0.1");
int res=bind(sockfd,(struct sockaddr*)(&ser),sizeof(ser));
assert(res!=-1);
listen(sockfd,5);
return sockfd;
}
void SetNonBlock(int fd) //将文件描述符设置为非阻塞的
{
int old_option=fcntl(fd,F_GETFL);
int new_option=old_option | O_NONBLOCK;
fcntl(fd,F_SETFL,new_option);
}
void et(int fd,int epollfd,struct epoll_event event)
{
if(event.events & EPOLLRDHUP)
{
epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,NULL);
printf("%d's connnection is broken now\n",fd);
close(fd);
}
else if(event.events & EPOLLIN)
{
printf("%d's data is:",fd);
while(1) //为了将数据一次性处理完毕
{
char buff[128]={0};
int n=recv(fd,buff,5,0);
if(n<=0)
{
if((errno == EAGAIN) || (errno==EWOULDBLOCK)) //对于非阻塞IO,下面的条件成立表示数据已经全部读取完毕,此后,epoll就能再次触发sockfd上面的EPOLLIN事件,以驱动下一次读操作
{
break;
}
}
printf("%s",buff);
}
printf("\n");
send(fd,"OK",2,0);
}
else
{
printf("Something else happened\n");
}
}
void Handle_Link(int n,int sockfd,int epollfd,struct epoll_event events[])
{
int i=0;
int fd=events[i].data.fd;
for(;i<n;i++)
{
if(fd==sockfd)
{
struct sockaddr_in cli;
socklen_t client=sizeof(cli);
int c=accept(sockfd,(struct sockaddr*)(&cli),&client);
SetNonBlock(c);
if(c<=0)
{
printf("Error\n");
continue;
}
else
{
struct epoll_event event;
event.events=EPOLLIN | EPOLLRDHUP | EPOLLET;
event.data.fd=c;
epoll_ctl(epollfd,EPOLL_CTL_ADD,c,&event);
}
}
else
{
et(fd,epollfd,events[i]);
}
}
}
int main()
{
int sockfd=sockfd_create();
int epollfd=epoll_create(5);
struct epoll_event event;
event.events=EPOLLIN;
event.data.fd=sockfd;
epoll_ctl(epollfd,EPOLL_CTL_ADD,sockfd,&event);
while(1)
{
struct epoll_event events[SIZE];
int n=epoll_wait(epollfd,events,SIZE,-1);
if(n<=0)
{
printf("Error\n");
continue;
}
Handle_Link(n,sockfd,epollfd,events);
printf("Only one time\n"); //若只打印一次,表明数据一次性处理完
}
}
cli.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<assert.h>
int main()
{
int sockfd=socket(PF_INET,SOCK_STREAM,0);
assert(sockfd != -1);
struct sockaddr_in ser,cli;
ser.sin_family=AF_INET;
ser.sin_port=htons(7100);
ser.sin_addr.s_addr=inet_addr("127.0.0.1");
connect(sockfd,(struct sockaddr*)(&ser),sizeof(ser));
while(1)
{
printf("please input: ");
fflush(stdout);
char buff[128]={0};
fgets(buff,128,stdin);
buff[strlen(buff)-1] = '\0';
send(sockfd,buff,127,0);
if(strcmp(buff,"end")==0)
{
break;
}
char recvbuff[128]={0};
recv(sockfd,recvbuff,127,0);
printf("%s\n",recvbuff);
}
close(sockfd);
}
执行结果: