欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IO多路復(fù)用之epoll全面總結(jié)(必看篇)

 更新時(shí)間:2016年12月24日 10:23:32   投稿:jingxian  
下面小編就為大家?guī)硪黄狪O多路復(fù)用之epoll全面總結(jié)(必看篇)。小編覺得挺不錯(cuò)的?,F(xiàn)在就分享給大家。也給大家做個(gè)參考。一起跟隨小編過來看看吧

1、基本知識(shí)

epoll是在2.6內(nèi)核中提出的,是之前的select和poll的增強(qiáng)版本。相對(duì)于select和poll來說,epoll更加靈活,沒有描述符限制。epoll使用一個(gè)文件描述符管理多個(gè)描述符,將用戶關(guān)系的文件描述符的事件存放到內(nèi)核的一個(gè)事件表中,這樣在用戶空間和內(nèi)核空間的copy只需一次。

2、epoll接口

epoll操作過程需要三個(gè)接口,分別如下:

#include <sys/epoll.h>
int epoll_create(int size);
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);

(1) int epoll_create(int size);

創(chuàng)建一個(gè)epoll的句柄,size用來告訴內(nèi)核這個(gè)監(jiān)聽的數(shù)目一共有多大。這個(gè)參數(shù)不同于select()中的第一個(gè)參數(shù),給出最大監(jiān)聽的fd+1的值。需要注意的是,當(dāng)創(chuàng)建好epoll句柄后,它就是會(huì)占用一個(gè)fd值,在linux下如果查看/proc/進(jìn)程id/fd/,是能夠看到這個(gè)fd的,所以在使用完epoll后,必須調(diào)用close()關(guān)閉,否則可能導(dǎo)致fd被耗盡。

(2)int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

epoll的事件注冊(cè)函數(shù),它不同與select()是在監(jiān)聽事件時(shí)告訴內(nèi)核要監(jiān)聽什么類型的事件epoll的事件注冊(cè)函數(shù),它不同與select()是在監(jiān)聽事件時(shí)告訴內(nèi)核要監(jiān)聽什么類型的事件,而是在這里先注冊(cè)要監(jiān)聽的事件類型。第一個(gè)參數(shù)是epoll_create()的返回值,第二個(gè)參數(shù)表示動(dòng)作,用三個(gè)宏來表示:

EPOLL_CTL_ADD:注冊(cè)新的fd到epfd中;
EPOLL_CTL_MOD:修改已經(jīng)注冊(cè)的fd的監(jiān)聽事件;
EPOLL_CTL_DEL:從epfd中刪除一個(gè)fd;

第三個(gè)參數(shù)是需要監(jiān)聽的fd,第四個(gè)參數(shù)是告訴內(nèi)核需要監(jiān)聽什么事,struct epoll_event結(jié)構(gòu)如下:

struct epoll_event {
 __uint32_t events; /* Epoll events */
 epoll_data_t data; /* User data variable */
};

events可以是以下幾個(gè)宏的集合:

EPOLLIN :表示對(duì)應(yīng)的文件描述符可以讀(包括對(duì)端SOCKET正常關(guān)閉);

EPOLLOUT:表示對(duì)應(yīng)的文件描述符可以寫;

EPOLLPRI:表示對(duì)應(yīng)的文件描述符有緊急的數(shù)據(jù)可讀(這里應(yīng)該表示有帶外數(shù)據(jù)到來);

EPOLLERR:表示對(duì)應(yīng)的文件描述符發(fā)生錯(cuò)誤;

EPOLLHUP:表示對(duì)應(yīng)的文件描述符被掛斷;

EPOLLET: 將EPOLL設(shè)為邊緣觸發(fā)(Edge Triggered)模式,這是相對(duì)于水平觸發(fā)(Level Triggered)來說的。

EPOLLONESHOT:只監(jiān)聽一次事件,當(dāng)監(jiān)聽完這次事件之后,如果還需要繼續(xù)監(jiān)聽這個(gè)socket的話,需要再次把這個(gè)socket加入到EPOLL隊(duì)列里

(3) int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);

等待事件的產(chǎn)生,類似于select()調(diào)用。參數(shù)events用來從內(nèi)核得到事件的集合,maxevents告之內(nèi)核這個(gè)events有多大,這個(gè)maxevents的值不能大于創(chuàng)建epoll_create()時(shí)的size,參數(shù)timeout是超時(shí)時(shí)間(毫秒,0會(huì)立即返回,-1將不確定,也有說法說是永久阻塞)。該函數(shù)返回需要處理的事件數(shù)目,如返回0表示已超時(shí)。

3、工作模式

epoll對(duì)文件描述符的操作有兩種模式:LT(level trigger)和ET(edge trigger)。LT模式是默認(rèn)模式,LT模式與ET模式的區(qū)別如下:

LT模式:當(dāng)epoll_wait檢測(cè)到描述符事件發(fā)生并將此事件通知應(yīng)用程序,應(yīng)用程序可以不立即處理該事件。下次調(diào)用epoll_wait時(shí),會(huì)再次響應(yīng)應(yīng)用程序并通知此事件。

ET模式:當(dāng)epoll_wait檢測(cè)到描述符事件發(fā)生并將此事件通知應(yīng)用程序,應(yīng)用程序必須立即處理該事件。如果不處理,下次調(diào)用epoll_wait時(shí),不會(huì)再次響應(yīng)應(yīng)用程序并通知此事件。

ET模式在很大程度上減少了epoll事件被重復(fù)觸發(fā)的次數(shù),因此效率要比LT模式高。epoll工作在ET模式的時(shí)候,必須使用非阻塞套接口,以避免由于一個(gè)文件句柄的阻塞讀/阻塞寫操作把處理多個(gè)文件描述符的任務(wù)餓死。

4、測(cè)試程序

編寫一個(gè)服務(wù)器回射程序echo,練習(xí)epoll過程。

服務(wù)器代碼如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <sys/types.h>

#define IPADDRESS  "127.0.0.1"
#define PORT    8787
#define MAXSIZE   1024
#define LISTENQ   5
#define FDSIZE   1000
#define EPOLLEVENTS 100

//函數(shù)聲明
//創(chuàng)建套接字并進(jìn)行綁定
static int socket_bind(const char* ip,int port);
//IO多路復(fù)用epoll
static void do_epoll(int listenfd);
//事件處理函數(shù)
static void
handle_events(int epollfd,struct epoll_event *events,int num,int listenfd,char *buf);
//處理接收到的連接
static void handle_accpet(int epollfd,int listenfd);
//讀處理
static void do_read(int epollfd,int fd,char *buf);
//寫處理
static void do_write(int epollfd,int fd,char *buf);
//添加事件
static void add_event(int epollfd,int fd,int state);
//修改事件
static void modify_event(int epollfd,int fd,int state);
//刪除事件
static void delete_event(int epollfd,int fd,int state);

int main(int argc,char *argv[])
{
  int listenfd;
  listenfd = socket_bind(IPADDRESS,PORT);
  listen(listenfd,LISTENQ);
  do_epoll(listenfd);
  return 0;
}

static int socket_bind(const char* ip,int port)
{
  int listenfd;
  struct sockaddr_in servaddr;
  listenfd = socket(AF_INET,SOCK_STREAM,0);
  if (listenfd == -1)
  {
    perror("socket error:");
    exit(1);
  }
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  inet_pton(AF_INET,ip,&servaddr.sin_addr);
  servaddr.sin_port = htons(port);
  if (bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) == -1)
  {
    perror("bind error: ");
    exit(1);
  }
  return listenfd;
}

static void do_epoll(int listenfd)
{
  int epollfd;
  struct epoll_event events[EPOLLEVENTS];
  int ret;
  char buf[MAXSIZE];
  memset(buf,0,MAXSIZE);
  //創(chuàng)建一個(gè)描述符
  epollfd = epoll_create(FDSIZE);
  //添加監(jiān)聽描述符事件
  add_event(epollfd,listenfd,EPOLLIN);
  for ( ; ; )
  {
    //獲取已經(jīng)準(zhǔn)備好的描述符事件
    ret = epoll_wait(epollfd,events,EPOLLEVENTS,-1);
    handle_events(epollfd,events,ret,listenfd,buf);
  }
  close(epollfd);
}

static void
handle_events(int epollfd,struct epoll_event *events,int num,int listenfd,char *buf)
{
  int i;
  int fd;
  //進(jìn)行選好遍歷
  for (i = 0;i < num;i++)
  {
    fd = events[i].data.fd;
    //根據(jù)描述符的類型和事件類型進(jìn)行處理
    if ((fd == listenfd) &&(events[i].events & EPOLLIN))
      handle_accpet(epollfd,listenfd);
    else if (events[i].events & EPOLLIN)
      do_read(epollfd,fd,buf);
    else if (events[i].events & EPOLLOUT)
      do_write(epollfd,fd,buf);
  }
}
static void handle_accpet(int epollfd,int listenfd)
{
  int clifd;
  struct sockaddr_in cliaddr;
  socklen_t cliaddrlen;
  clifd = accept(listenfd,(struct sockaddr*)&cliaddr,&cliaddrlen);
  if (clifd == -1)
    perror("accpet error:");
  else
  {
    printf("accept a new client: %s:%d\n",inet_ntoa(cliaddr.sin_addr),cliaddr.sin_port);
    //添加一個(gè)客戶描述符和事件
    add_event(epollfd,clifd,EPOLLIN);
  }
}

static void do_read(int epollfd,int fd,char *buf)
{
  int nread;
  nread = read(fd,buf,MAXSIZE);
  if (nread == -1)
  {
    perror("read error:");
    close(fd);
    delete_event(epollfd,fd,EPOLLIN);
  }
  else if (nread == 0)
  {
    fprintf(stderr,"client close.\n");
    close(fd);
    delete_event(epollfd,fd,EPOLLIN);
  }
  else
  {
    printf("read message is : %s",buf);
    //修改描述符對(duì)應(yīng)的事件,由讀改為寫
    modify_event(epollfd,fd,EPOLLOUT);
  }
}

static void do_write(int epollfd,int fd,char *buf)
{
  int nwrite;
  nwrite = write(fd,buf,strlen(buf));
  if (nwrite == -1)
  {
    perror("write error:");
    close(fd);
    delete_event(epollfd,fd,EPOLLOUT);
  }
  else
    modify_event(epollfd,fd,EPOLLIN);
  memset(buf,0,MAXSIZE);
}

static void add_event(int epollfd,int fd,int state)
{
  struct epoll_event ev;
  ev.events = state;
  ev.data.fd = fd;
  epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&ev);
}

static void delete_event(int epollfd,int fd,int state)
{
  struct epoll_event ev;
  ev.events = state;
  ev.data.fd = fd;
  epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,&ev);
}

static void modify_event(int epollfd,int fd,int state)
{
  struct epoll_event ev;
  ev.events = state;
  ev.data.fd = fd;
  epoll_ctl(epollfd,EPOLL_CTL_MOD,fd,&ev);
}

客戶端也用epoll實(shí)現(xiàn),控制STDIN_FILENO、STDOUT_FILENO、和sockfd三個(gè)描述符,程序如下所示:

#include <netinet/in.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>

#define MAXSIZE   1024
#define IPADDRESS  "127.0.0.1"
#define SERV_PORT  8787
#define FDSIZE    1024
#define EPOLLEVENTS 20

static void handle_connection(int sockfd);
static void
handle_events(int epollfd,struct epoll_event *events,int num,int sockfd,char *buf);
static void do_read(int epollfd,int fd,int sockfd,char *buf);
static void do_read(int epollfd,int fd,int sockfd,char *buf);
static void do_write(int epollfd,int fd,int sockfd,char *buf);
static void add_event(int epollfd,int fd,int state);
static void delete_event(int epollfd,int fd,int state);
static void modify_event(int epollfd,int fd,int state);

int main(int argc,char *argv[])
{
  int         sockfd;
  struct sockaddr_in servaddr;
  sockfd = socket(AF_INET,SOCK_STREAM,0);
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(SERV_PORT);
  inet_pton(AF_INET,IPADDRESS,&servaddr.sin_addr);
  connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
  //處理連接
  handle_connection(sockfd);
  close(sockfd);
  return 0;
}


static void handle_connection(int sockfd)
{
  int epollfd;
  struct epoll_event events[EPOLLEVENTS];
  char buf[MAXSIZE];
  int ret;
  epollfd = epoll_create(FDSIZE);
  add_event(epollfd,STDIN_FILENO,EPOLLIN);
  for ( ; ; )
  {
    ret = epoll_wait(epollfd,events,EPOLLEVENTS,-1);
    handle_events(epollfd,events,ret,sockfd,buf);
  }
  close(epollfd);
}

static void
handle_events(int epollfd,struct epoll_event *events,int num,int sockfd,char *buf)
{
  int fd;
  int i;
  for (i = 0;i < num;i++)
  {
    fd = events[i].data.fd;
    if (events[i].events & EPOLLIN)
      do_read(epollfd,fd,sockfd,buf);
    else if (events[i].events & EPOLLOUT)
      do_write(epollfd,fd,sockfd,buf);
  }
}

static void do_read(int epollfd,int fd,int sockfd,char *buf)
{
  int nread;
  nread = read(fd,buf,MAXSIZE);
    if (nread == -1)
  {
    perror("read error:");
    close(fd);
  }
  else if (nread == 0)
  {
    fprintf(stderr,"server close.\n");
    close(fd);
  }
  else
  {
    if (fd == STDIN_FILENO)
      add_event(epollfd,sockfd,EPOLLOUT);
    else
    {
      delete_event(epollfd,sockfd,EPOLLIN);
      add_event(epollfd,STDOUT_FILENO,EPOLLOUT);
    }
  }
}

static void do_write(int epollfd,int fd,int sockfd,char *buf)
{
  int nwrite;
  nwrite = write(fd,buf,strlen(buf));
  if (nwrite == -1)
  {
    perror("write error:");
    close(fd);
  }
  else
  {
    if (fd == STDOUT_FILENO)
      delete_event(epollfd,fd,EPOLLOUT);
    else
      modify_event(epollfd,fd,EPOLLIN);
  }
  memset(buf,0,MAXSIZE);
}

static void add_event(int epollfd,int fd,int state)
{
  struct epoll_event ev;
  ev.events = state;
  ev.data.fd = fd;
  epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&ev);
}

static void delete_event(int epollfd,int fd,int state)
{
  struct epoll_event ev;
  ev.events = state;
  ev.data.fd = fd;
  epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,&ev);
}

static void modify_event(int epollfd,int fd,int state)
{
  struct epoll_event ev;
  ev.events = state;
  ev.data.fd = fd;
  epoll_ctl(epollfd,EPOLL_CTL_MOD,fd,&ev);
}

5、測(cè)試結(jié)果

以上就是小編為大家?guī)淼腎O多路復(fù)用之epoll全面總結(jié)(必看篇)全部?jī)?nèi)容了,希望大家多多支持腳本之家~

相關(guān)文章

最新評(píng)論