Linux kernel [select poll epoll]區別__Linux

來源:互聯網
上載者:User

轉載:http://bookjovi.iteye.com/blog/1186736


Linux中非同步IO等待無非就三個系統調用:select, poll和epoll。很多人無法理解三種調用的區別,或不夠瞭解,今天就結合Linux kernel code詳細描述三個的區別。

 

    select:

    select 的限制就是最大1024個fd,可以查看kernel中的posix_types.h,裡面定義了fdset資料結構,顯然select不適合poll大量fd的情境(如webserver)。 

 

    include/linux/posix_types.h :

 

  C代碼    #undef __NFDBITS   #define __NFDBITS       (8 * sizeof(unsigned long))      #undef __FD_SETSIZE   #define __FD_SETSIZE    1024      #undef __FDSET_LONGS   #define __FDSET_LONGS   (__FD_SETSIZE/__NFDBITS)      #undef __FDELT   #define __FDELT(d)      ((d) / __NFDBITS)      #undef __FDMASK   #define __FDMASK(d)     (1UL << ((d) % __NFDBITS))      typedef struct {           unsigned long fds_bits [__FDSET_LONGS];   } __kernel_fd_set;    

 

  poll:

   poll相對於select改進了fdset size的限制,poll沒有再使用fdset數組結構,反而使用了pollfd,這樣使用者可以自訂非常大的pollfd數組,這個pollfd數組在kernel中的表現形式是poll_list鏈表,這樣就不存在了1024的限制了,除此之外poll相比select無太大區別。

 

  C代碼    int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,                   struct timespec *end_time)   {           struct poll_wqueues table;           int err = -EFAULT, fdcount, len, size;           /* Allocate small arguments on the stack to save memory and be             faster - use long to make sure the buffer is aligned properly             on 64 bit archs to avoid unaligned access */           long stack_pps[POLL_STACK_ALLOC/sizeof(long)];           struct poll_list *const head = (struct poll_list *)stack_pps;           struct poll_list *walk = head;           unsigned long todo = nfds;              if (nfds > rlimit(RLIMIT_NOFILE))                   return -EINVAL;              len = min_t(unsigned int, nfds, N_STACK_PPS);           for (;;) {                   walk->next = NULL;                   walk->len = len;                   if (!len)                           break;                      if (copy_from_user(walk->entries, ufds + nfds-todo,                                           sizeof(struct pollfd) * walk->len))                           goto out_fds;                      todo -= walk->len;                   if (!todo)                           break;                      len = min(todo, POLLFD_PER_PAGE);                   size = 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.