Linux编程之Epoll高并发

Posted znwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux编程之Epoll高并发相关的知识,希望对你有一定的参考价值。

网络上所有资料都说epoll是高并发、单线程、IO重叠服用的首选架构,比select和poll性能都要好,特别是在有大量不活跃连接的情况下。具体原理就不阐述了,下面说说使用。

具有有三个函数:

#include <sys/epoll.h>

 

1、int epoll_create ( int size );

size是epoll要监视的fd的规模。

 

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

(1)epfd:epoll_create的返回值。

(2)op  指定操作类型:

     EPOLL_CTL_ADD:往事件表中注册fd上的事件

     EPOLL_CTL_MOD:修改fd上的注册事件

     EPOLL_CTL_DEL:删除fd上的注册事件

(3)fd:要操作的文件描述符(socket)

 (4)event:指定要监听fd的什么事情。它是epoll_event结构指针类型:

struct epoll_event

{

     __unit32_t events;    // epoll事件

     epoll_data_t data;     // 用户数据

};

events:描述事件类型。events可以是以下几个宏的集合:

 EPOLLIN :表示对应的文件描述符可以读(包括对端SOCKET正常关闭);
EPOLLOUT:表示对应的文件描述符可以写;

 EPOLLPRI:表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);
EPOLLERR:表示对应的文件描述符发生错误;

EPOLLHUP:表示对应的文件描述符被挂断;

 EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。

EPOLLONESHOT:只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把这个socket加入到EPOLL队列里。

data成员:其中data.fd常用来装要操作的fd。

 

typedef union epoll_data

{

     void *ptr;

      int fd;

      __uint32_t u32;

      __uint64_t u64;

  } epoll_data_t;

 

 

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

epoll_wait的工作流程是:等待,如果有epoll事件发生立刻返回,否则等待timeout毫秒。返回时拷贝要处理的事件到events指向的数组,返回就绪的文件描述符的个数,失败时返回-1并设置errno。 

timeout:指定epoll的超时时间,单位是毫秒。当timeout为-1是,epoll_wait调用将永远阻塞,直到某个事件发生。当timeout为0时,epoll_wait调用将立即返回。 

maxevents:指定最多监听多少个事件。如果events指向的是20个单元的结构体数组,那么就设置maxevents为20。 

events: events指向的数组中将拷贝所有就绪的事件,从内核事件表中。events的成员是struct epoll_event类型,一般包含events(其值是如:EPOLLIN、EPOLLOUT等)。还有一个是data.fd,包含拷贝的事件对应的socket,如果是服务器监听socket,说明是有用户连接到这个socket,如果是其他已经连接好的socket,说明是有数据要发送或者接收。

如果事件数组中的数据得到处理,那么内核事件表中的事件就会被删除,下次wait就没有那些socket的事件了。 

 

实例代码:

epoll.c

#include <stdio.h>

#include <sys/epoll.h>

#include <sys/socket.h>

#include <stdlib.h>

#include <netinet/in.h>                 //包含sockaddr_in定义

#include <errno.h>

#include <string.h>                     //包含memset strncpy

 

 

int main(int argc,char* argv[])   //主函数

{

 

      int epfd1;int result;

      int server_len,client_len;

      int server_sockfd,client_sockfd;

      struct sockaddr_in server_address;       //定义在 <netinet/in.h>

      struct sockaddr_in client_address;

      struct epoll_event ev1;

      struct epoll_event ev[20];

      int epollreturn;

      int i,j,res;

      int sockfd;

      char ch = ‘0‘;

      char buff[1024];

     

      server_address.sin_family = AF_INET;

      server_address.sin_addr.s_addr = inet_addr("192.168.131.129");

      server_sockfd = socket(AF_INET,SOCK_STREAM,0);

      server_address.sin_port = htons(9734);

      server_len = sizeof(server_address);

      client_len = sizeof(client_address);

     

      result = bind(server_sockfd,(struct sockaddr*)&server_address,server_len);

      if(result!=0)

      {

           printf("bind failed ");

           exit(1);                             //在stdlib.h

      }   

 

     

     

      epfd1 = epoll_create(10000);

      ev1.data.fd = server_sockfd;

      ev1.events = EPOLLIN;

      /*

      printf("%08x ",EPOLLIN);

      printf("%08x ",EPOLLOUT);

      printf("%08x ",EPOLLPRI);

      printf("%08x ",EPOLLERR);

      printf("%08x ",EPOLLHUP);

      printf("%08x ",EPOLLET);

      printf("%08x ",EPOLLONESHOT);

      */

      epoll_ctl(epfd1,EPOLL_CTL_ADD,server_sockfd,&ev1);

     

     

     

      result = listen(server_sockfd,5);

      if(result!=0)

      {

           printf("listen failed ");

           exit(1);

      }

     

      memset(buff,0,1024);

      strncpy(buff,"this is server",14);

     

      for(;;)

      {   

           epollreturn  = epoll_wait(epfd1,ev,20,4000);

           printf("epollreturn is %d ",epollreturn);

           if(epollreturn>0)

           {

                 for(i=0;i<epollreturn;i++)

                 {

                     

                     if(ev[i].data.fd==server_sockfd)//如果新监测到一个SOCKET用户连接到了绑定的SOCKET端口,建立新的连接。

                      {

                           

                            client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);//没有计算client_len的值,会导致accept返回-1

                            printf("accept one client,socket:%d ",client_sockfd);                        

                            ev1.data.fd=client_sockfd;

                            ev1.events=EPOLLIN;

                            epoll_ctl(epfd1,EPOLL_CTL_ADD,client_sockfd,&ev1);

                           

                            //ev1.data.fd=client_sockfd;

                            //ev1.events=EPOLLOUT;

                            //epoll_ctl(epfd1,EPOLL_CTL_ADD,client_sockfd,&ev1); //注册

                      }

                      else if(ev[i].events&EPOLLIN)//如果是已经连接的用户,收到数据,那么进行读入。

                      {

                            sockfd = ev[i].data.fd;

                            if (sockfd < 0)

                            {

                                  printf("EPOLLIN,sockfd < 0 ");

                                  continue;

                            }

                            res = recv(sockfd,&ch,1,0);

                                                       

                            if (res < 0)

                            {

                                 if (errno == ECONNRESET)

                                  {

                                       close(sockfd);

                                       ev[i].data.fd = -1;

                                       printf("EPOLLIN,res<0,errno == ECONNRESET ");

                                  }

                                  else

                                  printf("EPOLLIN,recv error,res <0 ");

                            }

                            else if (res == 0)

                            {

                                  close(sockfd);                    //个测试发现关闭socket,epoll队列中就不再监视这个socket了,似乎不需要删除监视

                                  ev[i].data.fd = -1;

                                  printf("EPOLLIN,res == 0 ");

                                  ev1.data.fd=sockfd;

                                  ev1.events=EPOLLIN;

                                  epoll_ctl(epfd1,EPOLL_CTL_DEL,sockfd,&ev1);

                            }

                            else

                            {

                                  printf("EPOLLIN,receive one char %c,socket is %d ",ch,sockfd);

                            }

 

                            ev1.data.fd=sockfd; 

                            ev1.events=EPOLLOUT;

                            epoll_ctl(epfd1,EPOLL_CTL_MOD,sockfd,&ev1);

                            /**/

                      }

                     

                      else if(ev[i].events&EPOLLOUT) // 监测数据发送的原理是,对端调用recv,通知到服务器端,通知epoll,这个socket有数据要发。

                      {

                            sockfd = ev[i].data.fd;

                            res = send(sockfd,buff,102,0);

                            if(res==-1)

                            {

                                  printf("send error,res is %d ",res);

                                  close(sockfd);

                                  ev1.data.fd=sockfd;

                                  ev1.events=EPOLLOUT;

                                  epoll_ctl(epfd1,EPOLL_CTL_DEL,sockfd,&ev1);

                            }

                           

                           

                            ev1.data.fd=sockfd; //设置用于读操作的文件描述符

                            ev1.events=EPOLLIN; //设置用于注测的读操作事件

                            epoll_ctl(epfd1,EPOLL_CTL_MOD,sockfd,&ev1);  //修改sockfd上要处理的事件为EPOLIN

                           

                      }

                

                

                 }

          

          

           }

          

     

     

      }

      return 0;

}

 

 

client2.c

#include <sys/types.h>

#include <sys/socket.h>

#include <stdio.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>                     //包含memset strncpy

 

int main(int argc,char* argv[])                         //这是网络套接字,比unix套接字的测试要简单

{                                                       //测试方法,把client2和server2放在相同或者不同文件夹下都可以

      int sockfd;                                         // 一个终端运行./client2  另外一个终端运行  ./server2 就可以看到结果了

      int len,i,res;

      struct sockaddr_in address;

      int result;

      char ch = ‘A‘;

      char buff[1024];

     

      sockfd = socket(AF_INET,SOCK_STREAM,0);              //奇怪,一个client运行多个版本,每次获取的居然是同一个socket。改个名字也不行

      printf("socket is %d ",sockfd);

      address.sin_family = AF_INET;

      address.sin_addr.s_addr = inet_addr("192.168.131.129");

      address.sin_port = htons(9734);

      len = sizeof(address);

     

      result = connect(sockfd,(struct sockaddr*)&address,len);

      if(result == -1)

      {

           perror("oops:client1");

           exit(-1);

      }

     

      memset(buff,0,1024);

      i = 0;

      //for(i=0;i<10;i++)

      for(;;)

      {  

           res = send(sockfd,&ch,1,0);

           if(res==-1)

           {

                 printf("send error,res is %d,exiting program ",res);

                 close(sockfd);

                 return(-1);

           }

           /**/ 

           i++;

            memset(buff,0,102);

           res = recv(sockfd,buff,102,0); 

          

            //if((res==-1)||(res==0))

           if(res==-1)                       

           {

                 printf("recv error,res is %d,exiting program ",res);

                 close(sockfd);

                 return(-1);

           }

           printf("socket:%d,buff is %s,i is %d ",sockfd,buff,i);

           /**/

      }

      //scanf("%s",buff);

      printf("exiting program ");

      close(sockfd);  

      return 0;

}

 



以上是关于Linux编程之Epoll高并发的主要内容,如果未能解决你的问题,请参考以下文章

Linux网络编程——多路复用之epoll

Linux 网络编程八(epoll应用--大并发处理)

再探“高并发利器”epoll:看了一下源码

再探“高并发利器”epoll:看了一下源码

Unix/Linux 编程:网络编程之 epoll与Reactor

linux高性能服务器编程之epoll