簡體   English   中英

何時在 Linux 內核中使用內核線程與工作隊列

[英]When to use kernel threads vs workqueues in the linux kernel

在 linux 內核中有很多調度工作的方法:定時器、tasklet、工作隊列和內核線程。 何時使用一種或另一種的指南是什么?

有明顯的因素:定時器函數和小任務無法休眠,因此它們無法等待互斥鎖、條件變量等。

在驅動程序中為我們選擇哪種機制的其他因素是什么?

哪些是首選機制?

softirqs : deferred work runs in interrupt context
tasklets : deferred work runs in interrupt context
work queues : deferred work runs in process context

softirqs : Softirqs of the same type can run concurrently on several CPUs.
tasklets : Tasklets of different types can run concurrently on several CPUs, but tasklets of the same type cannot.
work queues : can run simultaneously on different CPU's

softirqs : cannot go to sleep
tasklets : cannot go to sleep
work queues : can go to sleep

softirqs : cannot be preempted/schedule
tasklets : cannot be preempted/schedule
work queues : maybe be preempted/schedule

softirqs : not easy to use
tasklets : easy to use
work queues : easy to use

正如你所說,這取決於手頭的任務:

工作隊列將工作推遲到內核線程中 - 您的工作將始終在進程上下文中運行。 它們是可調度的,因此可以睡覺。

通常,工作隊列或 sotftirqs/tasklet 之間沒有爭論; 如果延遲的工作需要休眠,則使用工作隊列,否則使用軟中斷或小任務。 Tasklet 也更適合於中斷處理(它們得到了某些保證,例如:一個 tasklet 永遠不會晚於下一個滴答運行,它總是相對於自身進行序列化,等等)。

當您確切地知道您希望某事發生的時間並且不想在此期間中斷/阻止進程時,內核計時器非常有用。 它們在進程上下文之外運行,並且它們對於其他代碼也是異步的,因此如果您不小心,它們就是競爭條件的來源。

希望這可以幫助。

內核線程構成了工作隊列的基礎。 它們是在進程上下文中運行的唯一類型的內核幫助程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM