linux设备驱动归纳总结(三):7.异步通知fasync_linux设备驱动归纳总结(三):7.异步通知fasync-程序员宅基地

技术标签: linux内核基础  linux  内核  嵌入式  

 转自:

http://blog.chinaunix.net/uid-25014876-id-62725.html

linux 设备驱动归纳总结(三): 7. 异步通知 fasync


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

异步通知fasync是应用于系统调用signalsigaction函数,下面我会使用signal函数。简单的说,signal函数就是让一个信号与与一个函数对应,没当接收到这个信号就会调用相应的函数。

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


一、什么是异步通知


个人认为,异步通知类似于中断的机制,如下面的将要举例的程序,当设备可写时,设备驱动函数发送一个信号给内核,告知内核有数据可读,在条件不满足之前,并不会造成阻塞。而不像之前学的阻塞型IOpoll它们是调用函数进去检查,条件不满足时还会造成阻塞


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


二、应用层中启用异步通知机制


其实就三个步骤:

1signal(SIGIO, sig_handler);

调用signal函数,让指定的信号SIGIO与处理函数sig_handler对应。

2fcntl(fd, F_SET_OWNER, getpid());

指定一个进程作为文件的“属主(filp->owner)”,这样内核才知道信号要发给哪个进程。

3f_flags = fcntl(fd, F_GETFL);

fcntl(fd, F_SETFL, f_flags | FASYNC);

在设备文件中添加FASYNC标志,驱动中就会调用将要实现的test_fasync函数。

三个步骤执行后,一旦有信号产生,相应的进程就会收到。


来个应用程序:

/*3rd_char_7/1st/app/monitor.c*/

1 #include <stdio.h>

2 #include <sys/types.h>

3 #include <sys/stat.h>

4 #include <fcntl.h>

5 #include <sys/select.h>

6 #include <unistd.h>

7 #include <signal.h>

8

9 unsigned int flag;

10

11 void sig_handler(int sig)

12 {

13 printf("<app>%s\n", __FUNCTION__);

14 flag = 1;

15 }

16

17 int main(void)

18 {

19 char buf[20];

20 int fd;

21 int f_flags;

22 flag = 0;

23

24 fd = open("/dev/test", O_RDWR);

25 if(fd < 0)

26 {

27 perror("open");

28 return -1;

29 }

30 /*三个步骤*/

31 signal(SIGIO, sig_handler);

32 fcntl(fd, F_SETOWN, getpid());

33 f_flags = fcntl(fd, F_GETFL);

34 fcntl(fd, F_SETFL, FASYNC | f_flags);

35

36 while(1)

37 {

38 printf("waiting \n"); //在还没收到信号前,程序还在不停的打印

39 sleep(4);

40 if(flag)

41 break;

42 }

43

44 read(fd, buf, 10);

45 printf("finish: read[%s]\n", buf);

46

47 close(fd);

48 return 0;

49 }


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


三、驱动中需要实现的异步通知


上面说的三个步骤,内核已经帮忙实现了前两个步骤,只需要我们稍稍实现第三个步骤的一个简单的传参。


实现异步通知,内核需要知道几个东西:哪个文件(filp),什么信号(SIGIIO),发给哪个进程(pid),收到信号后做什么(sig_handler)。这些都由前两个步骤完成了。

回想一下,在实现等待队列中,我们需要将一个等待队列wait_queue_t添加到指定的等待队列头wait_queue_head_t中。

在这里,同样需要把一个结构体struct fasync_struct添加到内核的异步队列头(名字是我自己取的)中。这个结构体用来存放对应设备文件的信息(fd, filp)并交给内核来管理。一但收到信号,内核就会在这个所谓的异步队列头找到相应的文件(fd),并在filp->owner中找到对应的进程PID,并且调用对应的sig_handler了。

看一下fasync_struct

1097 struct fasync_struct {

1098 int magic;

1099int fa_fd;

1100struct fasync_struct *fa_next; /* singly linked list *///一看就觉得他是链表

1101struct file*fa_file;

1102 };


上面红色标记说所的步骤都是由内核来完成,我们只要做两件事情:

1)定义结构体fasync_struct

struct fasync_struct *async_queue;

2)实现test_fasync,把函数fasync_helperfd,filp和定义的结构体传给内核。

108 int test_fasync (int fd, struct file *filp, int mode)

109 {

110 struct _test_t *dev = filp->private_data;

111

112 return fasync_helper(fd, filp, mode, &dev->async_queue);

113 }


讲一下函数fasync_helper:

int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)

一看就知道,前面的三个参数其实就是teat_fasync的三个参数,只要我们定义号的fasync_struct结构体也传进去就可以了。内核会完成我上面红色自己所说的事情。


另外还有两件事

3)当设备可写时,调用函数kill_fasync发送信号SIGIO给内核。

83 if (dev->async_queue){

84 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);

85 }

讲解一下这个函数:

void kill_fasync(struct fasync_struct **fp, int sig, int band)

sig就是我们要发送的信号。

band(带宽),一般都是使用POLL_IN,表示设备可读,如果设备可写,使用POLL_OUT


4)当设备关闭时,需要将fasync_struct从异步队列中删除:

117 test_fasync(-1, filp, 0);

删除也是调用test_fasync,不过改了一下参数而已。


既然说完了就上程序:上面的函数需要包含<linux/fs.h>

/*3rd_char_7/1st/test.c*/

23 struct _test_t{

24 char kbuf[DEV_SIZE];

25 unsigned int major;

26 unsigned int minor;

27 unsigned int cur_size;

28 dev_t devno;

29 struct cdev test_cdev;

30 wait_queue_head_t test_queue;

31 wait_queue_head_t read_queue;

32 wait_queue_head_t write_queue;

33 struct fasync_struct *async_queue; //1.定义结构体

34 };

。。。。省略。。。。

68 ssize_t test_write(struct file *filp, const char __user *buf, size_t count, loff_t *offset)

69 {

70 int ret;

71 struct _test_t *dev = filp->private_data;

72

73 if(copy_from_user(dev->kbuf, buf, count)){

74 ret = - EFAULT;

75 }else{

76 ret = count;

77 dev->cur_size += count;

78 P_DEBUG("write %d bytes, cur_size:[%d]\n", count, dev->cur_size);

79 P_DEBUG("kbuf is [%s]\n", dev->kbuf);

80 wake_up_interruptible(&dev->test_queue);

81 wake_up_interruptible(&dev->read_queue);

82

83 if (dev->async_queue){

84 kill_fasync(&dev->async_queue, SIGIO, POLL_IN); //3.可写时发送信号

85 }

86 }

87

88 return ret; //返回实际写入的字节数或错误号

89 }

。。。。省略。。。。

108 int test_fasync (int fd, struct file *filp, int mode) //2.实现test_fasync

109 {

110 struct _test_t *dev = filp->private_data;

111

112 return fasync_helper(fd, filp, mode, &dev->async_queue);

113 }

114

115 int test_close(struct inode *node, struct file *filp)

116 {

117 test_fasync(-1, filp, 0); //4文件关闭时将结构体从伊部队列中删除

118 return 0;

119 }

120

121 struct file_operations test_fops = {

122 .open = test_open,

123 .release = test_close,

124 .write = test_write,

125 .read = test_read,

126 .poll = test_poll,

127 .fasync = test_fasync, //此步骤切记

128 };

.。。。。。。


程序写完了就得验证一下:

[root: app]# insmod ../test.ko

major[253] minor[0]

hello kernel

[root: app]# mknod /dev/test c 253 0

[root: app]# ./monitor& //后台运行monitor

waiting

[root: app]# waiting //不停的打印,没有休眠

waiting

waiting

waiting

waiting

waiting

[root: app]# ./app_write //调用函数写数据,

<kernel>[test_write]write 10 bytes, cur_size:[10]

<kernel>[test_write]kbuf is [xiao bai]

<app>s<kernel>[test_read]read data..... //写完后minoter接收到信号,跳出循环读数据

<kernel>[test_read]read 10 bytes, cur_size:[0]

ig_handler //这是在sig_hanler里面打印的,本应出现在读函数之前,因为各个函数抢着打印,所以,出现了乱序,不过不影响验证。

finish: read[xiao bai]

[1] + Done ./monitor


贴张图总结一下:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


四、阻塞型IOpoll和异步通知的区别:


宋宝华书上的图,描述的挺好的:图片不态清晰,将就一下。

一个最重要的区别:

1)异步通知是不会造成阻塞的。

2)调用阻塞IO时如果条件不满足,会在驱动函数中的test_readtest_write中阻塞。

3)如果条件不满足,selcet会在系统调用中阻塞。


所谓的异步,就是进程可以在信号没到前干别的事情,等到信号到来了,进程就会被内核通知去做相应的信号操作。进程是不知道信号什么时候来的。


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


五,总结


今天只是讲了异步通知在内核中的实现,并且对应的应用函数和驱动函数需要做什么事情。最后总结了一下阻塞IOpoll和异步通知的区别。


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


源代码: 3rd_char_7.rar

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lijiatong1005/article/details/9316211

智能推荐

class和struct的区别-程序员宅基地

文章浏览阅读101次。4.class可以有⽆参的构造函数,struct不可以,必须是有参的构造函数,⽽且在有参的构造函数必须初始。2.Struct适⽤于作为经常使⽤的⼀些数据组合成的新类型,表示诸如点、矩形等主要⽤来存储数据的轻量。1.Class⽐较适合⼤的和复杂的数据,表现抽象和多级别的对象层次时。2.class允许继承、被继承,struct不允许,只能继承接⼝。3.Struct有性能优势,Class有⾯向对象的扩展优势。3.class可以初始化变量,struct不可以。1.class是引⽤类型,struct是值类型。

android使用json后闪退,应用闪退问题:从json信息的解析开始就会闪退-程序员宅基地

文章浏览阅读586次。想实现的功能是点击顶部按钮之后按关键字进行搜索,已经可以从服务器收到反馈的json信息,但从json信息的解析开始就会闪退,加载listview也不知道行不行public abstract class loadlistview{public ListView plv;public String js;public int listlength;public int listvisit;public..._rton转json为什么会闪退

如何使用wordnet词典,得到英文句子的同义句_get_synonyms wordnet-程序员宅基地

文章浏览阅读219次。如何使用wordnet词典,得到英文句子的同义句_get_synonyms wordnet

系统项目报表导出功能开发_积木报表 多线程-程序员宅基地

文章浏览阅读521次。系统项目报表导出 导出任务队列表 + 定时扫描 + 多线程_积木报表 多线程

ajax 如何从服务器上获取数据?_ajax 获取http数据-程序员宅基地

文章浏览阅读1.1k次,点赞9次,收藏9次。使用AJAX技术的好处之一是它能够提供更好的用户体验,因为它允许在不重新加载整个页面的情况下更新网页的某一部分。另外,AJAX还使得开发人员能够创建更复杂、更动态的Web应用程序,因为它们可以在后台与服务器进行通信,而不需要打断用户的浏览体验。在Web开发中,AJAX(Asynchronous JavaScript and XML)是一种常用的技术,用于在不重新加载整个页面的情况下,从服务器获取数据并更新网页的某一部分。使用AJAX,你可以创建异步请求,从而提供更快的响应和更好的用户体验。_ajax 获取http数据

Linux图形终端与字符终端-程序员宅基地

文章浏览阅读2.8k次。登录退出、修改密码、关机重启_字符终端

随便推点

Python与Arduino绘制超声波雷达扫描_超声波扫描建模 python库-程序员宅基地

文章浏览阅读3.8k次,点赞3次,收藏51次。前段时间看到一位发烧友制作的超声波雷达扫描神器,用到了Arduino和Processing,可惜啊,我不会Processing更看不懂人家的程序,咋办呢?嘿嘿,所以我就换了个思路解决,因为我会一点Python啊,那就动手吧!在做这个案例之前先要搞明白一个问题:怎么将Arduino通过超声波检测到的距离反馈到Python端?这个嘛,我首先想到了串行通信接口。没错!就是串口。只要Arduino将数据发送给COM口,然后Python能从COM口读取到这个数据就可以啦!我先写了一个测试程序试了一下,OK!搞定_超声波扫描建模 python库

凯撒加密方法介绍及实例说明-程序员宅基地

文章浏览阅读4.2k次。端—端加密指信息由发送端自动加密,并且由TCP/IP进行数据包封装,然后作为不可阅读和不可识别的数据穿过互联网,当这些信息到达目的地,将被自动重组、解密,而成为可读的数据。不可逆加密算法的特征是加密过程中不需要使用密钥,输入明文后由系统直接经过加密算法处理成密文,这种加密后的数据是无法被解密的,只有重新输入明文,并再次经过同样不可逆的加密算法处理,得到相同的加密密文并被系统重新识别后,才能真正解密。2.使用时,加密者查找明文字母表中需要加密的消息中的每一个字母所在位置,并且写下密文字母表中对应的字母。_凯撒加密

工控协议--cip--协议解析基本记录_cip协议embedded_service_error-程序员宅基地

文章浏览阅读5.7k次。CIP报文解析常用到的几个字段:普通类型服务类型:[0x00], CIP对象:[0x02 Message Router], ioi segments:[XX]PCCC(带cmd和func)服务类型:[0x00], CIP对象:[0x02 Message Router], cmd:[0x101], fnc:[0x101]..._cip协议embedded_service_error

如何在vs2019及以后版本(如vs2022)上添加 添加ActiveX控件中的MFC类_vs添加mfc库-程序员宅基地

文章浏览阅读2.4k次,点赞9次,收藏13次。有时候我们在MFC项目开发过程中,需要用到一些微软已经提供的功能,如VC++使用EXCEL功能,这时候我们就能直接通过VS2019到如EXCEL.EXE方式,生成对应的OLE头文件,然后直接使用功能,那么,我们上篇文章中介绍了vs2017及以前的版本如何来添加。但由于微软某些方面考虑,这种方式已被放弃。从上图中可以看出,这一功能,在从vs2017版本15.9开始,后续版本已经删除了此功能。那么我们如果仍需要此功能,我们如何在新版本中添加呢。_vs添加mfc库

frame_size (1536) was not respected for a non-last frame_frame_size (1024) was not respected for a non-last-程序员宅基地

文章浏览阅读785次。用ac3编码,执行编码函数时报错入如下:[ac3 @ 0x7fed7800f200] frame_size (1536) was not respected for anon-last frame (avcodec_encode_audio2)用ac3编码时每次送入编码器的音频采样数应该是1536个采样,不然就会报上述错误。这个数字并非刻意固定,而是跟ac3内部的编码算法原理相关。全网找不到,国内音视频之路还有很长的路,音视频人一起加油吧~......_frame_size (1024) was not respected for a non-last frame

Android移动应用开发入门_在安卓移动应用开发中要在活动类文件中声迷你一个复选框变量-程序员宅基地

文章浏览阅读230次,点赞2次,收藏2次。创建Android应用程序一个项目里面可以有很多模块,而每一个模块就对应了一个应用程序。项目结构介绍_在安卓移动应用开发中要在活动类文件中声迷你一个复选框变量

推荐文章

热门文章

相关标签