阻塞和非阻塞访问、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()函数

[cpp] view plaincopy
  1. //启动信号机制
  2. void sigterm_handler(int sigo)
  3. {
  4. char data[MAX_LEN];
  5. int len;
  6. len = read(STDIN_FILENO,&data,MAX_LEN);
  7. data[len] = 0;
  8. printf("Input available:%s\n",data);
  9. exit(0);
  10. }
  11. int main(void)
  12. {
  13. int oflags;
  14. //启动信号驱动机制
  15. signal(SIGIO,sigterm_handler);
  16. fcntl(STDIN_FILENO,F_SETOWN,getpid());
  17. oflags = fcntl(STDIN_FILENO,F_GETFL);
  18. fctcl(STDIN_FILENO,F_SETFL,oflags | FASYNC);
  19. //建立一个死循环,防止程序结束
  20. whlie(1);
  21. return 0;
  22. }

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);

和其他结构体指针放到设备结构体中,模板如下

[cpp] view plaincopy
  1. struct xxx_dev{
  2. struct cdev cdev;
  3. ...
  4. struct fasync_struct *async_queue;
  5. //异步结构体指针
  6. };

在设备驱动中的fasync()函数中,只需简单地将该函数的3个参数以及fasync_struct结构体指针的指针作为第四个参数传入fasync_helper()函数就可以了,模板如下

[cpp] view plaincopy
  1. static int xxx_fasync(int fd,struct file *filp, int mode)
  2. {
  3. struct xxx_dev *dev = filp->private_data;
  4. return fasync_helper(fd, filp, mode, &dev->async_queue);
  5. }

在设备资源可获得时应该调用kill_fasync()函数释放SIGIO信号,可读时第三个参数为POLL_IN,可写时第三个参数为POLL_OUT,模板如下

[cpp] view plaincopy
  1. static ssize_t xxx_write(struct file *filp,const char __user *buf,size_t count,loff_t *ppos)
  2. {
  3. struct xxx_dev *dev = filp->private_data;
  4. ...
  5. if(dev->async_queue)
  6. kill_fasync(&dev->async_queue,GIGIO,POLL_IN);
  7. ...
  8. }

最后在文件关闭时,要将文件从异步通知列表中删除

[cpp] view plaincopy
  1. int xxx_release(struct inode *inode,struct file *filp)
  2. {
  3. xxx_fasync(-1,filp,0);
  4. ...
  5. return 0;
  6. }

3、下面是个实例:

hello.c

[cpp] view plaincopy
  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include <linux/cdev.h>
  4. #include <linux/device.h>
  5. #include <asm/uaccess.h>
  6. #include <linux/fcntl.h>
  7. static int major = 250;
  8. static int minor=0;
  9. static dev_t devno;
  10. static struct class *cls;
  11. static struct device *test_device;
  12. static char temp[64]={0};
  13. static struct fasync_struct *fasync;
  14. static int hello_open (struct inode *inode, struct file *filep)
  15. {
  16. return 0;
  17. }
  18. static int hello_release(struct inode *inode, struct file *filep)
  19. {
  20. return 0;
  21. }
  22. static ssize_t hello_read(struct file *filep, char __user *buf, size_t len, loff_t *pos)
  23. {
  24. if(len>64)
  25. {
  26. len =64;
  27. }
  28. if(copy_to_user(buf,temp,len))
  29. {
  30. return -EFAULT;
  31. }
  32. return len;
  33. }
  34. static ssize_t hello_write(struct file *filep, const char __user *buf, size_t len, loff_t *pos)
  35. {
  36. if(len>64)
  37. {
  38. len = 64;
  39. }
  40. if(copy_from_user(temp,buf,len))
  41. {
  42. return -EFAULT;
  43. }
  44. printk("write %s\n",temp);
  45. kill_fasync(&fasync, SIGIO, POLL_IN);
  46. return len;
  47. }
  48. static int hello_fasync (int fd, struct file * file, int on)
  49. {
  50. return fasync_helper(fd, file, on, &fasync);
  51. }
  52. static struct file_operations hello_ops=
  53. {
  54. .open = hello_open,
  55. .release = hello_release,
  56. .read =hello_read,
  57. .write=hello_write,
  58. };
  59. static int hello_init(void)
  60. {
  61. int ret;
  62. devno = MKDEV(major,minor);
  63. ret = register_chrdev(major,"hello",&hello_ops);
  64. cls = class_create(THIS_MODULE, "myclass");
  65. if(IS_ERR(cls))
  66. {
  67. unregister_chrdev(major,"hello");
  68. return -EBUSY;
  69. }
  70. test_device = device_create(cls,NULL,devno,NULL,"hello");//mknod /dev/hello
  71. if(IS_ERR(test_device))
  72. {
  73. class_destroy(cls);
  74. unregister_chrdev(major,"hello");
  75. return -EBUSY;
  76. }
  77. return 0;
  78. }
  79. static void hello_exit(void)
  80. {
  81. device_destroy(cls,devno);
  82. class_destroy(cls);
  83. unregister_chrdev(major,"hello");
  84. printk("hello_exit \n");
  85. }
  86. MODULE_LICENSE("GPL");
  87. module_init(hello_init);
  88. module_exit(hello_exit);

test.c

[cpp] view plaincopy
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <signal.h>
  6. static int fd,len;
  7. static char buf[64]={0};
  8. void func(int signo)
  9. {
  10. printf("signo %d \n",signo);
  11. read(fd,buf,64);
  12. printf("buf=%s \n",buf);
  13. }
  14. main()
  15. {
  16. int flage,i=0;
  17. fd = open("/dev/hello",O_RDWR);
  18. if(fd<0)
  19. {
  20. perror("open fail \n");
  21. return ;
  22. }
  23. fcntl(fd,F_SETOWN,getpid());
  24. flage = fcntl(fd,F_GETFL);
  25. fcntl(fd,F_SETFL,flage|FASYNC);
  26. signal(SIGIO,func);
  27. while(1)
  28. {
  29. sleep(1);
  30. printf("%d\n",i++);
  31. }
  32. close(fd);
  33. }

Linux 设备驱动中的 I/O模型(二)—— 异步通知和异步I/O相关推荐

  1. Linux 设备驱动中的 I/O模型(一)—— 阻塞和非阻塞I/O

    在前面学习网络编程时,曾经学过I/O模型 Linux 系统应用编程--网络编程(I/O模型),下面学习一下I/O模型在设备驱动中的应用. 回顾一下在Unix/Linux下共有五种I/O模型,分别是: ...

  2. Linux设备驱动中的并发控制总结

    并发(concurrency)指的是多个执行单元同时.并行被执行.而并发的执行单元对共享资源(硬件资源和软件上的全局.静态变量)的访问则容易导致竞态(race conditions).   SMP是一 ...

  3. linux 两个驱动 竞态,第7章 Linux设备驱动中的并发控制之一(并发与竞态)

    本章导读 Linux设备驱动中必须解决的一个问题是多个进程对共享资源的并发访问,并发的访问会导致竞态(竞争状态). Linux提供了多种解决竞态问题的方式,这些方式适合不同的应用场景. 7.1讲解了并 ...

  4. linux 设备驱动阻塞,深入浅出:Linux设备驱动中的阻塞和非阻塞I/O

    今天写的是Linux设备驱动中的阻塞和非阻塞I/0,何谓阻塞与非阻塞I/O?简单来说就是对I/O操作的两种不同的方式,驱动程序可以灵活的支持用户空间对设备的这两种访问方式. 一.基本概念: 阻塞操作 ...

  5. Linux设备驱动开发详解:第7章 Linux设备驱动中的并发控制

    7.1并发与竞态 (1).竞态的发生场景:CPU0的进程与CPU1的进程之间.CPU0的中断与CPU1的进程之间.CPU0的中断与CPU1的中断之间: (2).解决竞态问题的途径是保证对共享资源的互斥 ...

  6. linux write引起进程挂起,Linux设备驱动中的阻塞与非阻塞总结

    Linux设备驱动中的阻塞与非阻塞总结 阻塞操作是指,在执行设备操作时,若不能获得资源,则进程挂起直到满足可操作的条件再进行操作. 非阻塞操作的进程在不能进行设备操作时,并不挂起.被挂起的进程进入sl ...

  7. Linux设备驱动中的阻塞和非阻塞IO

    这篇文章我们来了解下Linux设备驱动中阻塞和非阻塞. 阻塞:阻塞是指执行设备操作时,如果不能获得设备资源,则挂起进程,是进程进入休眠模式,直到设备资源可以获取. 非阻塞:非阻塞是在不能获取设备资源时 ...

  8. linux编写驱动后write已杀死_《Linux4.0设备驱动开发详解》笔记--第九章:Linux设备驱动中的异步通知与同步I/O...

    在设备驱动中使用异步通知可以使得对设备的访问可进行时,由驱动主动通知应用程序进行访问.因此,使用无阻塞I/O的应用程序无需轮询设备是否可访问,而阻塞访问也可以被类似"中断"的异步通 ...

  9. Linux设备驱动中的阻塞与非阻塞I/O

    阻塞和非阻塞I/O是设备访问的两种不同模式,驱动程序可以灵活的支持用户空间对设备的这两种访问方式 本例子讲述了这两者的区别 并实现I/O的等待队列机制, 并进行了用户空间的验证 基本概念: 1> ...

最新文章

  1. 大数据-spark-hbase-hive等学习视频资料
  2. 高效职场人不得不懂的“脑”知识
  3. 清华中德大数据研究学生交换项目成果报告会成功举办
  4. 1595 hdu find the longest of the shortest
  5. 动态规划—完全背包问题
  6. 初探奥尔良(Orleans)
  7. 如何解决PIP命令不可用
  8. Web应用运行在pywebview在窗口
  9. Android -- 自动挂断电话
  10. 编写可靠shell脚本的8个建议
  11. mysql去除重复的数据
  12. 计算机408考研 思维导图 知识整理
  13. Pr 水墨动画转场效果
  14. PyQt5项目:网速监控器
  15. 检验方法的验证、确认步骤及详细计算方法
  16. 理想电压源的内阻是0,理想电流源的内阻是无穷大
  17. 英特尔安腾服务器芯片,英特尔开始出货新安腾服务器处理器
  18. dnf剑魂buff等级上限_DNF:国服鬼剑85版本以来的变迁,剑魂最惨,阿修罗起伏不大...
  19. 华为p40手机是不是android,华为P40新手机配新操作系统,网友:再见了安卓
  20. 略胜知云?适合大学生的一款文献翻译神器,网页版工具

热门文章

  1. mac os x10.8下如何使用git与github
  2. Chrome跨域问题
  3. 【原创】最值得推荐wince应用和wince驱动入门书籍
  4. 广播多路访问链路上的OSPF
  5. UPS分类:直流UPS和交流UPS
  6. 铁拳nat映射_铁拳如何重塑我的数据可视化设计流程
  7. 个人项目api接口_5个免费有趣的API,可用于学习个人项目等
  8. Sort HDU5884(二分+多叉哈夫曼树)
  9. 笔记本安装win7和arch linux双系统+xfce4桌面
  10. 项目回顾1-图片上传-form表单还是base64-前端图片压缩