Linux网络编程--epoll模型之LT触发模式和ET触发模式

epoll对文件描述符有两种操作模式--LT(level trigger电平模式)和ET(edge trigger边缘模式)

简单来讲,LT是epoll的默认操作模式,当epoll_wait函数检测到有事件发生并将通知应用程序,而应用程序不一定必须立即进行处理,这样epoll_wait函数再次检测到此事件的时候还会通知应用程序,直到事件被处理。

而ET模式,只要epoll_wait函数检测到事件发生,通知应用程序立即进行处理,后续的epoll_wait函数将不再检测此事件。因此ET模式在很大程度上降低了同一个事件被epoll触发的次数,因此效率比LT模式高。

下面的代码对两种模式进行了对比和分析。

客户端模型发送54字节的数据到服务端,ET模式只触发一次,而LT模式要触发6次。再次证明了ET模式要比LT模式触发的次数少很多。

epoll服务端代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MAX_EVENT_NUMBER 1024
#define BUFFER_SIZE 10

int setnonblocking( int fd )
{
    int old_option = fcntl( fd, F_GETFL );
    int new_option = old_option | O_NONBLOCK;
    fcntl( fd, F_SETFL, new_option );
    return old_option;
}

void addfd( int epollfd, int fd, bool enable_et )
{
    epoll_event event;
    event.data.fd = fd;
    event.events = EPOLLIN;
    if( enable_et )
    {
        event.events |= EPOLLET;
    }
    epoll_ctl( epollfd, EPOLL_CTL_ADD, fd, &event );
    setnonblocking( fd );
}

void lt( epoll_event* events, int number, int epollfd, int listenfd )
{
    char buf[ BUFFER_SIZE ];
    for ( int i = 0; i < number; i++ )
    {
        int sockfd = events[i].data.fd;
        if ( sockfd == listenfd )
        {
            struct sockaddr_in client_address;
            socklen_t client_addrlength = sizeof( client_address );
            int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
            addfd( epollfd, connfd, false );
        }
        else if ( events[i].events & EPOLLIN )
        {
            printf( "event trigger once\n" );
            memset( buf, '\0', BUFFER_SIZE );
            int ret = recv( sockfd, buf, BUFFER_SIZE-1, 0 );
            if( ret <= 0 )
            {
                close( sockfd );
                continue;
            }
            printf( "get %d bytes of content: %s\n", ret, buf );
        }
        else
        {
            printf( "something else happened \n" );
        }
    }
}

void et( epoll_event* events, int number, int epollfd, int listenfd )
{
    char buf[ BUFFER_SIZE ];
    for ( int i = 0; i < number; i++ )
    {
        int sockfd = events[i].data.fd;
        if ( sockfd == listenfd )
        {
            struct sockaddr_in client_address;
            socklen_t client_addrlength = sizeof( client_address );
            int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
            addfd( epollfd, connfd, true );
        }
        else if ( events[i].events & EPOLLIN )
        {
            printf( "event trigger once\n" );
            /*因为ET模式只触发一次,所以使用循环确保数据全部接受*/
            while( 1 )
            {
                memset( buf, '\0', BUFFER_SIZE );
                int ret = recv( sockfd, buf, BUFFER_SIZE-1, 0 );
                if( ret < 0 )
                {
                     /*下面if条件成立,则读缓冲区数据已经读取完成*/
                    if( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) )
                    {
                        printf( "read later\n" );
                        break;
                    }
                    close( sockfd );
                    break;
                }
                else if( ret == 0 )
                {
                    close( sockfd );
                }
                else
                {
                    printf( "get %d bytes of content: %s\n", ret, buf );
                }
            }
        }
        else
        {
            printf( "something else happened \n" );
        }
    }
}

int main( int argc, char* argv[] )
{
    if( argc <= 2 )
    {
        printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );

    int ret = 0;
    struct sockaddr_in address;
    bzero( &address, sizeof( address ) );
    address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &address.sin_addr );
    address.sin_port = htons( port );

    int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
    assert( listenfd >= 0 );

    ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) );
    assert( ret != -1 );

    ret = listen( listenfd, 5 );
    assert( ret != -1 );

    epoll_event events[ MAX_EVENT_NUMBER ];
    int epollfd = epoll_create( 5 );
    assert( epollfd != -1 );
    addfd( epollfd, listenfd, true );

    while( 1 )
    {
        int ret = epoll_wait( epollfd, events, MAX_EVENT_NUMBER, -1 );
        if ( ret < 0 )
        {
            printf( "epoll failure\n" );
            break;
        }
    
        //lt( events, ret, epollfd, listenfd );
        et( events, ret, epollfd, listenfd );
    }

    close( listenfd );
    return 0;
}


epoll客户端代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8888
#define MAX_EVENT_NUMBER 1024
#define BUFFER_SIZE 10

void sig_handle(int sig){
	printf("catch a sigpipe signal\n");
	printf("server have close\n");
	exit(0);
}

int main( int argc, char* argv[] )
{
    const char* ip = "127.0.0.1";

    int ret = 0;
    char buf[1024];
    struct sockaddr_in address;
    bzero( &address, sizeof( address ) );
    address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &address.sin_addr );
    address.sin_port = htons( PORT );
		
    signal(SIGPIPE,sig_handle);
		
    int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
    assert( listenfd >= 0 );

    while( 1 ){
	int ret = connect(listenfd,(struct sockaddr *)&address,sizeof(struct sockaddr_in));
	if ( ret < 0 )
	{
	   printf( "connect failure\n" );
	   sleep(1);
	   continue;
	 }
	break;			
    }

    while( 1 )
    {
        memset(buf,0,sizeof(buf));
        fgets(buf,BUFFER_SIZE,stdin);
        send(listenfd,buf,BUFFER_SIZE-1,0);
    }

    close( listenfd );
    return 0;
}
运行结果展示:

ET模式:

客户端:

/home/kevin/src/linux>./epoll_c 
111111111222222222333333333444444444555555555666666666

服务端:

/home/kevin/src/linux>./epoll_s 127.0.0.1 8888
event trigger once
get 9 bytes of content: 111111111
get 9 bytes of content: 222222222
get 9 bytes of content: 333333333
get 9 bytes of content: 444444444
get 9 bytes of content: 555555555
get 9 bytes of content: 666666666
read later


LT模式:

客户端:

/home/kevin/src/linux>./epoll_c 
111111111222222222333333333444444444555555555666666666

服务端:

/home/kevin/src/linux>./epoll_s 127.0.0.1 8888
event trigger once
get 9 bytes of content: 111111111
event trigger once
get 9 bytes of content: 222222222
event trigger once
get 9 bytes of content: 333333333
event trigger once
get 9 bytes of content: 444444444
event trigger once
get 9 bytes of content: 555555555
event trigger once
get 9 bytes of content: 666666666


你可能感兴趣的:(Linux网络编程--epoll模型之LT触发模式和ET触发模式)