select pool 差異分析

一 使用方法

1) seclect的用法


           FD_ZERO(&rfds);
           FD_SET(0, &rfds);

           /* Wait up to five seconds. */

           tv.tv_sec = 5;
           tv.tv_usec = 0;

           retval = select(1, &rfds, NULL, NULL, &tv);
           /* Don't rely on the value of tv now! */

看fd_set的定義

#define FD_SETSIZE 1024
#define NFDBITS (8 * sizeof(fd_mask))
typedef struct {
  fd_mask fds_bits[FD_SETSIZE/NFDBITS];
} fd_set;

select最多可以跟蹤1024個(gè)文件或者socket.

而poll的用法,單個(gè)文件與事件綁定,動(dòng)態(tài)分配沒有1024這個(gè)限制

struct pollfd {
  int fd;
  short events;
  short revents;
};
int poll(struct pollfd *fds, nfds_t nfds, int timeout);

二 內(nèi)核實(shí)現(xiàn)

記得之前2.6內(nèi)核時(shí)看到poll和select的實(shí)現(xiàn)大體相同,現(xiàn)在就看一下Kernel如何實(shí)現(xiàn)這兩個(gè)函數(shù)的。

select
 |- __pselect6
     |- syscall pselect6
         |- do_pselect
              |- poll_select_set_timeout
                 core_sys_select
                   |- do_select
                        |-(*f_op->poll)
                 poll_select_copy_remaining    

poll的調(diào)用流程

poll
  |- ppoll
      |- ppoll64
            |- __ppoll
                |- SYSCALL ppoll
                     |- do_sys_poll
                          |- do_poll
                               |- do_pollfd
                                   |- f.file->f_op->poll

最后都是通過file的poll函數(shù)實(shí)現(xiàn)。因此本質(zhì)上poll和select是實(shí)現(xiàn)是相似的,只不過用法不一樣

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容