在应用中经常调用selec函数实现多路访问,select函数的实现依赖与驱动的 xxx_poll函数。在xxx_poll函数中,核心的函数是poll_wait函数。在网上查了很多资料,发现轮询的操作依赖于select和poll_wait函数的配合。网上对poll函数是这样解释的:
Poll函数原型:
Unsigned int(*poll)(struct file *filp, struct poll_table *wait);
第一个参数为file结构体指针,第二个参数为轮询表指针。这个函数应该进行以下两项工作
1、对可能引起设备文件状态变化的等待队列调用poll_wait()函数,将对应等待队列添加到 poll_table
2、返回表示是否能对设备进行无阻塞、写访问的掩码
Poll_wait()函数不会引起阻塞。它所做的工作就是把当前进程添加到wait参数指定的等待列表(poll_table)中.
fifo_poll函数:
- static unsigned int cfifo_poll(struct file *file, poll_table *wait)
-
{
-
unsigned int mask = 0;
-
struct cfifo_t *dev = file->private_data;
-
-
down(&dev->sem);
-
poll_wait(file, &dev->r_wait, wait);
-
poll_wait(file, &dev->w_wait, wait);
-
-
if(dev->current_len != 0)
-
mask |= POLLIN | POLLRDNORM;
-
-
if(dev->current_len != GLOBALMEM_SIZE)
-
mask |= POLLOUT | POLLWRNORM;
-
-
up(&dev->sem);
-
return mask;
-
}
问题是:在我们的使用中select 函数经常是要阻塞的,但是在xxx_poll函数中只是通过poll_wait将读写的等待队列加入 poll_table中.并没有阻塞的地方。
poll_wait的实现,总结一下就是:将select的查询的文件描述符的进程等待队列加入到内核的等待队列的维护中,作用就是供select调用查询。
在select的实现中我们就可以看到这些等待队列的作用了。在内核的fs/select.c中有poll 和select的
相关实现。select函数的核心是:do_select。在这里实现了阻塞,poll_wait()函数也是为它服务的。在poll_wait中加入的等待队列也在这里使用。
- 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);
-
wait = &table.pt;
-
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;
-
if (f_op && f_op->poll) {
-
wait_key_set(wait, in, out, bit);
-
mask = (*f_op->poll)(file, wait);
-
}
-
fput_light(file, fput_needed);
-
if ((mask & POLLIN_SET) && (in & bit)) {
-
res_in |= bit;
-
retval++;
-
wait = NULL;
-
}
-
if ((mask & POLLOUT_SET) && (out & bit)) {
-
res_out |= bit;
-
retval++;
-
wait = NULL;
-
}
-
if ((mask & POLLEX_SET) && (ex & bit)) {
-
res_ex |= bit;
-
retval++;
-
wait = NULL;
-
}
-
}
-
}
-
if (res_in)
-
*rinp = res_in;
-
if (res_out)
-
*routp = res_out;
-
if (res_ex)
-
*rexp = res_ex;
-
cond_resched();
-
}
-
wait = NULL;
-
if (retval || timed_out || signal_pending(current)) //如果条件(文件描述符有改变,超时,信号中断)成立 ,break跳出循环
-
break;
-
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;
-
}
- //将进程休眠,设置为可被中断状态 可以被read和write 函数中的wake_up_interruptible(&dev->w_wait);wake_up_int//erruptible(&dev->r_wait);函数唤醒这也是poll_wait函数的作用。唤醒后,继续循环,会调用驱动中的xxx_poll函数//,在 if (retval || timed_out || signal_pending(current))完成select调用。
-
if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
-
to, slack))
-
timed_out = 1;
-
}
-
-
poll_freewait(&table);
-
-
return retval;
-
}
测试代码:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #define FIFO_CLEAR 0x01
- #define BUFFER_LEN 20
- int main(int argc, char *argv[])
- {
- int fd;
- int i;
- fd_set wfds;
- fd_set rfds;
- //fd = open("/dev/cfifo0", O_RDONLY | O_NONBLOCK);
- fd = open("/dev/cfifo0", O_RDONLY | O_WRONLY);
- if(fd > 0)
- ioctl(fd, FIFO_CLEAR, 0);
- else
- return -1;
- for(i = 0; i < 3; i++)
- {
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
- FD_SET(fd, &rfds);
- // FD_SET(fd, &wfds);
- printf("[%s %s %d] select start\n \n", __FUNCTION__, __FILE__, __LINE__);
- select(fd+1, &rfds, NULL, NULL, NULL);
- printf("[%s %s %d] select end\n \n", __FUNCTION__, __FILE__, __LINE__);
- if(FD_ISSET(fd, &rfds) )
- {
- printf("cfifo can read\n");
- }
- // if(FD_ISSET(fd, &wfds) )
- // {
- // printf("cfifo can write\n");
- // }
- }
- return 0;
- }
root@wang:/work/wanghuan/drives# ls
bdev.c cdev.c cfifo.c cfifo.ko cfifo_poll_test.c Makefile modules.order test
root@wang:/work/wanghuan/drives# ./test &
[1] 4457
root@wang:/work/wanghuan/drives# [main cfifo_poll_test.c 51] select start
//在这里select阻塞 就是在do_select if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE, to, slack))
root@wang:/work/wanghuan/drives# ls > /dev/cfifo0
//写入后 cfifo_write的wake_up_interruptible(&dev->r_wait); 唤醒select 进程
[main cfifo_poll_test.c 53] select end
cfifo can read
[main cfifo_poll_test.c 51] select start
[main cfifo_poll_test.c 53] select end
cfifo can read
[main cfifo_poll_test.c 51] select start
[main cfifo_poll_test.c 53] select end
cfifo can read
[1]+ Done ./test
阅读(1246) | 评论(0) | 转发(1) |