linux中的kill_async与fasync_helper函数使用

fasync是为了使驱动的读写和应用程序的读写分开,使得应用程序可以在驱动读写的时候去做别的事。

应用程序通过fcntl给自己的SIGIO信号安装自己的响应函数,

驱动通过kill_fasync(&async, SIGIO, POLL_IN); 发SIGIO信号给应用程序,应用程序就调用自己安装的响应函数去处理。

fasync_helper作用就是初始化fasync,包括分配内存和设置属性,最后在驱动的release里把fasync_helper初始化的东西free掉。



例子:

驱动程序:

[cpp] view plain copy
  1. /******************************************* 
  2. 使用linux3.2.81内核 
  3. ********************************************/  
  4. #include <linux/module.h>  
  5. #include <linux/kernel.h>  
  6. #include <linux/fs.h>  
  7. #include <linux/init.h>  
  8. #include <linux/delay.h>  
  9. #include <linux/device.h>  
  10. #include <linux/interrupt.h>  
  11. #include <linux/sched.h>  
  12. #include <linux/irq.h>  
  13. #include <asm/uaccess.h>  
  14. #include <asm/irq.h>  
  15. #include <asm/io.h>  
  16. #include <mach/regs-gpio.h>  
  17. #include <mach/hardware.h>  
  18. #include <linux/poll.h>  
  19.   
  20. static struct class *buttonsdrv_class;              //类结构体  
  21. static struct device    *buttonsdrv_class_dev;  //设备结构体  
  22.   
  23. volatile unsigned long *gpfcon = NULL ;  
  24. volatile unsigned long *gpfdat = NULL;  
  25.   
  26. volatile unsigned long *gpgcon = NULL ;  
  27. volatile unsigned long *gpgdat = NULL;  
  28.   
  29. unsigned int press_cnt[4];   //记录该按键按下的次数  
  30.   
  31. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);    //声明(定义)等待队列  
  32.   
  33. /* 中断事件标志, 中断服务程序将它置1,buttons_drv_read将它清0 */  
  34. static volatile int ev_press = 0;  
  35.   
  36. static struct fasync_struct *button_async;  
  37.   
  38. //中断处理函数  
  39. static irqreturn_t buttons_irq(int irq, void *dev_id)  
  40. {  
  41.         volatile int *press_cnt = (volatile int *)dev_id;  
  42.           
  43.         *press_cnt = *press_cnt +1;  //按键计数值加一  
  44.   
  45.     ev_press = 1;                  /* 表示中断发生了 */  
  46.     wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */  
  47.         //发送信号SIGIO信号给fasync_struct 结构体所描述的PID,触发应用程序的SIGIO信号处理函数   
  48.         kill_fasync (&button_async, SIGIO, POLL_IN);  
  49.     return IRQ_RETVAL(IRQ_HANDLED);  
  50. }  
  51.   
  52. static int buttons_drv_open(struct inode *inode, struct file *file)  
  53. {  
  54.     /* 
  55.     参数解析:中断号,中断处理函数,中断处理标志(如:双边沿触发),中断名称(任意取), 
  56.                         dev_id(中断处理函数)在中断共享时会用到,一般设置为这个设备的设备结构体或者NULL 
  57.     */  
  58.     request_irq(IRQ_EINT0,buttons_irq, IRQ_TYPE_EDGE_BOTH, "S2",&press_cnt[0]);     /* 配置GPF0为输入引脚 ,注册中断处理函数*/  
  59.     request_irq(IRQ_EINT2,buttons_irq, IRQ_TYPE_EDGE_BOTH, "S3",&press_cnt[1]);  
  60.     request_irq(IRQ_EINT11,buttons_irq,IRQ_TYPE_EDGE_BOTH, "S4",&press_cnt[2]);  
  61.     request_irq(IRQ_EINT19,buttons_irq,IRQ_TYPE_EDGE_BOTH, "S5",&press_cnt[3]);  
  62.       
  63.     return 0;  
  64. }  
  65.   
  66.   
  67. static int buttons_drv_close(struct inode *inode, struct file *file)  
  68. {  
  69.     //释放已经注册的中断  
  70.     free_irq(IRQ_EINT0,  &press_cnt[0]);  
  71.     free_irq(IRQ_EINT2,  &press_cnt[1]);  
  72.     free_irq(IRQ_EINT11, &press_cnt[2]);  
  73.     free_irq(IRQ_EINT19, &press_cnt[3]);  
  74.     return 0;  
  75. }  
  76.   
  77.   
  78. static int buttons_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)  
  79. {  
  80.   
  81.     /* 如果没有按键动作, 休眠     wait_event_interruptible的第二个参数是一个判断条件,如果条件成立则退出休眠,否则进入休眠*/  
  82.     wait_event_interruptible(button_waitq, ev_press); //按键按下中断处理函数中会将ev_press=1  
  83.   
  84.     /* 如果有按键动作, 返回键值 */  
  85.     copy_to_user(buf, press_cnt, min(sizeof(press_cnt),size));  //根据实际需要读出对应个数的值到用户空间  
  86.     ev_press = 0;   //读取完后清0  
  87.       
  88.     return 1;  
  89. }  
  90.   
  91. static int buttons_drv_fasync (int fd, struct file *filp, int on)  
  92. {  
  93.     printk("driver: buttons_drv_fasync\n");  
  94.     //初始化fasync_struct 结构体 (fasync_struct->fa_file->f_owner->pid)   
  95.     return fasync_helper (fd, filp, on, &button_async);  
  96. }  
  97.   
  98. /* 
  99. 当应用程序操作设备文件时所调用的open、read、write等函数,最终会调用这个结构体中上的对应函数 
  100. */  
  101. static struct file_operations buttons_drv_fops = {  
  102.     .owner   =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */  
  103.     .open    =   buttons_drv_open,   
  104.     .release =   buttons_drv_close,    
  105.         .read      =     buttons_drv_read,     
  106.         .fasync  =   buttons_drv_fasync,    
  107. };  
  108.   
  109.   
  110. int major;  
  111. //指定insmod命令时会调用这个函数  
  112. static int buttons_drv_init(void)  
  113. {  
  114.     //第一个参数如果等于0,则表示采用系统动态分配的主设备号;不为0,则表示静态注册  
  115.     major = register_chrdev(0, "buttons_drv", &buttons_drv_fops); // 注册, 告诉内核,返回动态创建的主设备号  
  116.       
  117.     //以下两条语句是为了实现自动创建设备  
  118.     buttonsdrv_class = class_create(THIS_MODULE, "buttonsdrv");  //class_create为该设备创建一个class  
  119.     buttonsdrv_class_dev = device_create(buttonsdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */ //class_device_create创建对应的设备  
  120.       
  121.     //驱动中要使用虚拟地址,不能直接使用物理地址  
  122.     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);  //gpfcon映射为0x56000050,映射长度为16个字节(gpfcon为虚地址,0x56000050为物理地址)  
  123.     gpfdat = gpfcon + 1;  //gpfcon + 1即为0x56000050+4  
  124.   
  125.     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);  //gpgcon映射为0x56000060,映射长度为16个字节(gpgcon为虚地址,0x560000650为物理地址)  
  126.     gpgdat = gpgcon + 1;  //gpgcon + 1即为0x56000060+4  
  127.       
  128.     return 0;  
  129. }  
  130.   
  131. //执行rmmod时会调用这个函数  
  132. static void buttons_drv_exit(void)  
  133. {  
  134.     unregister_chrdev(major, "buttons_drv");                // 卸载  
  135.   
  136.     device_unregister(buttonsdrv_class_dev);  //将自动创建的设备注销  
  137.     class_destroy(buttonsdrv_class);                    //删除创建的类  
  138.     iounmap(gpfcon);                                        //取消gpfcon的映射  
  139.     iounmap(gpgcon);                                        //取消gpgcon的映射  
  140. }  
  141.   
  142. //指定驱动程序的初始化函数和卸载函数  
  143. module_init(buttons_drv_init);  
  144. module_exit(buttons_drv_exit);  
  145.   
  146.   
  147. MODULE_LICENSE("GPL");  //防止出现“module license”unspecified taints kernel的警告  

应用程序:

[cpp] view plain copy
  1. /* 
  2. 字符设备测试程序 
  3. */  
  4.   
  5. #include <sys/types.h>  
  6. #include <sys/stat.h>  
  7. #include <fcntl.h>  
  8. #include <stdio.h>  
  9. #include <signal.h>  
  10.   
  11. int fd;  
  12.   
  13. //信号处理函数  
  14. void my_signal_fun(int signum)  
  15. {  
  16.     int i;  
  17.     int press_cnt[4];  
  18.       
  19.     read(fd, press_cnt, sizeof(press_cnt)); //读取值到press_cnt缓存区,读取长度sizeof(press_cnt)  
  20.     for(i = 0 ; i<sizeof(press_cnt)/sizeof(press_cnt[0]);i++)  
  21.     {  
  22.         if(press_cnt[i]) //如果按下次数不为0,打印出来  
  23.             printf("K%d has been pressed %d times \n", i+1, press_cnt[i]);  
  24.     }  
  25. }  
  26.   
  27. int main(int argc , char ** argv)  
  28. {  
  29.   
  30.   
  31.     int Oflags;  
  32.       
  33.     //在应用程序中捕捉SIGIO信号(由驱动程序发送)    
  34.     signal(SIGIO, my_signal_fun);  
  35.       
  36.     fd = open("/dev/buttons", O_RDWR);  //打开设备  
  37.     if(fd<0)  
  38.     {  
  39.         printf("Can't open");  
  40.         return -1;  
  41.     }  
  42.       
  43.     //将当前进程PID设置为fd文件所对应驱动程序将要发送SIGIO,SIGUSR信号进程PID    
  44.     fcntl(fd, F_SETOWN, getpid());  
  45.     //获取fd的打开方式  
  46.     Oflags = fcntl(fd, F_GETFL); //先获取当前的状态,然后或运算就不会导致当前状态丢失  
  47.     //将fd的打开方式设置为FASYNC --- 即 支持异步通知    
  48.  //该行代码执行会触发 驱动程序中 file_operations->fasync 函数 ------fasync函数调用fasync_helper初始化一个fasync_struct结构体,该结构体描述了将要发送信号的进程PID (fasync_struct->fa_file->f_owner->pid)    
  49.     fcntl(fd, F_SETFL, Oflags | FASYNC);  
  50.       
  51.       
  52.     while (1)  
  53.     {  
  54.         sleep(5);  //挂起进程5秒,信号会提前打断休眠  
  55.         printf("wait!\n"); //5秒后输出wait! ,一定要加上“\n”,否则无法刷新缓存区,会一直没有输出  
  56.     }  
  57. }  
发布了141 篇原创文章 · 获赞 108 · 访问量 27万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章