(十)Linux之等待队列
(一)阻塞和非阻塞
阻塞:執(zhí)行設(shè)備操作時(shí),若不能獲得資源,則掛起進(jìn)程進(jìn)入休眠直到滿足可操作的條件后再操作。
非阻塞:進(jìn)程在不能進(jìn)行設(shè)備操作時(shí),并不掛起,它要么放棄,要么不停地查詢,直至可以進(jìn)行操作為止。
(二)為什么學(xué)習(xí)等待隊(duì)列
在講解等待隊(duì)列的作用之前先來看一下內(nèi)核的休眠機(jī)制:
正在運(yùn)行的進(jìn)程讓出CPU,休眠的進(jìn)程會(huì)被內(nèi)核擱置在一邊,只有當(dāng)內(nèi)核再次把休眠的進(jìn)程喚醒, 進(jìn)程才會(huì)重新在CPU運(yùn)行,這是內(nèi)核中的進(jìn)程調(diào)度 一個(gè)CPU在同一時(shí)間只能有一個(gè)進(jìn)程在運(yùn)行,內(nèi)核將所有的進(jìn)程按一定的算法將CPU輪流的給每個(gè) 進(jìn)程使用,而休眠就是進(jìn)程沒有被運(yùn)行時(shí)的一種形式。在休眠下,進(jìn)程不占用CPU,等待被喚醒。當(dāng)一個(gè)進(jìn)程休眠時(shí),其他進(jìn)程為了能夠喚醒休眠的進(jìn)程,它必須知道休眠的進(jìn)程在哪里,出于這樣的原因,需要有一個(gè)稱為等待隊(duì)列的結(jié)構(gòu)體。等待隊(duì)列是一個(gè)存放著等待某個(gè)特定事件進(jìn)程鏈表。所以等待隊(duì)列相當(dāng)于休眠進(jìn)程的鏈表,需要手動(dòng)進(jìn)行操作
它的作用主要是:實(shí)現(xiàn)中斷處理、進(jìn)程同步及延時(shí)進(jìn)程
(三)等待隊(duì)列的相關(guān)接口
等待隊(duì)列頭數(shù)據(jù)結(jié)構(gòu):
struct __wait_queue_head {spinlock_t lock; //自旋鎖機(jī)制struct list_head task_list; }; typedef struct __wait_queue_head wait_queue_head_t; wait_queue_head_t:即為等待隊(duì)列頭的數(shù)據(jù)類型初始化等待隊(duì)列頭:
靜態(tài)定義等待隊(duì)列頭并初始化: #define DECLARE_WAIT_QUEUE_HEAD(name) \wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)動(dòng)態(tài)定以并初始化:#define init_waitqueue_head(q) \do { \static struct lock_class_key __key; \\__init_waitqueue_head((q), #q, &__key); \} while (0)休眠等待隊(duì)列wait_event或wait_event_interruptible:
/*** wait_event - sleep until a condition gets true* @wq: the waitqueue to wait on* @condition: a C expression for the event to wait for** The process is put to sleep (TASK_UNINTERRUPTIBLE) until the* @condition evaluates to true. The @condition is checked each time* the waitqueue @wq is woken up.** wake_up() has to be called after changing any variable that could* change the result of the wait condition.*/ #define wait_event(wq, condition) \ do { \if (condition) //判斷條件是否滿足,如果滿足則退出等待 \break; \__wait_event(wq, condition); //如果不滿足,則進(jìn)入__wait_event宏 \ } while (0)#define __wait_event(wq, condition) \ do { \DEFINE_WAIT(__wait); \/*定義并且初始化等待隊(duì)列項(xiàng),后面我們會(huì)將這個(gè)等待隊(duì)列項(xiàng)加入我們的等待隊(duì)列當(dāng)中,同時(shí)在初始化的過程中,會(huì)定義func函數(shù)的調(diào)用函數(shù)autoremove_wake_function函數(shù),該函數(shù)會(huì)調(diào)用default_wake_function函數(shù)。*/ \for (;;) { \prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \/*調(diào)用prepare_to_wait函數(shù),將等待項(xiàng)加入等待隊(duì)列當(dāng)中,并將進(jìn)程狀態(tài)置為不可中斷TASK_UNINTERRUPTIBLE;*/ if (condition) //繼續(xù)判斷條件是否滿足 \break; \schedule(); //如果不滿足,則交出CPU的控制權(quán),使當(dāng)前進(jìn)程進(jìn)入休眠狀態(tài) \} \finish_wait(&wq, &__wait);/**如果condition滿足,即沒有進(jìn)入休眠狀態(tài),跳出了上面的for循環(huán),便會(huì)將該等待隊(duì)列進(jìn)程設(shè)置為可運(yùn)行狀態(tài),并從其所在的等待隊(duì)列頭中刪除 */ \ } while (0)#define wait_event_interruptible(wq, condition) \ ({ /**如果condition為false,那么__wait_event_interruptible將會(huì)被執(zhí)行*/ \int __ret = 0; \if (!(condition)) \__wait_event_interruptible(wq, condition, __ret); \__ret; \ })喚醒等待隊(duì)列節(jié)點(diǎn)wake_up_interruptible或wake_up:
#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key); /*** __wake_up - wake up threads blocked on a waitqueue.* @q: the waitqueue* @mode: which threads* @nr_exclusive: how many wake-one or wake-many threads to wake up* @key: is directly passed to the wakeup function** It may be assumed that this function implies a write memory barrier before* changing the task state if and only if any tasks are woken up.*//**定義wake_up函數(shù)宏,同時(shí)其需要一個(gè)wait_queue_head_t的結(jié)構(gòu)體指針,在該宏中調(diào)用__wake_up方法。*/ #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL) #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)加interruptible接口標(biāo)識(shí)喚醒可中斷
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL) #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL) #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL) #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)(四)如何實(shí)現(xiàn)等待隊(duì)列
1、創(chuàng)建等待隊(duì)列頭
//1.靜態(tài)定義并初始化 #define DECLARE_WAIT_QUEUE_HEAD(name) \wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)//2.動(dòng)態(tài)定以并初始化:wait_queue_head_t name;init_waitqueue_head(q)默認(rèn)情況下會(huì)把當(dāng)前進(jìn)程作為等待任務(wù)放到等待隊(duì)列中
2、在需要休眠的地方調(diào)用休眠操作
3、在滿條件的地方喚醒等待隊(duì)列
wake_up 、wake_up_interruptible(五)實(shí)例代碼
chrdev.c
#include <linux/kernel.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/slab.h> #include <linux/interrupt.h> #include <linux/gpio.h> #include <linux/init.h> #include <linux/wait.h> #include <linux/sched.h> #include <linux/uaccess.h>#define CDEVCOUNT 5 #define CDEVNAME "cdevdevice" #define INODENAME "mycdev" int count=0; dev_t dev=0; int keyflag;//等待隊(duì)列第二個(gè)參數(shù) char keyvalue[4]={-1,-1,-1,-1}; struct cdev * cdev =NULL; struct class * cdevclass =NULL; wait_queue_head_t waithead;struct Key {unsigned int gpios;char * name;int num;unsigned int irq;};struct Key key[]={{EXYNOS4_GPX3(2),"K1",0},{EXYNOS4_GPX3(3),"K2",1},{EXYNOS4_GPX3(4),"K3",2},{EXYNOS4_GPX3(5),"K4",3},}; irqreturn_t key_handler(int irq, void * dev) {struct Key * tmp =(struct Key *)dev;int value[4];value[tmp->num]= gpio_get_value(tmp->gpios);gpio_set_value(EXYNOS4X12_GPM4(tmp->num),value[tmp->num]);keyflag = 1;keyvalue[tmp->num] = value[tmp->num];wake_up_interruptible(&waithead);printk("key%d value is %d\n",tmp->num,value[tmp->num]);printk("the current cpu is %d\n",smp_processor_id());return IRQ_HANDLED;}int cdev_open (struct inode *node, struct file *file) {printk("cdev_open is install\n");return 0; } ssize_t cdev_read (struct file *fp, char __user *buf, size_t size, loff_t *offset) {int ret =0;if((fp->f_flags & O_NONBLOCK)== O_NONBLOCK)//非阻塞{if(!keyflag)return -EAGAIN;}else//阻塞{wait_event_interruptible(waithead,keyflag);}keyflag =0;ret = copy_to_user(buf,keyvalue,4);if(ret <0)return -EFAULT;printk("cdev_read is install\n");return 0; } ssize_t cdev_write (struct file *fp, const char __user * buf, size_t size, loff_t *offset) {printk("cdev_write is install\n");return 0; } int cdev_release (struct inode *node, struct file *fp) {printk("cdev_release is install\n");return 0; } struct file_operations fop={.open=cdev_open,.read=cdev_read,.write=cdev_write,.release=cdev_release, };void mycdev_add(void) {//1.申請(qǐng)?jiān)O(shè)備號(hào)--動(dòng)態(tài)int ret =alloc_chrdev_region(&dev,0, CDEVCOUNT, CDEVNAME);if(ret)return ;//初始化cdev結(jié)構(gòu)體cdev = cdev_alloc();if(!cdev){goto out;}cdev_init(cdev,&fop);//添加字符設(shè)備到系統(tǒng)中ret =cdev_add(cdev,dev, CDEVCOUNT);if(ret){goto out1;}//創(chuàng)建設(shè)備類cdevclass = class_create(THIS_MODULE, INODENAME);if(IS_ERR(cdevclass)){goto out2;}for (count=0;count<CDEVCOUNT;count++)device_create(cdevclass, NULL, dev+count, NULL, "mydevice%d",count);out:unregister_chrdev_region(dev,CDEVCOUNT); return ; out1:unregister_chrdev_region(dev,CDEVCOUNT);kfree(cdev);return ; out2:cdev_del(cdev);unregister_chrdev_region(dev,CDEVCOUNT);kfree(cdev);return ;}static int __init dev_module_init(void) {int ret=0,i=0;unsigned long flags= IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING|IRQF_SHARED;for(i=0;i<4;i++){key[i].irq = gpio_to_irq(key[i].gpios);ret =request_irq(key[i].irq, key_handler,flags,key[i].name,(void *) &key[i]);}init_waitqueue_head(&waithead);//創(chuàng)建等待隊(duì)列頭mycdev_add();printk("this is dev_module_init \n");return 0; }static void __exit dev_module_cleanup(void) {int i=0;for(i=0;i<4;i++){key[i].irq = gpio_to_irq(key[i].gpios);free_irq(key[i].irq,(void *)&key[i]);}for(count=0;count<CDEVCOUNT;count++){device_destroy(cdevclass, dev+count);}class_destroy(cdevclass);cdev_del(cdev);unregister_chrdev_region(dev, CDEVCOUNT);kfree(cdev);printk("this is dev_module_cleanup\n"); }module_init(dev_module_init); module_exit(dev_module_cleanup); MODULE_LICENSE("GPL");chr_app.c
#include<stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>char status[]={-1,-1,-1,-1}; char key[]={-1,-1,-1,-1}; int main(int argc, char *argv[]) {int i=0,fd= open(argv[1],O_RDWR);if(fd== -1){perror("open");return -1;}while(1){if(read(fd,status,4)<0){printf("按鍵狀態(tài)未改變\n");sleep(1);continue;}for(i=0;i<4;i++){if(status[i] != key[i] ){printf("key%d is %s\n",i,status[i]?"up":"down");status[i]=key[i];}}}close(fd);return 0; }Makefile
CFLAG =-C TARGET = chrdev TARGET1 = chr_app KERNEL = /mydriver/linux-3.5 obj-m += $(TARGET).oall:make $(CFLAG) $(KERNEL) M=$(PWD)arm-linux-gcc -o $(TARGET1) $(TARGET1).c clean:make $(CFLAG) $(KERNEL) M=$(PWD) clean本文章僅供學(xué)習(xí)交流用禁止用作商業(yè)用途,文中內(nèi)容來水枂編輯,如需轉(zhuǎn)載請(qǐng)告知,謝謝合作
微信公眾號(hào):zhjj0729
微博:文藝to青年
總結(jié)
以上是生活随笔為你收集整理的(十)Linux之等待队列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三星新材是干什么的 低温储藏设备玻璃门
- 下一篇: 快餐店怎么做才吸引人 教给大家一些好用的