内核事件表
epoll是Linux特有的I/O复用函数。它在实现和使用上与select、poll有很大差异。首先,epoll使用一组函数来完成任务,而不是一个函数。其次,epoll吧用户关心的文件描述符上的事件放在内核里的一个时间表中,从而无需像select和poll那样每次调用都要重复传入文件描述符或事件集。但epoll需要使用一个额外的文件描述符;来唯一标识内核中的这个事件表。这个文件描述符使用如下epoll——create函数来创建:
epoll_create
#include<sys/epoll.h>
int epoll_create(int size);
size参数现在并不起任何作用,只是给内核一个提示,告诉事件表需要多大。该函数返回的文件描述符将用作其他epoll系统调用函数的第一个参数,以指定要访问的内核事件表。
下面的函数来操作epoll的内核事件表:
epoll_ctl
#include<sys/epoll.h>
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
(1)fd参数是操作的文件描述符
(2)op参数则指定操作类型,操作类型有如下的三种:
EPOLL_CTL_ADD,往事件表中注册fd上的事件
EPOLL_CTL_MOD,修改fd上的注册事件
EPOLL_CTL_DEL,删除fd上的注册事件
(3)event参数指定事件,它是epoll_event结构指针类型。epoll_event的定义如下:
struct epoll_event
{
_uint32_t events; //epoll事件
epoll_data_t data; //用户数据
};
其中events成员描述事件类型。epoll支持的事件类型基本与poll相同。表示epoll事件类型的宏是在poll对应的宏前加上“E”,比如epoll的数据可读事件是EPOLLIN。但epoll有两个额外的事件类型——EPOLLET和EPOLLONESHOT。它们对于epoll的高效运作非常关键。data成员用于存储用户数据,其类型epoll_data_t的定义如下:
typedef union epoll_data
{
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
}epoll_data_t;
epoll_data_t是一个联合体,其4个成员中使用最多的是fd,它指定事件从属的目标文件描述符。ptr成员可用来指定与fd相关的用户数据。但由于epoll_data_t是一个联合体,我们不能同时使用其ptr成员和fd成员,因此。如果要将文件描述符和用户数据关联起来,以实现快速的数据访问,只能使用其他手段,比如放弃使用epoll_data_t的fd成员,而在ptr指向的用户数据中包括fd。
epoll_ctl成功时返回0,失败时返回-1并设置errno。
epoll_wait
epoll系列系统调用函数的主要接口是epoll_wait函数。它在一段超时时间内等待一组文件描述符上的事件,其原型为:
#include<sys/epoll.h>
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
该函数成功时返回就绪的文件描述符的个数,失败时返回-1并设置errno。
(1)timeout参数的含义是指定epoll的超时值,单位是毫秒。当timeout为-1时,epoll调用将永远阻塞,直到某个事件发生;当timeout为0时,epoll调用将立即返回。
(2)maxevents参数指定最多监听多少个事件,它必须大于0.
(3)epoll_wait函数如果检测到事件,就将所有就绪的时间从内核事件表中复制到它的第二个参数events指向的数组中。这个数组只用来输出epoll_wait检测到的就绪事件,而不像select和poll的数组参数那样既用于传入用户注册的事件,又用于输出内核检测到的就绪事件。这就极大地提高了应用程序索引就绪文件描述符的效率。
ser.c
#define _GNU_SOURCE //想要使用EPOLLRDHUP必须引进的头文件
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<assert.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/epoll.h>
#define SIZE 100
int main()
{
int sockfd=socket(AF_INET,SOCK_STREAM,0); //网络的TCP连接
assert(sockfd!=-1);
struct sockaddr_in ser,cli;
memset(&ser,0,sizeof(ser));
ser.sin_family=AF_INET;
ser.sin_port=htons(8100);
ser.sin_addr.s_addr=inet_addr("127.0.0.1");
int ret=bind(sockfd,(struct sockaddr*)(&ser),sizeof(ser));
assert(ret!=-1);
listen(sockfd,5);
int fds=epoll_create(5); //创建内核事件表,5并不起作用,内核红黑树,可拓展
struct epoll_event event;
event.events=EPOLLIN; //sockfd触发的事件设为可读
event.data.fd=sockfd; //将sockfd添加到内核事件表中
epoll_ctl(fds,EPOLL_CTL_ADD,sockfd,&event);
while(1)
{
struct epoll_event events[SIZE]; //内核填充就绪的文件描述符和事件
int n=epoll_wait(fds,events,SIZE,-1); //找出就绪文件描述符,并返回就绪文件描述的个数
if(n<0)
{
printf("Error\n");
continue;
}
int i=0;
for(;i<n;i++)
{
int fd=events[i].data.fd;
if(events[i].events & EPOLLIN)
{
if(events[i].events & EPOLLRDHUP) //断开连接
{
epoll_ctl(fds,EPOLL_CTL_DEL,fd,NULL); //删除fd
close(fd);
}
else if(fd==sockfd) //建立连接
{
int client=sizeof(cli);
int c=accept(sockfd,(struct sockaddr*)(&cli),&client);
if(c<=0)
{
printf("Error\n");
continue;
}
else
{
event.events=EPOLLIN | EPOLLRDHUP; //将c也要加入内核事件表中,并将其所触发的事件设为可读或断开连接
event.data.fd=c;
epoll_ctl(fds,EPOLL_CTL_ADD,c,&event); //将c加入到内核事件表中
}
}
else //接受数据
{
char buff[128]={0};
int n=recv(fd,buff,127,0);
printf("%d: %s\n",fd,buff);
send(fd,"OK",2,0);
}
}
}
}
}
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(8100);
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);
}
运行结果: