Linux驅動之同步、互斥、阻塞的應用
同步、互斥、阻塞的概念:
同步:在並發程序設計中,各進程對公共變量的訪問必須加以制約,這種制約稱為同步。
互斥機制:訪問共享資源的代碼區叫做臨界區,這裏的共享資源可能被多個線程需要,但這些共享資源又不能被同時訪問,因此臨界區需要以某種互斥機制加以保護,以確保共享資源被互斥訪問。
阻塞與非阻塞:阻塞調用是指調用結果返回之前,當前線程會被掛起,調用線程只有在得到結果之後才會返回。非阻塞調用指在不能立刻得到結果之前,該調用不會阻塞當前線程,而是直接返回。
在按鍵驅動的例子中,如果有多個應用程序調用按鍵驅動的設備文件,這時候就要利用同步與互斥的概念對這個種情況進行處理:
1、利用原子變量標誌來判斷設備文件是否被打開,原子變量在操作的時候不能被打斷,它是利用關閉中斷的方式實現的,一旦關閉了中斷,內核將不能對進程進行調度,這就保證了原子性。
直接修改驅動代碼,先定義一個原子變量
static atomic_t open_flag = ATOMIC_INIT(1); //定義原子變量open_flag 並初始化為1
接著修改打開文件的函數與關閉文件的函數,初始化時open_flag 為1,一旦打開函數被調用則會減1變為0。關閉函數被調用後會加1又變成1。
a、在sixth_drv_open 中利用atomic_dec_and_test函數判斷是否已經被調用,如果返回值為0,說明已經被調用。調用atomic_inc函數,並且返回。
b、在sixth_drv_close中第調用atomic_inc。
static int sixth_drv_open (structinode * inode, struct file * file) { int ret; if(atomic_dec_and_test(&open_flag)==0)//自檢後是否為0,不為0說明已經被人調用 { atomic_inc(&open_flag);//原子變量+1 return -EBUSY; } ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]); if(ret) { printk("open failed 1\n"); return -1; } ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]); if(ret) { printk("open failed 2\n"); return -1; } ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]); if(ret) { printk("open failed 3\n"); return -1; } ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]); if(ret) { printk("open failed 4\n"); return -1; } return 0; } static int sixth_drv_close(struct inode * inode, struct file * file) { atomic_inc(&open_flag);//原子變量+1 free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]); free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]); free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]); free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]); return 0; }
2、利用信號量對打開的文件進行保護:信號量(semaphore)是用於保護臨界區的一種常用方法,只有得到信號量的進程才能執行臨界區代碼。當獲取不到信號量時,進程進入休眠等待狀態。 直接修改驅動代碼,先定義一個互斥鎖
static DECLARE_MUTEX(button_lock); //定義互斥鎖
接著更改按鍵驅動中打開文件的函數與關閉文件的函數:
a、在sixth_drv_open函數中如果文件打開方式非阻塞的,那麽調用down_trylock函數獲取信號量,此函數如果獲取不到信號量,直接返回;如果打開文件的方式是阻塞的,那麽調用down函數,如果獲取不到信號量,則將進程休眠直到獲取信號量為止。
b、在sixth_drv_close函數利用up函數直接釋放掉信號量。
static int sixth_drv_open (struct inode * inode, struct file * file) { int ret; if(file->f_flags & O_NONBLOCK)//非阻塞方式 { if(down_trylock(&button_lock))//獲取信號量失敗則返回 return -EBUSY; } else down(&button_lock);//獲得信號量 ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]); if(ret) { printk("open failed 1\n"); return -1; } ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]); if(ret) { printk("open failed 2\n"); return -1; } ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]); if(ret) { printk("open failed 3\n"); return -1; } ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]); if(ret) { printk("open failed 4\n"); return -1; } return 0; } static int sixth_drv_close(struct inode * inode, struct file * file) { up(&button_lock);//釋放信號量 free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]); free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]); free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]); free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]); return 0; }
將完整的按鍵驅動的源代碼貼出
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <asm/io.h> //含有iomap函數iounmap函數 #include <asm/uaccess.h>//含有copy_from_user函數 #include <linux/device.h>//含有類相關的處理函數 #include <asm/arch/regs-gpio.h>//含有S3C2410_GPF0等相關的 #include <linux/irq.h> //含有IRQ_HANDLED\IRQ_TYPE_EDGE_RISING #include <asm-arm/irq.h> //含有IRQT_BOTHEDGE觸發類型 #include <linux/interrupt.h> //含有request_irq、free_irq函數 #include <linux/poll.h> #include <asm-generic/errno-base.h> //含有各種錯誤返回值 //#include <asm-arm\arch-s3c2410\irqs.h> static struct class *sixth_drv_class;//類 static struct class_device *sixth_drv_class_dev;//類下面的設備 static int sixthmajor; static unsigned long *gpfcon = NULL; static unsigned long *gpfdat = NULL; static unsigned long *gpgcon = NULL; static unsigned long *gpgdat = NULL; struct fasync_struct *sixth_fasync; static unsigned int key_val; struct pin_desc { unsigned int pin; unsigned int key_val; }; static struct pin_desc pins_desc[4] = { {S3C2410_GPF0,0x01}, {S3C2410_GPF2,0x02}, {S3C2410_GPG3,0x03}, {S3C2410_GPG11,0x04} }; static unsigned int ev_press; static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//註冊一個等待隊列button_waitq static atomic_t open_flag = ATOMIC_INIT(1); //定義原子變量open_flag 並初始化為1 static DECLARE_MUTEX(button_lock); //定義互斥鎖 /* *0x01、0x02、0x03、0x04表示按鍵被按下 */ /* *0x81、0x82、0x83、0x84表示按鍵被松開 */ /* *利用dev_id的值為pins_desc來判斷是哪一個按鍵被按下或松開 */ static irqreturn_t buttons_irq(int irq, void *dev_id) { unsigned int pin_val; struct pin_desc * pin_desc = (struct pin_desc *)dev_id;//取得哪個按鍵被按下的狀態 pin_val = s3c2410_gpio_getpin(pin_desc->pin); if(pin_val) //按鍵松開 key_val = 0x80 | pin_desc->key_val; else key_val = pin_desc->key_val; wake_up_interruptible(&button_waitq); /* 喚醒休眠的進程 */ ev_press = 1; kill_fasync(&sixth_fasync, SIGIO, POLL_IN);//發生信號給進程 return IRQ_HANDLED; } static int sixth_drv_open (struct inode * inode, struct file * file) { int ret; // if(atomic_dec_and_test(&open_flag)==0)//自檢後是否為0,不為0說明已經被人調用 // { // atomic_inc(&open_flag);//原子變量+1 // return -EBUSY; // } if(file->f_flags & O_NONBLOCK)//非阻塞方式 { if(down_trylock(&button_lock))//獲取信號量失敗則返回 return -EBUSY; } else down(&button_lock);//獲得信號量 ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]); if(ret) { printk("open failed 1\n"); return -1; } ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]); if(ret) { printk("open failed 2\n"); return -1; } ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]); if(ret) { printk("open failed 3\n"); return -1; } ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]); if(ret) { printk("open failed 4\n"); return -1; } return 0; } static int sixth_drv_close(struct inode * inode, struct file * file) { // atomic_inc(&open_flag);//原子變量+1 up(&button_lock);//釋放信號量 free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]); free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]); free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]); free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]); return 0; } static ssize_t sixth_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off) { int ret; if(count != 1) { printk("read error\n"); return -1; } if(file->f_flags & O_NONBLOCK)//非阻塞方式 { if(!ev_press)//判斷是否有按鍵按下,如果沒有直接返回 { key_val = 0; copy_to_user(userbuf, &key_val, 1); return -EBUSY; } } else//如果沒有按鍵動作,直接進入休眠 wait_event_interruptible(button_waitq, ev_press);//將當前進程放入等待隊列button_waitq中 ret = copy_to_user(userbuf, &key_val, 1); ev_press = 0;//按鍵已經處理可以繼續睡眠 if(ret) { printk("copy error\n"); return -1; } return 1; } static unsigned int sixth_drv_poll(struct file *file, poll_table *wait) { unsigned int ret = 0; poll_wait(file, &button_waitq, wait);//將當前進程放到button_waitq列表 if(ev_press) ret |=POLLIN;//說明有數據被取到了 return ret; } static int sixth_drv_fasync(int fd, struct file * file, int on) { int err; printk("fansync_helper\n"); err = fasync_helper(fd, file, on, &sixth_fasync);//初始化sixth_fasync if (err < 0) return err; return 0; } static struct file_operations sixth_drv_ops = { .owner = THIS_MODULE, .open = sixth_drv_open, .read = sixth_drv_read, .release = sixth_drv_close, .poll = sixth_drv_poll, .fasync = sixth_drv_fasync, }; static int sixth_drv_init(void) { sixthmajor = register_chrdev(0, "buttons", &sixth_drv_ops);//註冊驅動程序 if(sixthmajor < 0) printk("failes 1 buttons_drv register\n"); sixth_drv_class = class_create(THIS_MODULE, "buttons");//創建類 if(sixth_drv_class < 0) printk("failes 2 buttons_drv register\n"); sixth_drv_class_dev = class_device_create(sixth_drv_class, NULL, MKDEV(sixthmajor,0), NULL,"buttons");//創建設備節點 if(sixth_drv_class_dev < 0) printk("failes 3 buttons_drv register\n"); gpfcon = ioremap(0x56000050, 16);//重映射 gpfdat = gpfcon + 1; gpgcon = ioremap(0x56000060, 16);//重映射 gpgdat = gpgcon + 1; printk("register buttons_drv\n"); return 0; } static void sixth_drv_exit(void) { unregister_chrdev(sixthmajor,"buttons"); class_device_unregister(sixth_drv_class_dev); class_destroy(sixth_drv_class); iounmap(gpfcon); iounmap(gpgcon); printk("unregister buttons_drv\n"); } module_init(sixth_drv_init); module_exit(sixth_drv_exit); MODULE_LICENSE("GPL");
接著改寫測試程序,測試加入阻塞方式打開文件,在fd = open(filename, O_RDWR|O_NONBLOCK)函數中加入O_NONBLOCK即可以按阻塞方式打開。
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <poll.h> #include <signal.h> static int fd; //static void fifth_testsignal(int signum) //{ // unsigned char key_val; // // printf("signal = %d\n",signum); // read(fd, &key_val, 1); // printf("signumkey_val: 0x%x\n\n",key_val); //} /* *usage ./buttonstest */ int main(int argc, char **argv) { char* filename="dev/buttons"; int oflags,ret; unsigned char key_val; fd = open(filename, O_RDWR|O_NONBLOCK);//打開dev/firstdrv設備文件,非阻塞方式打開 if (fd < 0)//小於0說明沒有成功 { printf("error, can‘t open %s\n", filename); return 0; } if(argc !=1) { printf("Usage : %s ",argv[0]); return 0; } // signal(SIGIO, fifth_testsignal);//註冊一個信號,函數為fifth_testsignal // // fcntl(fd, F_SETOWN, getpid()); // 告訴內核,發給誰 // // oflags = fcntl(fd, F_GETFL); //取得當前的狀態 // // fcntl(fd, F_SETFL, oflags | FASYNC); // 改變fasync標記,最終會調用到驅動的faync > fasync_helper:初始化/釋放fasync_struct while(1) { ret = read(fd, &key_val, 1); printf("ret = %d,key_val: 0x%x\n",ret,key_val); sleep(5); } return 0; }將驅動程序與測試程序編譯後運行。發現以阻塞方式運行的測試程序如果再次運行會處於睡眠狀態;如果以非阻塞方式再次運行程序,會導致第二個程序退出。 以上只是記錄了怎麽調用內核函數來實現互斥、阻塞機制,具體原理還未分析,後面再分析。
Linux驅動之同步、互斥、阻塞的應用