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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章