9.按鍵之使用異步通知
之前學的應用層都是:
1)查詢方式:一直讀
2)中斷方式.同樣一直讀,直到中斷進程喚醒
3)poll機制:一直在poll函數中睡眠,一定時間讀一次
以上3種,我們都是讓應用程序主動去讀,本節我們學習異步通知,它的作用就是當驅動層有數據時,主動告訴應用程序,然後應用程序再來讀, 這樣,應用程序就可以幹其它的事情,不必一直讀
比如:kill -9 pid ,其實就是通過發信號殺死進程,kill發數據9給指定id號進程
1.怎麽來收信號?
通過signal函數來實現獲取信號,先來看看以下例子:
頭函數:
sighandler_t signal(int signum, sighandler_t handler);
函數說明:讓一個信號與與一個函數對應,每當接收到這個信號就會調用相應的函數。
頭文件: #include <signal.h>
參數1: 指明了所要處理的信號類型
信號有以下幾種:
- SIGINT 鍵盤中斷(如break、ctrl+c鍵被按下)
- SIGUSR1 用戶自定義信號1,kill的USR1(10)信號
- SIGUSR2 用戶自定義信號2, kill的USR2(12)信號
參數2: 信號產生後需要處理的方式,可以是個函數
代碼如下:
#include <stdio.h> #include <signal.h> void my_signal_run(intsignum) //信號處理函數 { static int run_cnt=0; printf("signal = %d, %d count\r\n",signum,++count); } int main(int argc,char **argv) { signal(SIGUSR1,my_signal_run); //調用signal函數,讓指定的信號SIGUSR1與處理函數my_signal_run對應。 while(1) {
sleep(1000); //去做其它事,睡眠1s } return 0; }
然後運行後,使用kill -10 802,可以看到產生單信號USR1(10)時就會調用my_signal_run()打印數據。
# kill -10 802
# signal = 10, 1 count
# kill -10 802
# signal = 10, 2 count
2. 來實現異步通知
要求:
- 一、應用程序要實現有:註冊信號處理函數,使用signal函數
- 二、誰來發?驅動來發
- 三、發給誰?驅動發給應用程序,但應用程序必須告訴驅動PID,
- 四、怎麽發?驅動程序調用kill_fasync函數
3先來寫驅動程序,我們在之前的中斷程序上修改
3.1定義 異步信號結構體 變量:
static struct fasync_struct * button_async;
3.2在file_operations結構體添加成員.fasync函數,並寫函數
static struct file_operations third_drv_fops={ .owner = THIS_MODULE, .open = fourth_drv_open, .read = fourth _drv_read, .release= fourth _drv_class, .poll = fourth _poll, .fasync = fourth_fasync //添加初始化異步信號函數 }; static int fourth_fasync (int fd, struct file *file, int on) { return fasync_helper(fd, file, on, & button_async); //初始化button_async結構體,就能使用kill_fasync()了 }
成員.fasync函數又是什麽情況下使用?
是被應用程序調用,在下面第4小節會見到。
3.3在buttons_irq中斷服務函數裏發送信號:
kill_fasync(&button_async, SIGIO, POLL_IN); //當有中斷時,就發送SIGIO信號給應用層,應用層就會觸發與SIGIO信號對應的函數
3.4 驅動程序代碼如下:
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/irq.h> #include <asm/irq.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h> #include <asm/uaccess.h> #include <asm/io.h> #include <linux/poll.h> static struct class *fourthdrv_class; static struct class_device *fourthdrv_class_devs; /* 聲明等待隊列類型中斷 button_wait */ static DECLARE_WAIT_QUEUE_HEAD(button_wait); /* 異步信號結構體變量 */ static struct fasync_struct * button_async; /* * 定義中斷事件標誌 * 0:進入等待隊列 1:退出等待隊列 */ static int even_press=0; /* * 定義全局變量key_val,保存key狀態 */ static int key_val=0; /* *引腳描述結構體 */ struct pin_desc{ unsigned int pin; unsigned int pin_status; }; /* *key初始狀態(沒有按下): 0x01,0x02,0x03,0x04 *key狀態(按下): 0x81,0x82,0x83,0x84 */ struct pin_desc pins_desc[4]={ {S3C2410_GPF0,0x01 }, {S3C2410_GPF2, 0x02 }, {S3C2410_GPG3, 0x03 }, {S3C2410_GPG11,0x04}, } ; int fourth_drv_class(struct inode *inode, struct file *file) //卸載中斷 { free_irq(IRQ_EINT0,&pins_desc[0]); free_irq(IRQ_EINT2,&pins_desc[1]); free_irq(IRQ_EINT11,&pins_desc[2]); free_irq(IRQ_EINT19,&pins_desc[3]); return 0; } /* * 確定是上升沿還是下降沿 */ static irqreturn_t buttons_irq (int irq, void *dev_id) //中斷服務函數 { struct pin_desc *pindesc=(struct pin_desc *)dev_id; //獲取引腳描述結構體 unsigned int pin_val=0; pin_val=s3c2410_gpio_getpin(pindesc->pin); if(pin_val) { /*沒有按下 (下降沿),清除0x80*/ key_val=pindesc->pin_status&0xef; } else { /*按下(上升沿),加上0x80*/ key_val=pindesc->pin_status|0x80; } even_press=1; //退出等待隊列 wake_up_interruptible(&button_wait); //喚醒 中斷 kill_fasync(&button_async, SIGIO, POLL_IN); //發送SIGIO信號給應用層 return IRQ_RETVAL(IRQ_HANDLED); } static int fourth_drv_open(struct inode *inode, struct file *file) { request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S1",&pins_desc[0]); request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE, "S2", &pins_desc[1]); request_irq(IRQ_EINT11, buttons_irq,IRQT_BOTHEDGE, "S3", &pins_desc[2]); request_irq(IRQ_EINT19, buttons_irq,IRQT_BOTHEDGE, "S4", &pins_desc[3]); return 0; } static int fourth_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos) { /*將中斷 進入等待隊列(休眠狀態)*/ wait_event_interruptible(button_wait, even_press); /*有按鍵按下,退出等待隊列,上傳key_val 給用戶層*/ if(copy_to_user(buf,&key_val,sizeof(key_val))) return EFAULT; even_press=0; return 0; } static unsigned fourth_poll(struct file *file, poll_table *wait) { unsigned int mask = 0; poll_wait(file, &button_wait, wait); // 不會立即休眠 if (even_press) mask |= POLLIN | POLLRDNORM; return mask; } static int fourth_fasync (int fd, struct file *file, int on) { return fasync_helper(fd, file, on, & button_async); //初始化button_async結構體,就能使用kill_fasync()了 } static struct file_operations fourth_drv_fops={ .owner = THIS_MODULE, .open = fourth_drv_open, .read = fourth_drv_read, .release=fourth_drv_class, //裏面添加free_irq函數,來釋放中斷服務函數 .poll = fourth_poll, .fasync= fourth_fasync, //初始化異步信號函數 }; volatile int fourth_major; static int fourth_drv_init(void) { fourth_major=register_chrdev(0,"fourth_drv",&fourth_drv_fops); //創建驅動 fourthdrv_class=class_create(THIS_MODULE,"fourth_dev"); //創建類名 fourthdrv_class_devs=class_device_create(fourthdrv_class, NULL, MKDEV(fourth_major,0), NULL,"buttons"); return 0; } static int fourth_drv_exit(void) { unregister_chrdev(fourth_major,"fourth_drv"); //卸載驅動 class_device_unregister(fourthdrv_class_devs); //卸載類設備 class_destroy(fourthdrv_class); //卸載類 return 0; } module_init(fourth_drv_init); module_exit(fourth_drv_exit); MODULE_LICENSE("GPL v2");
4 寫應用測試程序
步驟如下:
1) signal(SIGIO, my_signal_fun);
調用signal函數,當接收到SIGIO信號就進入my_signal_fun函數,讀取驅動層的數據
2) fcntl(fd,F_SETOWN,getpid());
指定進程做為fd文件的”屬主”,內核收到F_SETOWN命令,就會設置pid(驅動無需處理),這樣fd驅動程序就知道發給哪個進程
3) oflags=fcntl(fd,F_GETFL);
獲取fd的文件狀態標誌
4) fcntl(fd,F_SETFL, oflags| FASYNC );
添加FASYNC狀態標誌,會調用驅動中成員.fasync函數,執行fasync_helper()來初始化異步信號結構體
這4個步驟執行後,一旦有驅動層有SIGIO信號時,進程就會收到
應用層代碼如下:
#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <string.h> #include <poll.h> #include <signal.h> #include <unistd.h> #include <fcntl.h> int fd,ret; void my_signal_fun(int signame) //有信號來了 { read( fd, &ret, 1); //讀取驅動層數據 printf("key_vale=0X%x\r\n",ret); } /*useg: fourthtext */ int main(int argc,char **argv) { int oflag; unsigned int val=0; fd=open("/dev/buttons",O_RDWR); if(fd<0) {printf("can‘t open!!!\n"); return -1;} signal(SIGIO,my_signal_fun); //指定的信號SIGIO與處理函數my_signal_run對應 fcntl( fd, F_SETOWN, getip()); //指定進程作為fd 的屬主,發送pid給驅動 oflag=fcntl( fd, F_GETFL); //獲取fd的文件標誌狀態 fcntl( fd, F_SETFL, oflag|FASYNC); //添加FASYNC狀態標誌,調用驅動層.fasync成員函數 while(1) { sleep(1000); //做其它的事情 } return 0; }
5 運行查看結果
下節開始學習——同步、互斥、阻塞
9.按鍵之使用異步通知