Linux 裝置驅動--- 核心等待隊列 --- wait_queue_head --- wait_event_interruptible --- 按鍵驅動程式最佳化__Linux

來源:互聯網
上載者:User
等待隊列:

          在 Linux 驅動程式設計中,可以使用等待隊列來實現進程的阻塞.

          等待隊列可以看作儲存進程的容器,在阻塞進程時,將進程放入等待隊列;

          當喚醒進程時,從等待隊列中取出進程.


等待隊列的 定義 和 初始化 wait_queue_head_t    DECLARE_WAIT_QUEUE_HEAD  :

          Linux 2.6 核心提供了如下關於等待隊列的操作:
          1,定義等待隊列.

                    wait_queue_head_t   my_queue
          2,初始化等待隊列.

                    init_waitqueue_head ( &my_queue )
          3,定義並初始化等待隊列.

                   DECLARE_WAIT_QUEUE_HEAD  ( my_queue )


等待隊列的 睡眠  wait_event_interruptible : 有條件睡眠:

              1,  wait_event ( queue , condition )

                    當 condition ( 一個布林運算式 ) 為真,立即返回;否則讓進程進入 TASK_UNINTERRUPTIBLE 模式

                    睡眠,並掛在 queue 參數所指定的等待隊列上.


             2,  wait_event_interruptible ( queue , condition )

                    當 condition ( 一個布林運算式 ) 為真,立即返回;否則讓進程進入 TASK_INTERRUPTIBLE 模式

                    睡眠,並掛在 queue 參數所指定的等待隊列上.


              3, int  wait_event_killable ( wait_queue_t  queue , condition )

                    當 condition ( 一個布林運算式 ) 為真,立即返回;否則讓進程進入 TASK_KILLABLE 模式

                    睡眠,並掛在 queue 參數所指定的等待隊列上.


無條件睡眠:

                    ( 老版本,不建議使用 )

                    sleep_on  ( wait_queue_head_t  *q )

                    讓進程進入 不可中斷 的睡眠,並把它放入等待隊列 q.


                    interruptible_sleep_on  ( wait_queue_head_t  *q )

                    讓進程進入 可中斷 的睡眠,並把它放入等待隊列 q.


等待隊列中喚醒進程 wake_up :

                    wake_up ( wait_queue_t  *q )

                    從等待隊列 q 中喚醒狀態為 TASKUNINTERRUPTIBLE ,TASK_INTERRUPTIBLE ,TASK_KILLABLE

                    的所有進程.

                    wake_up_interruptible ( wait_queue_t  *q )

                    從等待隊列 q 中喚醒狀態為 TASK_INTERRUPTIBLE 的進程.


執行個體 --- 按鍵驅動程式 最佳化:

下面列出一個執行個體,方便理解和使用 等待隊列:

比如我們在編寫 按鍵驅動程式的時候,我們的 應用程式 採用 while(1) 一直去 read 按索引值,這樣的話 CPU 限定佔用過大;

所以,我們採用 等待隊列 來最佳化按鍵驅動程式:


首先   定義並且初始化 等待隊列:

在程式開頭 定義並且初始化 等待隊列  DECLARE_WAIT_QUEUE_HEAD 

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);


並定義一個 static volatile 變數  :

static volatile int ev_press = 0;


然後 在 read 方法中 將等待隊列睡眠:

在有按鍵按下的時候,讀取按索引值;

沒有按鍵按下 的情況下將等待隊列睡眠睡眠  wait_event_interruptible 

static int tq2440_irq_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){unsigned long err;if (!ev_press){if (filp->f_flags & O_NONBLOCK)return -EAGAIN;elsewait_event_interruptible(button_waitq, ev_press);}ev_press = 0;err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));return err ? -EFAULT : min(sizeof(key_values), count);}



再在  中斷服務程式中 喚醒等待隊列:

在 中斷服務程式中 喚醒等待隊列  wake_up_interruptible 

在 按鍵按下時,進入中斷服務程式,在這時候 將等待隊列喚醒

static irqreturn_t irq_interrupt(int irq, void *dev_id){struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;int down;down = !s3c2410_gpio_getpin(button_irqs->pin);if (down != (key_values[button_irqs->number] & 1)){key_values[button_irqs->number] = '0' + down;ev_press = 1;wake_up_interruptible(&button_waitq);}return IRQ_RETVAL(IRQ_HANDLED);}

通過 對 static volatile int ev_press 變數的值 判斷,來確定 等待隊列 是睡眠 還是馬上讀取索引值 .


測試 --- 按鍵驅動程式: 1,insmod 驅動;



2,在後台運行 測試應用程式:



3,ps  命令查看 應用程式狀態:

buttons  stat 狀態為 sleep;



4,cat /proc/interrupts 命令 看中斷有沒有被申請:

看各個按鍵的中斷有沒有被申請  KEY1-4 :



5,測試按鍵:











聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.