epoll对文件描述符的操作有两种模式:LT模式和ET模式。
LT模式(普通模式):也叫水平触发。描述符上有数据就绪,如果用户没有处理完,可以反复提醒,当下一轮IO函数执行时会继续提醒用户该描述符上有数据,直到用户将数据读完为止。
ET模式(高效模式):也叫边沿触发。描述符上有数据就绪,如果用户把数据没有处理或没处理完,也只提醒一次,下一轮IO函数执行时不会提醒,除非有新数据到达。
一、LT模式
ltepoll.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <errno.h>
//ltepoll
#define MAXFD 10
void epoll_add(int epfd, int fd)
{
struct epoll_event ev;
//ev.events = EPOLLIN;
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = fd;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1)
{
perror("epoll ctl error");
}
}
void epoll_del(int epfd, int fd)
{
if(epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) == -1)
{
perror("epoll ctl del error");
}
}
int main()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(sockfd != -1);
struct sockaddr_in saddr, caddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.s