探讨epoll_wait

NAME
       epoll_wait, epoll_pwait - wait for an I/O event on an epoll file descriptor

SYNOPSIS
       #include

       int epoll_wait(int epfd, struct epoll_event *events,
                      int maxevents, int timeout);
       int epoll_pwait(int epfd, struct epoll_event *events,
                      int maxevents, int timeout,
                      const sigset_t *sigmask);

DESCRIPTION
       The  epoll_wait()  system  call  waits for events on the epoll file descriptor epfd for a maximum time of timeout milliseconds.  The memory area pointed to by events
       will contain the events that will be available for the caller.  Up to maxevents are returned by epoll_wait(2).  The maxevents parameter must be  greater  than  zero.
       Specifying  a  timeout  of  -1  makes  epoll_wait(2) wait indefinitely, while specifying a timeout equal to zero makes epoll_wait(2) to return immediately even if no
       events are available (return code equal to zero).  The struct epoll_event is defined as :

            typedef union epoll_data {
                void *ptr;
                int fd;
                __uint32_t u32;
                __uint64_t u64;
            } epoll_data_t;

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

       The data of each returned structure will contain the same data the user set with a epoll_ctl(2) (EPOLL_CTL_ADD,EPOLL_CTL_MOD) while the events  member  will  contain
       the returned event bit field.

   epoll_pwait()
       The  relationship  between epoll_wait() and epoll_pwait() is analogous to the relationship between select(2) and pselect(2): like pselect(2), epoll_pwait() allows an
       application to safely wait until either a file descriptor becomes ready or until a signal is caught.

 

可是,epoll_wait的maxevents该设置成多少呢?是不是如果有超过maxevents的事件就绪,就抛弃了?先看epoll_wait系统实现的调用栈。

epoll_wait
    ep_poll
        ep_events_transfer
            ep_collect_ready_items
            ep_send_events
简单分析:
(1) ep_collect_ready_items 可以看出,事件之前是挂在一个列表ep->rdllist上的,这个函数就是把指定数量(该数量就是epoll_wait的参数maxevents)的事件挂到另一个列表txlist,也就是没有取出来的
事件仍然挂在ep->rdllist上,不会丢失,下个epoll_wait再通知用户
(2) ep_send_events 执行的动作就是把列表txlist的内容放到缓冲区,然后复制到用户缓冲区

 

maxevents 是epoll_wait可以处理的连接事件的最大限度值,这个值一般要小于或等于epoll_create的那个size,当然如果设置成比size还大的话也无所谓,size是epoll整体可以监听的最大fd数量。maxevents的意义是防止epoll的API在填写你传进去的指针events的时候,超过指针指向的内存的大小从而导致内存溢出。


你可能感兴趣的:(探讨epoll_wait)