ITKeyword,专注技术干货聚合推荐

注册 | 登录

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

u010193457 分享于

2020腾讯云10周年活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
地址https://www.aliyun.com/minisite/goods

推荐:Linux网络编程--epoll模型ET触发模式之epolloneshot事件

epoll模型的ET模式一般来说只触发一次,然而在并发程序中有特殊情况的存在,譬如当epoll_wait已经检测到socket描述符fd1,并通知应用程序处理fd1的数据,那么处

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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <pthread.h>

#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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>

#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;
}
运行结果展示:

推荐:Epoll 的LT和ET模式

   man epoll    The epoll event distribution interface is able to behave both  as  Edge       Triggered  ( ET ) and Level Triggered ( LT ). The differ

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


推荐:UNIX网络编程——epoll的ET/LT模式注意点

epoll模型有两种工作模式,ET和LT两种模式下都有一些细节值得注意,以下是一些思考:   一、ET模式下 Q1:调用accept时,到底TCP完成队列里有多少个已经建立好的

epoll对文件描述符有两种操作模式--LT(level trigger电平模式)和ET(edge trigger边缘模式) 简单来讲,LT是epoll的默认操作模式,当epoll_wait函数检测到有事件发生并将通知应用程序,而应用

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。