探讨epoll_wait

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

SYNOPSIS
       #include <sys/epoll.h>

       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的时候,超过指针指向的内存的大小从而导致内存溢出。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值