标签:
int select(int nfds,
fd_set *restrict readfds,
fd_set *restrict writefds,
fd_set *restrict errorfds,
struct timeval *restrict timeout);
SYSCALL_DEFINE5(select, int, n,
fd_set __user *, inp,
fd_set __user *, outp,
fd_set __user *, exp,
struct timeval __user *, tvp)
{
ret = core_sys_select(n, inp, outp, exp, to);
ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
return ret;
}
core_sys_select
主要工作:
do_select
实现核心的轮询工作。
int core_sys_select(int n,
fd_set __user *inp,
fd_set __user *outp,
fd_set __user *exp,
struct timespec *end_time)
{
fd_set_bits fds;
// …
if ((ret = get_fd_set(n, inp, fds.in)) ||
(ret = get_fd_set(n, outp, fds.out)) ||
(ret = get_fd_set(n, exp, fds.ex))) //
*get_fd_set仅仅调用copy_from_user从用户空间拷贝了fd_set*/goto out;
zero_fd_set(n, fds.res_in);
zero_fd_set(n, fds.res_out);
zero_fd_set(n, fds.res_ex);
//发现do_select函数
ret = do_select(n, &fds, end_time);
/*把结果集,拷贝回用户空间*/
- if (set_fd_set(n, inp, fds.res_in) ||
set_fd_set(n, outp, fds.res_out) ||
set_fd_set(n, exp, fds.res_ex))
ret = -EFAULT;
}
int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
{
struct poll_wqueues table;
poll_table *wait;
poll_initwait(&table);//
这个函数实现很关键,其内部的init_poll_funcptr
初始化回调函数为__pollwait
, 后面轮询会回调这个函数,然后通过这个函数把进程添加到对应的监听文件等待队列,当有事件到来时,就会唤醒这个进程。for (;;) {
//一次大循环
for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
// …
struct fd f;
f = fdget(i);
if (f.file) {
const struct file_operations *f_op; //每个设备拥有一个struct file_operations结构体
f_op = f.file->f_op;
mask = DEFAULT_POLLMASK;
if (f_op->poll) { //轮询函数不为空,每当设备模块加载就自动会加载设备轮询函数,等于将轮回函数统一付给poll这个指针,以便调用
wait_key_set(wait, in, out,
bit, busy_flag);//检查集合// 对每个fd进行I/O事件检测 (*f_op->poll)返回当前设备fd的状态(可读可写)
mask = (*f_op->poll)(f.file, wait);//将会调用poll_wait函数,检测文件设备的状态,并且将当前进程加入到设备等待队列中。并且返回掩码
}
fdput(f);
}
}
// 退出循环体
if (retval || timed_out || signal_pending(current))
break;
// 轮询一遍没有发现就绪。那就休眠
if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
to, slack))
timed_out = 1;
}
}
这个函数实现很关键,这里
init_poll_funcptr
初始化回调函数为__pollwait
, 后面轮询会回调这个函数,然后通过这个函数把进程添加到对应的监听文件等待队列,当有事件到来时,就会唤醒这个进程。poll_initwait(&table);
void poll_initwait(struct poll_wqueues *pwq){
//这里p->_qproc实际就是__pollwait函数,因为p->qproc在init_poll_funcptr中被赋值为__pollwait函数指针init_poll_funcptr(&pwq->pt, __pollwait); //初始化函数指针,设置为__pollwait
pwq->error = 0;
pwq->table = NULL;
pwq->inline_index = 0;}
static inline void
init_poll_funcptr(poll_table *pt, poll_queue_proc qproc){pt->qproc = qproc;
}
struct file {
struct path f_path;//路径
struct inode *f_inode; //inode
const struct file_operations *f_op; //包含各种用于操作设备的函数指针
- } __attribute__((aligned(4))); /* lest something weird decides that 2
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
// select()轮询设备fd的操作函数,对应一个file 跟poll_table_struct *
unsigned int (*poll) (struct file *, struct poll_table_struct *); //驱动加载。一般就挂到这个地方轮询函数
};
struct scull_pipe {
wait_queue_head_t inq, outq; //可读可写队列
};
static unsigned int scull_p_poll(struct file *filp, poll_table *wait)
{
struct scull_pipe *dev = filp->private_data;
unsigned int mask = 0;
mutex_lock(&dev->mutex);
poll_wait(filp, &dev->inq, wait);//pollwait函数包含了__pollwait.这函数就是把当前进程添加到设备队列中
poll_wait(filp, &dev->outq, wait);//等待
if (dev->rp != dev->wp)
mask |= POLLIN | POLLRDNORM; //可读
if (spacefree(dev))
mask |= POLLOUT | POLLWRNORM; //可写
mutex_unlock(&dev->mutex);
return mask;//返回该设备的掩码,是否就绪可读可写
}
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
if (p && p->_qproc && wait_address)
p->_qproc(filp, wait_address, p);//这里p->_qproc实际就是__pollwait函数,因为p->qproc在do_select中被赋值为__pollwait函数指针
}
typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_table_struct *);
poll_table里的函数指针,是在do_select()
初始化的。
int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
{
struct poll_wqueues table;
poll_table *wait;
poll_initwait(&table);//初始化
}
void poll_initwait(struct poll_wqueues *pwq)
{
// 初始化poll_table里的函数指针
init_poll_funcptr(&pwq->pt, __pollwait);
}
EXPORT_SYMBOL(poll_initwait);
static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
{
pt->_qproc = qproc;//将poll_table的函数指针设置为__pollwait完成初始化工作
pt->_key = ~0UL; /* all events enabled */
}
static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
poll_table *p)
{
// 把当前进程装到设备的等待队列
add_wait_queue(wait_address, &entry->wait);
}
static ssize_t scull_p_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
wake_up_interruptible(&dev->inq); //唤醒当前进程
}
标签:
原文地址:http://www.cnblogs.com/zengyiwen/p/5755205.html