1.用户空间调用(参考 poll(2) - Linux man page

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

it waits for one of a set of file descriptors to become ready to perform I/O.

The set of file descriptors to be monitored is specified in the fds argument, which is an array of structures of the following form:

struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};

关于timeout参数的说明:

  timeout>0,设置超时时间为timeout

  timeout=0,直接返回

  timeout<0,无限长超时时间

返回值说明:

On success, a positive number is returned; this is the number of structures which have nonzero revents fields (in other words, those descriptors with events or errors reported).

A value of 0 indicates that the call timed out and no file descriptors were ready. On error, -1 is returned, and errno is set appropriately.

2.内核调用

asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,long timeout_msecs)
-->ret = do_sys_poll(ufds, nfds, &timeout);
-->struct poll_wqueues table;
-->poll_initwait(&table);
-->init_poll_funcptr(&pwq->pt, __pollwait);
-->把ufds指向的用户空间数据拷贝到内核空间,由poll_list结构体保存
-->fdcount = do_poll(nfds, head, &table, timeout);
for (;;) {
-->set_current_state(TASK_INTERRUPTIBLE);
-->for (walk = list; walk != NULL; walk = walk->next) //遍历poll_list结构中的pollfd结构
-->do_pollfd(pfd, pt)
-->if (file->f_op && file->f_op->poll) //驱动中的poll函数
--> void __pollwait(struct file *filp, wait_queue_head_t *wait_address,poll_table *p)
-->struct poll_table_entry *entry = poll_get_entry(p);
-->entry->filp = filp;
-->entry->wait_address = wait_address;
-->init_waitqueue_entry(&entry->wait, current);
-->add_wait_queue(wait_address, &entry->wait);//加入等待队列头
-->return mask;//返回设备是否可读写状态
-->if (count || !*timeout || signal_pending(current))
break;//设备可读写,超时时间到,有信号中断三种情况都返回
-->__timeout = schedule_timeout(__timeout);//进程调度,休眠
}
//唤醒后返回
-->__set_current_state(TASK_RUNNING);
-->return count;
-->把poll_list结构体保存的pollfd.revents返回给用户空间的ufds数组
-->poll_freewait(&table);//释放poll_table_entry中的等待队列

3.相关结构体都在fs/select.c,include/linux/poll.h

poll_wqueues是最关键的一个结构体,

 struct poll_wqueues {
poll_table pt;
struct poll_table_page * table;
int error;
int inline_index;
struct poll_table_entry inline_entries[N_INLINE_POLL_ENTRIES];
};

poll_table结构体只包含一个函数指针,初始化时指向

void __pollwait(struct file *filp, wait_queue_head_t *wait_address,poll_table *p)

 typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_table_struct *);

 typedef struct poll_table_struct {
poll_queue_proc qproc;
} poll_table;

poll_table_page就是包含poll_table_entry结构的链表,作用与inline_entries[N]相同,检测文件数少的时候用不到这个结构

 struct poll_table_page {
struct poll_table_page * next;
struct poll_table_entry * entry;
struct poll_table_entry entries[];
};

poll_table_entry包含文件指针,等待队列及等待队列头

 struct poll_table_entry {
struct file * filp;
wait_queue_t wait;
wait_queue_head_t * wait_address;
};

4.操作

poll_wqueues初始化

 static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
{
pt->qproc = qproc;
}
void poll_initwait(struct poll_wqueues *pwq)
{
init_poll_funcptr(&pwq->pt, __pollwait);
pwq->error = ;
pwq->table = NULL;
pwq->inline_index = ;
}

那么poll_table结构中的函数什么时候使用及干什么呢?

我们自己在驱动中的poll函数中会调用poll_wait()函数

 static unsigned forth_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = ;
poll_wait(file, &button_waitq, wait); // 不会立即休眠 if (ev_press)
mask |= POLLIN | POLLRDNORM; return mask;
}

而poll_wait()函数最终会调用__pollwait()函数

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);
}
 /* Add a new entry */
static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
poll_table *p)
{
struct poll_table_entry *entry = poll_get_entry(p);
if (!entry)
return;
get_file(filp);
entry->filp = filp;
entry->wait_address = wait_address; //即forth_drv_poll中传入的button_waitq等待队列头
init_waitqueue_entry(&entry->wait, current);
add_wait_queue(wait_address, &entry->wait);
}

Linux poll机制的更多相关文章

  1. [转载] Linux poll机制

    原地址:http://hongwazi.blog.163.com/blog/#m=0&t=3&c=poll poll的是一种查询的方式,英文解释 :民意调查 函数原型:int poll ...

  2. 嵌入式Linux驱动学习之路(十二)按键驱动-poll机制

    实现的功能是在读取按键信息的时候,如果没有产生按键,则程序休眠在read函数中,利用poll机制,可以在没有退出的情况下让程序自动退出. 下面的程序就是在读取按键信息的时候,如果5000ms内没有按键 ...

  3. Linux学习 :中断处理机制 & poll机制

    中断是指在CPU正常运行期间,由于内外部事件或由程序预先安排的事件引起的CPU暂时停止正在运行的程序,转而为该内部或外部事件或预先安排的事件服务 的程序中去,服务完毕后再返回去继续运行被暂时中断的程序 ...

  4. linux驱动编写之poll机制

    一.概念 1.poll情景描述 以按键驱动为例进行说明,用阻塞的方式打开按键驱动文件/dev/buttons,应用程序使用read()函数来读取按键的键值.这样做的效果是:如果有按键按下了,调用该re ...

  5. Linux驱动之poll机制的理解与简单使用

    之前在Linux驱动之按键驱动编写(中断方式)中编写的驱动程序,如果没有按键按下.read函数是永远没有返回值的,现在想要做到即使没有按键按下,在一定时间之后也会有返回值.要做到这种功能,可以使用po ...

  6. Linux之poll机制分析

    应用程序访问1个设备文件时可用阻塞/非阻塞方式.如果是使用阻塞方式,则直接调用open().read().write(),但是在驱动程序层会判断是否可读/可写,如果不可读/不可写,则将当前进程休眠,直 ...

  7. linux IO多路复用POLL机制深入分析

    POLL机制的作用这里就不进行介绍,根据linux man手册,解释为在一个文件描述符上等待某个事件.按照抽象一点的理解,当某个事件被触发(条件被满足),文件描述符变为有状态,那么用户空间可以根据此进 ...

  8. linux字符驱动之poll机制按键驱动

    在上一节中,我们讲解了如何自动创建设备节点,实现一个中断方式的按键驱动.虽然中断式的驱动,效率是蛮高的,但是大家有没有发现,应用程序的死循环里的读函数是一直在读的:在实际的应用场所里,有没有那么一种情 ...

  9. 【原创】Linux select/poll机制原理分析

    前言 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 1. 概述 Linux系统 ...

随机推荐

  1. 栈 && 教授的测试

    卡特兰数:https://blog.csdn.net/wu_tongtong/article/details/78161211 https://www.luogu.org/problemnew/sho ...

  2. 阿里云ECS基础知识01

  3. 使用express+mongoDB搭建多人博客 学习(3)connect-flash和mongodb,表单注册

    1.根目录下新建settings.js,存放数据库配置 module.exports={ cookieSecret:"myblog", db:"blog", h ...

  4. 谈PHP中的钩子

    钩子,英文为hooks.在程序中应用相当广泛,但是究竟什么是钩子呢?本人介绍一下目前本人对钩子的理解和相关心得. 假如有这么一段程序流: function fun(){ funA(); funB(); ...

  5. #82. 【UR #7】水题生成器

    链接:http://uoj.ac/problem/82 今天是世界水日,著名的水题资源专家蝈蝈大臣向世界宣布了他的一项新发明 —— 水题生成器. 每道题目都有一个正整数的难度值.水题生成器虽然强大但是 ...

  6. CF1093D Beautiful Graph

    思路: 题目倒是没啥好说的,就是注意memset的效率问题.如果循环多次调用memset去初始化一个比较大的数组,那就会很费时间.就是因为这个被hack了.:( 实现: #include <bi ...

  7. IOS之TextView属性设置

    UIFontDescriptor *bodyFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFont ...

  8. SQLServer怎样把本地数据导入到远程服务器上(转载)

    平常用到mssql时间比较少,总是过一段时间就忘记应该怎么操作了.当要做mssq把本地数据导入到远程服务器的时候,就去网上搜索很久都没有图解的,所以今天自己收集一下免得下次又到处去找.希望对自己,同时 ...

  9. sysdig安装和使用介绍

    安装步骤1)安装资源库rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.publiccurl -s -o ...

  10. python GIL锁问题

    一.GIL是什么 官方解释: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple na ...