一 使用方法
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)分配沒(mé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
最后都是通過(guò)file的poll函數(shù)實(shí)現(xiàn)。因此本質(zhì)上poll和select是實(shí)現(xiàn)是相似的碗硬,只不過(guò)用法不一樣