字符设备之poll机制
poll机制作用:相当于一个定时器。时间到了还没有资源就唤醒进程。
主要用途就是:进程设置一段时间用来等待资源,假设时间到了资源还没有到来,进程就立马从睡眠状态唤醒不再等待。当然这仅仅是使用于这段时间以后资源对于该进程已经没用的情况。
内核中poll机制的实现过程:
sys_poll函数在include/linux/syscalls.h中声明
//函数定义前加宏asmlinkage ,表示这些函数通过堆栈而不是通过寄存器传递參数。
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,long timeout);
在系统调用表arch\arm\kernel\calls.S中调用
CALL(sys_poll) //系统调用跳转表的一项
关于系统调用表的初始化在arch/arm/kernel/entry-common.S中
.equ NR_syscalls,0 //将NR_syscalls初始化为0
#define CALL(x) .equ NR_syscalls,NR_syscalls+1 //将CALL(x) 定义为: NR_syscalls = NR_syscalls + 1
#include "calls.S"//将calls.S的内容包进来,CALL(x)上面已经有了定义,这里就相当于运行了多次NR_syscalls++,最后就统计了系统调用的个数。并对NR_syscalls进行了4的倍数对齐。这一招,特么好厉害! #undef CALL //撤销CALL宏定义
#define CALL(x) .long x //对CALL又一次进行宏定义。也是4字节对齐
arch/arm/kernel/entry-common.S中:
sys_syscall:
bic scno, r0, #__NR_OABI_SYSCALL_BASE
cmp scno, #__NR_syscall - __NR_SYSCALL_BASE
cmpne scno, #NR_syscalls @ check range
stmloia sp, {r5, r6} @ shuffle args
movlo r0, r1
movlo r1, r2
movlo r2, r3
movlo r3, r4
ldrlo pc, [tbl, scno, lsl #2]
b sys_ni_syscall
终于sys_poll()函数,就相当于以下的函数:在fs/select.c文件里,SYSCALL_DEFINE3是有3个參数的系统调用的宏定义
SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,long, timeout_msecs)
{
...... ret = do_sys_poll(ufds, nfds, to);//调用 ......
}
好,看看应用层调用poll函数时的底层驱动运行线路
【app:】
poll();
【kernel: 】
sys_poll
do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,struct timespec *end_time)
poll_initwait(&table);
init_poll_funcptr(&table->pt, __pollwait);-->pt->qproc = __pollwait; //初始化qproc函数指针,让他指向__pollwait函数
do_poll(nfds, head, &table, end_time);
for(;;)
{
for (; pfd != pfd_end; pfd++) //查询多个驱动程序
{
if (do_pollfd(pfd, pt)) -> mask = file->f_op->poll(file, pwait);return mask;
{ //do_pollfd函数相当于调用驱动里面的xxx_poll函数,以下另外再进行分析,返回值mask非零。count++,记录等待事件发生的进程数
count++;
pt = NULL;
}
} if (count || timed_out) //若count不为0(有等待的事件发生了)或者timed_out不为0(有信号发生或超时),则推出休眠
break; //上述条件不满足以下開始进入休眠,若有等待的事件发生了,超时或收到信号则唤醒
poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)
}
驱动里边的xxx_poll()函数分析
xxx_poll(struct file *file, poll_table *wait)
poll_wait(file, &xxxx_waitq, wait);
//////////////////////////////////////////////////////////////////
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_initwait()函数设置的函数qproc即__pollwait,__pollwait函数仅仅是把当前进程挂到等待队列,仅仅是add_wait_queue(wait_address, &entry->wait);不进入休眠
}
測试驱动程序:poll_dev.c
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/module.h>
#include <linux/device.h> //class_create
#include <mach/regs-gpio.h> //S3C2440_GPF1
#include <mach/hardware.h>
#include <linux/interrupt.h> //wait_event_interruptible
#include <linux/fs.h>
#include <linux/poll.h> //poll /* 定义并初始化等待队列头 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); static struct class *buttondev_class;
static struct device *buttons_device; static struct pin_desc{
unsigned int pin;
unsigned int key_val;
}; static struct pin_desc pins_desc[4] = {
{S3C2410_GPF1,0x01}, //S3C2410_GPF1是对GPF1引脚这样的“设备”的编号dev_id
{S3C2410_GPF4,0x02},
{S3C2410_GPF2,0x03},
{S3C2410_GPF0,0x04},
};
static int ev_press = 0; static unsigned char key_val;
int major; /* 中断处理函数 */
static irqreturn_t handle_irq(int irq, void *dev_id)
{
struct pin_desc *irq_pindesc = (struct pin_desc *)dev_id;//
unsigned int pinval; pinval = s3c2410_gpio_getpin(irq_pindesc->pin);//获取按键值:有按键按下返回按键值0
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
if(pinval)
{
/* 松开 */
key_val = 0x80 | (irq_pindesc->key_val);
}
else
{
/* 按下 */
key_val = irq_pindesc->key_val;
} ev_press = 1; /* 表示中断已经发生 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
return IRQ_HANDLED;
} static int buttons_dev_open(struct inode * inode, struct file * filp)
{
/* K1-EINT1,K2-EINT4,K3-EINT2,K4-EINT0
* 配置GPF1、GPF4、GPF2、GPF0为相应的外部中断引脚
* IRQT_BOTHEDGE应该改为IRQ_TYPE_EDGE_BOTH
*/
request_irq(IRQ_EINT1, handle_irq, IRQ_TYPE_EDGE_FALLING, "K1",&pins_desc[0]);
request_irq(IRQ_EINT4, handle_irq, IRQ_TYPE_EDGE_FALLING, "K2",&pins_desc[1]);
request_irq(IRQ_EINT2, handle_irq, IRQ_TYPE_EDGE_FALLING, "K3",&pins_desc[2]);
request_irq(IRQ_EINT0, handle_irq, IRQ_TYPE_EDGE_FALLING, "K4",&pins_desc[3]);
return 0;
} static int buttons_dev_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT1,&pins_desc[0]);
free_irq(IRQ_EINT4,&pins_desc[1]);
free_irq(IRQ_EINT2,&pins_desc[2]);
free_irq(IRQ_EINT0,&pins_desc[3]);
return 0;
} static ssize_t buttons_dev_read(struct file *file, char __user *user, size_t size,loff_t *ppos)
{
if (size != 1)
return -EINVAL;
//wait_event_interruptible(button_waitq, ev_press);//使用poll机制。这里就不须要再推断要不要进入睡眠了。 copy_to_user(user, &key_val, 1); /* 将ev_press清零 */
ev_press = 0;
return 1;
} //////////////////////////关键点///////////////////////////////////////
static unsigned int buttons_dev_poll(struct file *file, poll_table *wait) //该函数一旦被调用就触发poll机制
{
unsigned int mask = 0; /* 该函数,仅仅是将进程挂在button_waitq队列上。而不是马上休眠 */
poll_wait(file, &button_waitq, wait);
/***
* 如果进程还poll在上面这一函数里边。尚未超时,如果此时有中断到来,中断处理程序将ev_press置位,然后唤醒休眠队列上相应的进程
***/
/* 进程唤醒之后,立刻往下运行。唤醒的可能原因:超时/中断处理 */
if(ev_press)
{
mask |= POLLIN | POLLRDNORM; /* 有数据可读 */
} /* 如果有按键按下时,mask |= POLLIN | POLLRDNORM,否则mask = 0 */
return mask;
}
/////////////////////////////////////////////////////////////////////////////// /* File operations struct for character device */
static const struct file_operations buttons_dev_fops = {
.owner = THIS_MODULE,
.open = buttons_dev_open,
.read = buttons_dev_read,
.release = buttons_dev_close,
.poll = buttons_dev_poll,
}; /* 驱动入口函数 */
static int buttons_dev_init(void)
{
/* 主设备号设置为0表示由系统自己主动分配主设备号 */
major = register_chrdev(0, "buttons_dev", &buttons_dev_fops); /* 创建buttondev类 */
buttondev_class = class_create(THIS_MODULE, "buttondev"); /* 在buttondev类下创建buttons设备,供应用程序打开设备*/
buttons_device = device_create(buttondev_class, NULL, MKDEV(major, 0), NULL, "buttons");// return 0;
} /* 驱动出口函数 */
static void buttons_dev_exit(void)
{
unregister_chrdev(major, "buttons_dev");
device_unregister(buttons_device); //卸载类下的设备
class_destroy(buttondev_class); //卸载类
} /* 模块载入和卸载函数的修饰 */
module_init(buttons_dev_init);
module_exit(buttons_dev_exit); MODULE_AUTHOR("CLBIAO");
MODULE_DESCRIPTION("Just for Demon");
MODULE_LICENSE("GPL"); //遵循GPL协议
測试应用程序:app_poll.c
/* 文件的编译指令是arm-linux-gcc -static -o app_poll app_poll.c */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h> /* fourth_test
*/
int main(int argc ,char *argv[]) {
int fd;
unsigned char key_val;
struct pollfd fds;
int ret; fd = open("/dev/buttons",O_RDWR);
if (fd < 0)
{
printf("open error\n");
}
fds.fd = fd;//查询的文件
fds.events = POLLIN; //期待收到poll_in值。表示有数据
while(1)
{
/* A value of 0 indicates that the call timed out and no file descriptors were ready
* poll函数返回0时。表示5s时间到了,而这段时间里。没有事件发生"数据可读"
*/
ret = poll(&fds,1,5000);
if(ret == 0)
{
printf("time out\n");
} else /* 假设没有超时,则读出按键值 */
{
read(fd,&key_val,1);
printf("key_val = 0x%x\n",key_val);
}
}
return 0;
}
測试结果:
小结:poll流程图
字符设备之poll机制的更多相关文章
- 3.字符设备驱动------Poll机制
1.poll情景描述 以之前的按键驱动为例进行说明,用阻塞的方式打开按键驱动文件/dev/buttons,应用程序使用read()函数来读取按键的键值. ) { read(fd, &key_v ...
- 浅析Linux字符设备驱动程序内核机制
前段时间在学习linux设备驱动的时候,看了陈学松著的<深入Linux设备驱动程序内核机制>一书. 说实话.这是一本非常好的书,作者不但给出了在设备驱动程序开发过程中的所须要的知识点(如对 ...
- 【转】Linux高级字符设备之Poll操作
原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275559.html 在用户程序中,select()和poll()也是与设备阻塞与非阻塞 ...
- 字符驱动程序之——poll机制
关于这个韦老师给了一个简单的参考文档: poll机制分析 韦东山 2009.12.10 所有的系统调用,基本都可以在它的名字前加上“sys_”前缀,这就是它在内核中对应的函数.比如系统调用open.r ...
- Linux高级字符设备驱动 poll方法(select多路监控原理与实现)
1.什么是Poll方法,功能是什么? 2.Select系统调用(功能) Select系统调用用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程. int selec ...
- linux字符驱动之poll机制按键驱动
在上一节中,我们讲解了如何自动创建设备节点,实现一个中断方式的按键驱动.虽然中断式的驱动,效率是蛮高的,但是大家有没有发现,应用程序的死循环里的读函数是一直在读的:在实际的应用场所里,有没有那么一种情 ...
- 字符设备驱动(六)按键poll机制
title: 字符设备驱动(六)按键poll机制 tags: linux date: 2018-11-23 18:57:40 toc: true --- 字符设备驱动(六)按键poll机制 引入 在字 ...
- 字符设备驱动笔记——poll机制分析(七)
poll机制分析 所有的系统调用,基于都可以在它的名字前加上“sys_”前缀,这就是它在内核中对应的函数.比如系统调用open.read.write.poll,与之对应的内核函数为:sys_open. ...
- 韦东山驱动视频笔记——3.字符设备驱动程序之poll机制
linux内核版本:linux-2.6.30.4 目的:我们在中断方式的按键应用程序中,如果没有按键按下,read就会永远在那等待,所以如果在这个程序里还想做其他事就不可能了.因此我们这次改进它,让它 ...
随机推荐
- [整理] webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用
webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用 https://www.cnblogs.com/moqiutao/p/7496718.html
- Java垃圾回收之新生代垃圾收集器
问题:什么是Stop-the-World? 1.JVM由于要执行GC而停止了应用程序的执行 2.任何一种GC算法中都会发生 3.多数GC优化通过减少Stop-the-world发生的时间来提高程序的性 ...
- JAVA基础——网络编程之网络链接
一.网络编程基本概念 1.OSI与TCP/IP体系模型 2.IP和端口 解决了文章最开始提到的定位的问题. IP在互联网中能唯一标识一台计算机,是每一台计算机的唯一标识(身份证):网络编程是和远程计算 ...
- Ubuntu 开机出现 "Your system is running in low-graphics mode"
Ubuntu 开机出现 "Your system is running in low-graphics mode" 可能是权限问题 按网上的方法发现sudo命令无法使用,且系统变为 ...
- 普通平衡树(treap)
题干:6种操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相同的数,因输出最小的排名) 4. 查询排名为x的数 5. 求x的前驱(前驱定义为小于 ...
- MySQL教程之存储过程与函数
存储程序分为存储过程和函数 可以使用CALL来调用存储过程,只能输出变量返回值.存储过程可以调用其他存储过程 函数可以从语句外调用,也能返回标量值 什么是存储过程? 简单的说,就是一组SQL语句集,功 ...
- python之多线程与多进程
1. 多进程与多线程 (1)背景:为何需要多进程或者多线程:在同一时间里,同一个计算机系统中如果允许两个或者两个以上的进程处于运行状态,这便是多任务.多任务会带来的好处例如用户边听歌.边上网.边打印, ...
- 使用js获取页面的各种高度
使用js获取相关高度: 获取网页被滚动条卷去的高度——兼容写法: scrollHeight = documen.body.scrollTop || document.documentElement.s ...
- c和c++如何把一个整数转化为string
c和c++如何把一个整数转化为string C++: 一.string转int的方式 采用最原始的string, 然后按照十进制的特点进行算术运算得到int,但是这种方式太麻烦,这里不介绍了. 采用标 ...
- 洛谷 P1938 [USACO09NOV] 找工就业Job Hunt
这道题可以说是一个复活SPFA的题 因为数据比较小,SPFA也比较简单 那就复习(复读)一次SPFA吧 #include<iostream> #include<cstdio> ...