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. UVA11292(排序贪心)

    笔者休息娱乐. ; int n, m, a; ll solve(int n, int m) { ll ans = ; priority_queue<int, vector<int>, ...

  2. 软件管理命令-- rpm

    RPM(红帽软件包管理器) 安装软件 rpm -ivh filename.rpm 升级软件 rpm -Uvh filename.rpm 卸载软件 rpm -e filename.rpm -i 安装一个 ...

  3. 解决thymeleaf严格html5校验的方法

    用的是springboot加thyemleaf做静态模板. 然后会有个很烦的东西,就这个静态模板对html的格式非常严格,导致很多框架的格式都用不了,然后这里有个解除的方法: 1.在pom中添加依赖: ...

  4. CF1149A Prefix Sum Primes

    思路: 质数一定是奇数.实现: #include <bits/stdc++.h> using namespace std; int main() { int n, t, x, y; whi ...

  5. WebService学习之旅(七)Axis2发布WebService的几种方式

    前面几篇文章中简单的介绍了如何使用Axis2发布WebService及如何使用Axis2实现Web服务的客户端调用,本节將详细介绍Axis2发布WebService的几种方式. 一.使用aar包方式发 ...

  6. freebsd安装ports

    /etc/portsnap.conf 里面更改 SERVERNAME=portsnap.hshh.org portsnap的命令比较少 fetch 获取数据 extract 释放全部ports upd ...

  7. C#调用Lame.exe

    string lameEXE = @"D:\lame3.100\lame.exe"; string lameArgs = "-b 128"; string wa ...

  8. 博客高亮代码及使用OpenLiveWriter修改之前博客

    简述:  最近查阅前辈资料的时候,看到写的博客很有条理,回头看下自己的乱做麻花,然后来时研究: 他们的代码看起来很漂亮然后我就查资料,在网页版上一直没法出来像他们的格式,后查资料看来的使用客户端工具才 ...

  9. Thread and Peocess

    Thread and Peocess pthread_create() 原型: int pthread_create(pthread_t* thread, pthread_attr_t* attr, ...

  10. 程序windows上可以上传附件,部署到 linux服务器后出现 “上传目录 不可写” 怎么解决?

    这样的问题一般都是linux  下文件读写权限引起的,用 shell  命名到上传附件的目录(如 cd /data/www/project/upload/),然后执行 shell 文件权限设置: 例如 ...