Linux 设备驱动中的 I/O模型(二)—— 异步通知和异步I/O
来源:互联网 发布:知乎关注最多的问题 编辑:程序博客网 时间:2024/05/18 18:04
阻塞和非阻塞访问、poll() 函数提供了较多地解决设备访问的机制,但是如果有了异步通知整套机制就更加完善了。
异步通知的意思是:一旦设备就绪,则主动通知应用程序,这样应用程序根本就不需要查询设备状态,这一点非常类似于硬件上“中断”的概念,比较准确的称谓是“信号驱动的异步I/O”。信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的。信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上,进程也不知道信号到底什么时候到达。
阻塞I/O意味着移植等待设备可访问后再访问,非阻塞I/O中使用poll()意味着查询设备是否可访问,而异步通知则意味着设备通知自身可访问,实现了异步I/O。由此可见,这种方式I/O可以互为补充。
1、异步通知的概念和作用
影响:阻塞–应用程序无需轮询设备是否可以访问
非阻塞–中断进行通知
即:由驱动发起,主动通知应用程序
2、linux异步通知编程
2.1 linux信号
作用:linux系统中,异步通知使用信号来实现
函数原型为:
void (*signal(int signum,void (*handler))(int)))(int)
原型比较难理解可以分解为
typedef void(*sighandler_t)(int); sighandler_t signal(int signum,sighandler_t handler);
第一个参数是指定信号的值,第二个参数是指定针对前面信号的处理函数
2.2 信号的处理函数(在应用程序端捕获信号)
signal()函数
//启动信号机制 void sigterm_handler(int sigo){char data[MAX_LEN];int len;len = read(STDIN_FILENO,&data,MAX_LEN);data[len] = 0;printf("Input available:%s\n",data);exit(0); } int main(void){ int oflags;//启动信号驱动机制signal(SIGIO,sigterm_handler);fcntl(STDIN_FILENO,F_SETOWN,getpid());oflags = fcntl(STDIN_FILENO,F_GETFL);fctcl(STDIN_FILENO,F_SETFL,oflags | FASYNC);//建立一个死循环,防止程序结束 whlie(1); return 0;}
2.3 信号的释放 (在设备驱动端释放信号)
为了是设备支持异步通知机制,驱动程序中涉及以下3项工作
(1)、支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应的进程ID。不过此项工作已由内核完成,设备驱动无须处理。
(2)、支持F_SETFL命令处理,每当FASYNC标志改变时,驱动函数中的fasync()函数得以执行。因此,驱动中应该实现fasync()函数
(3)、在设备资源中可获得,调用kill_fasync()函数激发相应的信号
设备驱动中异步通知编程比较简单,主要用到一项数据结构和两个函数。这个数据结构是fasync_struct 结构体,两个函数分别是:
a -- 处理FASYNC标志变更
int fasync_helper(int fd,struct file *filp,int mode,struct fasync_struct **fa);
b -- 释放信号用的函数
void kill_fasync(struct fasync_struct **fa,int sig,int band);
和其他结构体指针放到设备结构体中,模板如下
struct xxx_dev{struct cdev cdev;...struct fasync_struct *async_queue;//异步结构体指针};
在设备驱动中的fasync()函数中,只需简单地将该函数的3个参数以及fasync_struct结构体指针的指针作为第四个参数传入fasync_helper()函数就可以了,模板如下
static int xxx_fasync(int fd,struct file *filp, int mode){struct xxx_dev *dev = filp->private_data;return fasync_helper(fd, filp, mode, &dev->async_queue);}在设备资源可获得时应该调用kill_fasync()函数释放SIGIO信号,可读时第三个参数为POLL_IN,可写时第三个参数为POLL_OUT,模板如下
static ssize_t xxx_write(struct file *filp,const char __user *buf,size_t count,loff_t *ppos){struct xxx_dev *dev = filp->private_data;... if(dev->async_queue) kill_fasync(&dev->async_queue,GIGIO,POLL_IN); ...}最后在文件关闭时,要将文件从异步通知列表中删除
int xxx_release(struct inode *inode,struct file *filp){xxx_fasync(-1,filp,0); ...return 0; }
3、下面是个实例:
hello.c
#include <linux/module.h>#include <linux/fs.h>#include <linux/cdev.h>#include <linux/device.h>#include <asm/uaccess.h>#include <linux/fcntl.h>static int major = 250;static int minor=0;static dev_t devno;static struct class *cls;static struct device *test_device;static char temp[64]={0};static struct fasync_struct *fasync;static int hello_open (struct inode *inode, struct file *filep){return 0;}static int hello_release(struct inode *inode, struct file *filep){return 0;}static ssize_t hello_read(struct file *filep, char __user *buf, size_t len, loff_t *pos){if(len>64){len =64;}if(copy_to_user(buf,temp,len)){return -EFAULT;}return len;}static ssize_t hello_write(struct file *filep, const char __user *buf, size_t len, loff_t *pos){if(len>64){len = 64;}if(copy_from_user(temp,buf,len)){return -EFAULT;}printk("write %s\n",temp);kill_fasync(&fasync, SIGIO, POLL_IN);return len;}static int hello_fasync (int fd, struct file * file, int on){return fasync_helper(fd, file, on, &fasync);}static struct file_operations hello_ops={.open = hello_open,.release = hello_release,.read =hello_read,.write=hello_write,};static int hello_init(void){int ret;devno = MKDEV(major,minor);ret = register_chrdev(major,"hello",&hello_ops);cls = class_create(THIS_MODULE, "myclass");if(IS_ERR(cls)){unregister_chrdev(major,"hello");return -EBUSY;}test_device = device_create(cls,NULL,devno,NULL,"hello");//mknod /dev/helloif(IS_ERR(test_device)){class_destroy(cls);unregister_chrdev(major,"hello");return -EBUSY;}return 0;}static void hello_exit(void){device_destroy(cls,devno);class_destroy(cls);unregister_chrdev(major,"hello");printk("hello_exit \n");}MODULE_LICENSE("GPL");module_init(hello_init);module_exit(hello_exit);
test.c
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <signal.h>static int fd,len;static char buf[64]={0};void func(int signo){printf("signo %d \n",signo);read(fd,buf,64);printf("buf=%s \n",buf);}main(){int flage,i=0;fd = open("/dev/hello",O_RDWR);if(fd<0){perror("open fail \n");return ;}fcntl(fd,F_SETOWN,getpid());flage = fcntl(fd,F_GETFL);fcntl(fd,F_SETFL,flage|FASYNC);signal(SIGIO,func);while(1){sleep(1);printf("%d\n",i++);}close(fd);}
- Linux 设备驱动中的 I/O模型(二)—— 异步通知和异步I/O
- Linux 设备驱动中的 I/O模型(二)—— 异步通知和异步I/O
- Linux设备驱动中的异步通知与异步I/O
- Linux设备驱动中的异步通知与异步I/O
- linux设备驱动--异步通知和异步I/O
- Linux设备驱动中的异步通知与同步I/O
- linux设备驱动之异步通知与异步I/O
- 第9章 Linux设备驱动中的异步通知与异步I/O
- Linux 设备驱动中的异步通知与异步 I/O(2)
- Linux设备驱动之阻塞I/O与异步通知
- 深入浅出:Linux设备驱动之异步通知和异步I/O
- 深入浅出:Linux设备驱动之异步通知和异步I/O
- Linux设备驱动开发详解-Note(18)---Linux 设备驱动中的异步通知与异步 I/O(1)
- Linux设备驱动开发详解-Note(19)---Linux 设备驱动中的异步通知与异步 I/O(2)
- linux驱动学习--第十五天:第九章 Linux 设备驱动中的异步通知与异步I/O
- Linux 设备驱动中的 I/O模型(一)—— 阻塞和非阻塞I/O
- Linux 设备驱动中的 I/O模型(一)—— 阻塞和非阻塞I/O
- Linux I/O模型 与 Java I/O模型、驱动中的异步函数 的区别。
- Leetcode 147. Insertion Sort List(链表实现快排)
- android weight(权重)的具体分析
- LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)
- 凭啥Java的运行环境称虚拟机,Python的只能称解释器
- 今年第一次面试
- Linux 设备驱动中的 I/O模型(二)—— 异步通知和异步I/O
- Leetcode Best Time to Buy and Sell Stock III
- grep排除搜索的目录
- 视频编解码学习之二:数字视频
- Interop with PROPVARIANTs in .NET -- C# 调用C++ COM 系列笔记
- ubuntu14.04安装telnet
- Java实现抽象工厂模式+java的反射的机制
- Android中的门面模式
- 性能调优攻略