代码摘自:linux-2.6.29
sys_select -> core_sys_select -> do_select
sys_select -> core_sys_select -> do_select
-
int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
-
{
-
ktime_t expire, *to = NULL;
-
struct poll_wqueues table;
-
poll_table *wait;
-
int retval, i, timed_out = 0;
-
unsigned long slack = 0;
-
-
rcu_read_lock();
-
retval = max_select_fd(n, fds);
-
rcu_read_unlock();
-
-
if (retval < 0)
-
return retval;
-
n = retval;
-
-
poll_initwait(&table);
-
[color=Red]wait = &table.pt;[/color]
-
if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
-
wait = NULL;
-
timed_out = 1;
-
}
-
-
if (end_time && !timed_out)
-
slack = estimate_accuracy(end_time);
-
-
retval = 0;
-
for (;;) {
-
unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
-
-
inp = fds->in; outp = fds->out; exp = fds->ex;
-
rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
-
-
for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
-
unsigned long in, out, ex, all_bits, bit = 1, mask, j;
-
unsigned long res_in = 0, res_out = 0, res_ex = 0;
-
const struct file_operations *f_op = NULL;
-
struct file *file = NULL;
-
-
in = *inp++; out = *outp++; ex = *exp++;
-
all_bits = in | out | ex;
-
if (all_bits == 0) {
-
i += __NFDBITS;
-
continue;
-
}
-
-
for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
-
int fput_needed;
-
if (i >= n)
-
break;
-
if (!(bit & all_bits))
-
continue;
-
file = fget_light(i, &fput_needed);
-
if (file) {
-
f_op = file->f_op;
-
mask = DEFAULT_POLLMASK;
-
[color=Red]if (f_op && f_op->poll)
-
mask = (*f_op->poll)(file, retval ? NULL : wait);[/color]
-
fput_light(file, fput_needed);
-
if ((mask & POLLIN_SET) && (in & bit)) {
-
res_in |= bit;
-
retval++;
-
}
-
if ((mask & POLLOUT_SET) && (out & bit)) {
-
res_out |= bit;
-
retval++;
-
}
-
if ((mask & POLLEX_SET) && (ex & bit)) {
-
res_ex |= bit;
-
retval++;
-
}
-
}
-
}
-
if (res_in)
-
*rinp = res_in;
-
if (res_out)
-
*routp = res_out;
-
if (res_ex)
-
*rexp = res_ex;
-
cond_resched();
-
}
-
[color=Red]wait = NULL;
-
if (retval || timed_out || signal_pending(current))
-
break;[/color]
-
if (table.error) {
-
retval = table.error;
-
break;
-
}
-
-
/*
-
* If this is the first loop and we have a timeout
-
* given, then we convert to ktime_t and set the to
-
* pointer to the expiry value.
-
*/
-
if (end_time && !to) {
-
expire = timespec_to_ktime(*end_time);
-
to = &expire;
-
}
-
-
if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
-
to, slack))
-
timed_out = 1;
-
}
-
-
poll_freewait(&table);
-
-
return retval;
- }
在do_select中,外层的for循环第一次执行的时候,才会真正调用所有文件的poll函数:
if (f_op && f_op->poll)
mask = (*f_op->poll)(file, retval ? NULL : wait);
第一次执行时, retval=0,所以poll函数的第二个参数为wait,且wait = &table.pt;所以,wait!=NULL,
所以会调用到p->qproc(filp, wait_address, p);将当前进程相关的等待队列插入等待队列wait_address。
等到所有的文件的poll函数被调用完毕以后,查看他们的返回值
if (retval || timed_out || signal_pending(current))
break;
假如retval非0(retval保存了检测到的可操作的文件描述符的个数),则退出外层for循环,直接返回。
否则,调用poll_schedule_timeout让出cpu,等再次拿到cpu之后,继续进行外层for的循环,但是此时wait=NULL,所以,这一次
调用的文件的poll函数其实是poll(file, NULL)
我们注意到:
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
if (p && wait_address)
p->qproc(filp, wait_address, p);
}
所以,这一次poll_wait什么都没有做,所以文件的poll函数只是查询了一些标记值,然后根据它设置返回值。这是可以理解的:第一次的时候,已经将
相关的等待队列插入到了等待队列wait_address中去了,所以,以后就不再重复这样的动作了,仅仅是检查返回值以返回给do_select函数。
这是我的理解,欢迎大虾们补充
if (f_op && f_op->poll)
mask = (*f_op->poll)(file, retval ? NULL : wait);
第一次执行时, retval=0,所以poll函数的第二个参数为wait,且wait = &table.pt;所以,wait!=NULL,
所以会调用到p->qproc(filp, wait_address, p);将当前进程相关的等待队列插入等待队列wait_address。
等到所有的文件的poll函数被调用完毕以后,查看他们的返回值
if (retval || timed_out || signal_pending(current))
break;
假如retval非0(retval保存了检测到的可操作的文件描述符的个数),则退出外层for循环,直接返回。
否则,调用poll_schedule_timeout让出cpu,等再次拿到cpu之后,继续进行外层for的循环,但是此时wait=NULL,所以,这一次
调用的文件的poll函数其实是poll(file, NULL)
我们注意到:
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
if (p && wait_address)
p->qproc(filp, wait_address, p);
}
所以,这一次poll_wait什么都没有做,所以文件的poll函数只是查询了一些标记值,然后根据它设置返回值。这是可以理解的:第一次的时候,已经将
相关的等待队列插入到了等待队列wait_address中去了,所以,以后就不再重复这样的动作了,仅仅是检查返回值以返回给do_select函数。
这是我的理解,欢迎大虾们补充