一、poll机制
为什么我们需要poll机制呢。之前的测试程序是这样:
while (1)
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
在没有poll机制的情况下,大部分时间程序都处在read中休眠的那个位置。如果我们不想让程序停在这个位置,而是希望当有按键按下时,我们再去read,因此我们编写poll函数,测试程序调用poll函数根据返回值,来决定是否执行read函数。
poll机制作用:相当于定时器,设置一定时间使进程等待资源,如果时间到了中断还处于睡眠状态(等待队列),poll机制就会唤醒中断,获取一次资源
1.1、poll机制内核框架
如下图所示,在用户层上,使用poll或select函数时,和open、read那些函数一样,也要进入内核sys_poll函数里,接下来我们分析sys_poll函数来了解poll机制(位于/fs/select.c)
1)sys_poll代码如下:
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,long timeout_msecs)
{
if (timeout_msecs > 0) //参数timeout>0
{
timeout_jiffies = msecs_to_jiffies(timeout_msecs); //通过频率来计算timeout时间需要多少计数值
}
else
{
timeout_jiffies = timeout_msecs; //如果timeout时间为0,直接赋值
}
return do_sys_poll(ufds, nfds, &timeout_jiffies); //调用do_sys_poll。
}
2)然后进入do_sys_poll(位于fs/select.c):
int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
{
...
/*初始化一个poll_wqueues变量table*/
poll_initwait(&table);
...
fdcount = do_poll(nfds, head, &table, timeout);
...
}
3)进入poll_initwait函数,发现主要实现以下一句,后面会分析这里:
table ->pt-> qproc=__pollwait; //__pollwait将在驱动的poll函数里的poll_wait函数用到
4)然后进入do_poll函数, (位于fs/select.c):
static int do_poll(unsigned int nfds, struct poll_list *list, struct poll_wqueues *wait, s64 *timeout)
{
...
for (;;)
{
...
set_current_state(TASK_INTERRUPTIBLE); //设置为等待队列状态
...
for (; pfd != pfd_end; pfd++) { //for循环运行多个poll机制
/*将pfd和pt参数代入我们驱动程序里注册的poll函数*/
if (do_pollfd(pfd, pt)) //若返回非0,count++,后面并退出
{ count++;
pt = NULL; }
}
...
/*count非0(.poll函数返回非0),timeout超时计数到0,有信号在等待*/
if (count || !*timeout || signal_pending(current))
break;
...
/*进入休眠状态,只有当timeout超时计数到0,或者被中断唤醒才退出,*/
__timeout = schedule_timeout(__timeout);
...
}
__set_current_state(TASK_RUNNING); //开始运行
return count;
}
4.1)上面do_pollfd函数到底是怎么将pfd和pt参数代入的?代码如下(位于fs/select.c):
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
{
...
if (file->f_op && file->f_op->poll)
mask = file->f_op->poll(file, pwait);
...
return mask;
}
上面file->f_op 就是我们驱动里的file_oprations结构体,如下图所示:
所以do_pollfd(pfd, pt)就执行了我们驱动程序里的.poll(pfd, pt)函数(第2小节开始分析.poll函数)
4.2)当poll进入休眠状态后,又是谁来唤醒它?这就要分析我们的驱动程序.poll函数(第1.2小节开始分析.poll函数)
1.2、编写并分析.poll函数
在上一节驱动程序里添加以下代码:
#include <linux/poll.h> //添加头文件
/* .poll驱动函数: third_poll */
static unsigned int third_poll(struct file *fp, poll_table * wait) //fp:文件 wait:
{
unsigned int mask =0;
poll_wait(fp, &button_wait, wait);
if(even_press) //中断事件标志, 1:退出休眠状态 0:进入休眠状态
mask |= POLLIN | POLLRDNORM ;
return mask; //当超时,就返给应用层为0 ,被唤醒了就返回POLLIN | POLLRDNORM ;
}
static struct file_operations third_drv_fops={
.owner = THIS_MODULE,
.open = third_drv_open,
.read = third_drv_read,
.release=third_drv_class,
.poll = third_poll, //创建.poll函数
};
1)在我们1.1-4)小节do_poll函数有一段以下代码:
if (do_pollfd(pfd, pt)) //若返回非0,count++,后面并退出
{
count++;
pt = NULL;
}
且在1.1-4.1)分析出: do_pollfd(pfd, pt)就是指向的驱动程序third_poll()函数,
所以当我们有按键按下时, 驱动函数third_poll()就会返回mask非0值,然后在内核函数do_poll里的count就++,poll机制并退出睡眠.
2)分析在内核中poll机制如何被驱动里的中断唤醒的
在驱动函数third_poll()里有以下一句:
poll_wait(fp, &button_wait, wait);

如上图所示,代入参数,poll_wait()就是执行了: p->qproc(filp, button_wait, p);
刚好对应了我们1.1-3)小节的:
table ->pt-> qproc=__pollwait;
所以poll_wait()函数就是调用了: __pollwait(filp, button_wait, p);
然后我们来分析__pollwait函数,pollwait的代码如下:
static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,poll_table *p)
{
... ...
//把current进程挂载到&entry->wait下
init_waitqueue_entry(&entry->wait, current);
//再&entry->wait把添加到到button_wait中断下
add_wait_queue(wait_address, &entry->wait);
}
它是将poll进程添加到了button_wait中断队列里,这样,一有按键按下时,在中断服务函数里就会唤醒button_wait中断,同样也会唤醒poll机制,使poll机制重新进程休眠计数
3)驱动程序.poll函数返回值介绍
当中断休眠状态时,返回mask为0
当运行时返回:mask |= POLLIN | POLLRDNORM
其中参数意义如下图:
所以POLLIN | POLLRDNORM:普通数据可读|优先级带数据可读
mask就返回到应用层poll函数,
1.3、改进测试程序third_poll_test.c(添加poll函数)
在linux中可以通过man poll 来查看poll函数如何使用
poll函数原型如下(#include <poll.h>):
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
参数介绍:
1) *fds:是一个poll描述符结构体数组(可以处理多个poll),结构体pollfd如下:
struct pollfd {
int fd; /* file descriptor 文件描述符*/
short events; /* requested events 请求的事件*/
short revents; /* returned events 返回的事件(函数返回值)*/
};
其中events和revents值参数如下图:
2) nfds:表示多少个poll,如果1个,就填入1
3) timeout:定时多少ms
返回值介绍:
返回值为0:表示超时或者fd文件描述符无法打开
返回值为 -1:表示错误
返回值为>0时 :就是以下几个常量
最终改进的测试代码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <poll.h> //添加poll头文件
/*useg: thirdtext */
int main(int argc,char **argv)
{
int fd,ret;
unsigned int val=0;
struct pollfd fds; //定义poll文件描述结构体
fd=open("/dev/buttons",O_RDWR);
if(fd<0)
{ printf("can't open!!!\n");
return -1;
}
fds.fd=fd;
fds.events= POLLIN; //请求类型是 普通或优先级带数据可读
while(1)
{
ret=poll(&fds,1,5000) ; //一个poll, 定时5000ms,进入休眠状态
if(ret==0) //超时
{
printf("time out \r\n");
}
else if(ret>0) //poll机制被唤醒,表示有数据可读
{
read(fd,&val,1); //读取一个值
printf("key_val=0X%x\r\n",val);
}
}
return 0;
}
效果如下:
若5S没有数据,则打印time out。
二、使用异步通知
之前学的应用层都是:
1)查询方式:一直读
2)中断方式.同样一直读,直到中断进程唤醒
3)poll机制:一直在poll函数中睡眠,一定时间读一次
以上3种,我们都是让应用程序主动去读,本节我们学习异步通知,它的作用就是当驱动层有数据时,主动告诉应用程序,然后应用程序再来读, 这样,应用程序就可以干其它的事情,不必一直读
比如:kill -9 pid ,其实就是通过发信号杀死进程,kill发数据9给指定id号进程
2.1、怎么来收信号?
通过signal函数来实现获取信号,先来看看以下例子:
头函数:
sighandler_t signal(int signum, sighandler_t handler);
函数说明:让一个信号与与一个函数对应,每当接收到这个信号就会调用相应的函数。
头文件: #include <signal.h>
参数1: 指明了所要处理的信号类型
信号有以下几种:
SIGINT 键盘中断(如break、ctrl+c键被按下)
SIGUSR1 用户自定义信号1,kill的USR1(10)信号
SIGUSR2 用户自定义信号2, kill的USR2(12)信号
参数2: 信号产生后需要处理的方式,可以是个函数
代码如下:
#include <stdio.h>
#include <signal.h>
void my_signal_run(int signum) //信号处理函数
{
static int run_cnt=0;
printf("signal = %d, %d count\r\n",signum,++count);
}
int main(int argc,char **argv)
{
signal(SIGUSR1,my_signal_run); //调用signal函数,让指定的信号SIGUSR1与处理函数my_signal_run对应。
while(1)
{
sleep(1000); //去做其它事,睡眠1s
}
return 0;
}
然后运行后,使用kill -10 802,可以看到产生单信号USR1(10)时就会调用my_signal_run()打印数据。
# kill -10 802
# signal = 10, 1 count
# kill -10 802
# signal = 10, 2 count
2.2、实现异步通知的要求
1)应用程序要实现有:注册信号处理函数,使用signal函数
2)谁来发?驱动来发
3)发给谁?驱动发给应用程序,但应用程序必须告诉驱动PID,
4)怎么发?驱动程序调用kill_fasync函数
2.3、写驱动函数
我们在之前的中断程序上修改
1)定义“异步信号结构体”变量
static struct fasync_struct * button_async;
2)在file_operations结构体添加成员.fasync函数,并写函数
static struct file_operations third_drv_fops={
.owner = THIS_MODULE,
.open = fourth_drv_open,
.read = fourth _drv_read,
.release= fourth _drv_class,
.poll = fourth _poll,
.fasync = fourth_fasync //添加初始化异步信号函数
};
static int fourth_fasync (int fd, struct file *file, int on)
{
return fasync_helper(fd, file, on, & button_async); //初始化button_async结构体,就能使用kill_fasync()了
}
成员.fasync函数又是什么情况下使用?
是被应用程序调用,在下面第2.4小节会见到。
3)在buttons_irq中断服务函数里发送信号:
kill_fasync(&button_async, SIGIO, POLL_IN);
//当有中断时,就发送SIGIO信号给应用层,应用层就会触发与SIGIO信号对应的函数
4)驱动程序代码如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/poll.h>
static struct class *fourthdrv_class;
static struct class_device *fourthdrv_class_devs;
/* 声明等待队列类型中断 button_wait */
static DECLARE_WAIT_QUEUE_HEAD(button_wait);
/* 异步信号结构体变量 */
static struct fasync_struct * button_async;
/*
* 定义中断事件标志
* 0:进入等待队列 1:退出等待队列
*/
static int even_press=0;
/*
* 定义全局变量key_val,保存key状态
*/
static int key_val=0;
/*
*引脚描述结构体
*/
struct pin_desc{
unsigned int pin;
unsigned int pin_status;
};
/*
*key初始状态(没有按下): 0x01,0x02,0x03,0x04
*key状态(按下): 0x81,0x82,0x83,0x84
*/
struct pin_desc pins_desc[4]={
{S3C2410_GPF0,0x01 },
{S3C2410_GPF2, 0x02 },
{S3C2410_GPG3, 0x03 },
{S3C2410_GPG11,0x04},
} ;
int fourth_drv_class(struct inode *inode, struct file *file) //卸载中断
{
free_irq(IRQ_EINT0,&pins_desc[0]);
free_irq(IRQ_EINT2,&pins_desc[1]);
free_irq(IRQ_EINT11,&pins_desc[2]);
free_irq(IRQ_EINT19,&pins_desc[3]);
return 0;
}
/*
* 确定是上升沿还是下降沿
*/
static irqreturn_t buttons_irq (int irq, void *dev_id) //中断服务函数
{
struct pin_desc *pindesc=(struct pin_desc *)dev_id; //获取引脚描述结构体
unsigned int pin_val=0;
pin_val=s3c2410_gpio_getpin(pindesc->pin);
if(pin_val)
{
/*没有按下 (下降沿),清除0x80*/
key_val=pindesc->pin_status&0xef;
}
else
{
/*按下(上升沿),加上0x80*/
key_val=pindesc->pin_status|0x80;
}
even_press=1; //退出等待队列
wake_up_interruptible(&button_wait); //唤醒 中断
kill_fasync(&button_async, SIGIO, POLL_IN); //发送SIGIO信号给应用层
return IRQ_RETVAL(IRQ_HANDLED);
}
static int fourth_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S1",&pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE, "S2", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq,IRQT_BOTHEDGE, "S3", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq,IRQT_BOTHEDGE, "S4", &pins_desc[3]);
return 0;
}
static int fourth_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
/*将中断 进入等待队列(休眠状态)*/
wait_event_interruptible(button_wait, even_press);
/*有按键按下,退出等待队列,上传key_val 给用户层*/
if(copy_to_user(buf,&key_val,sizeof(key_val)))
return EFAULT;
even_press=0;
return 0;
}
static unsigned fourth_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_wait, wait); // 不会立即休眠
if (even_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int fourth_fasync (int fd, struct file *file, int on)
{
return fasync_helper(fd, file, on, & button_async); //初始化button_async结构体,就能使用kill_fasync()了
}
static struct file_operations fourth_drv_fops={
.owner = THIS_MODULE,
.open = fourth_drv_open,
.read = fourth_drv_read,
.release=fourth_drv_class, //里面添加free_irq函数,来释放中断服务函数
.poll = fourth_poll,
.fasync= fourth_fasync, //初始化异步信号函数
};
volatile int fourth_major;
static int fourth_drv_init(void)
{
fourth_major=register_chrdev(0,"fourth_drv",&fourth_drv_fops); //创建驱动
fourthdrv_class=class_create(THIS_MODULE,"fourth_dev"); //创建类名
fourthdrv_class_devs=class_device_create(fourthdrv_class, NULL, MKDEV(fourth_major,0), NULL,"buttons");
return 0;
}
static int fourth_drv_exit(void)
{
unregister_chrdev(fourth_major,"fourth_drv"); //卸载驱动
class_device_unregister(fourthdrv_class_devs); //卸载类设备
class_destroy(fourthdrv_class); //卸载类
return 0;
}
module_init(fourth_drv_init);
module_exit(fourth_drv_exit);
MODULE_LICENSE("GPL v2");
2.4、写应用测试程序
步骤如下:
1)signal(SIGIO, my_signal_fun);
调用signal函数,当接收到SIGIO信号就进入my_signal_fun函数,读取驱动层的数据
2) fcntl(fd,F_SETOWN,getpid());
指定进程做为fd文件的”属主”,内核收到F_SETOWN命令,就会设置pid(驱动无需处理),这样fd驱动程序就知道发给哪个进程
3) oflags=fcntl(fd,F_GETFL);
获取fd的文件状态标志
4) fcntl(fd,F_SETFL, oflags| FASYNC );
添加FASYNC状态标志,会调用驱动中成员.fasync函数,执行fasync_helper()来初始化异步信号结构体
这4个步骤执行后,一旦有驱动层有SIGIO信号时,进程就会收到
应用层代码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
int fd,ret;
void my_signal_fun(int signame) //有信号来了
{
read( fd, &ret, 1); //读取驱动层数据
printf("key_vale=0X%x\r\n",ret);
}
/*useg: fourthtext */
int main(int argc,char **argv)
{
int oflag;
unsigned int val=0;
fd=open("/dev/buttons",O_RDWR);
if(fd<0)
{
printf("can't open!!!\n");
return -1;
}
signal(SIGIO,my_signal_fun); //指定的信号SIGIO与处理函数my_signal_run对应
fcntl( fd, F_SETOWN, getip()); //指定进程作为fd 的属主,发送pid给驱动
oflag=fcntl( fd, F_GETFL); //获取fd的文件标志状态
fcntl( fd, F_SETFL, oflag|FASYNC); //添加FASYNC状态标志,调用驱动层.fasync成员函数
while(1)
{
sleep(1000); //做其它的事情
}
return 0;
}
2.5、运行查看结果

本文详细解析了Linux下的poll机制工作原理,包括内核框架、编写与分析.poll函数,以及如何使用poll函数进行非阻塞式I/O操作。同时,介绍了异步通知的概念,演示了如何利用信号和fasync函数实现驱动层向应用层的主动数据通知。

8464

被折叠的 条评论
为什么被折叠?



