何时在 Linux 内核中使用内核线程与工作队列 [英] When to use kernel threads vs workqueues in the linux kernel

查看:21
本文介绍了何时在 Linux 内核中使用内核线程与工作队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux 内核中有多种调度工作的方法:定时器、tasklet、工作队列和内核线程.何时使用一种或另一种的指南是什么?

There are many ways to schedule work in the linux kernel: timers, tasklets, work queues, and kernel threads. What are the guidelines for when to use one vs another?

有一个明显的因素:定时器函数和tasklet不能休眠,所以不能等待互斥体、条件变量等.

There are the obvious factors: timer functions and tasklets cannot sleep, so they cannot wait on mutexes, condition variables, etc.

在驱动程序中为我们选择哪种机制的其他因素是什么?

What are the other factors in choosing which mechanism to us in a driver?

哪些是首选机制?

推荐答案

正如你所说,这取决于手头的任务:

As you said, it depends on the task at hand:

工作队列将工作推迟到内核线程中 - 您的工作将始终在进程中运行语境.他们是可安排的,因此可以睡觉.

Work queues defer work into a kernel thread - your work will always run in process context. They are schedulable and can therefore sleep.

通常情况下,工作队列或 sotftirqs/tasklet 之间没有争论;如果延迟的工作需要休眠,则使用工作队列,否则使用软中断或小任务.Tasklet 也更适合于中断处理(它们得到了某些保证,例如:一个 tasklet 永远不会晚于下一个滴答运行,它总是相对于自身进行序列化等).

Normally, there is no debate between work queues or sotftirqs/tasklets; if the deferred work needs to sleep, work queues are used, otherwise softirqs or tasklets are used. Tasklets are also more suitable for interrupt handling (they are given certain assurances such as: a tasklet is never ran later than on the next tick, it's always serialized with regard to itself, etc.).

当您确切地知道希望某事发生的时间并且不希望在此期间中断/阻止进程时,内核计时器非常有用.它们在进程上下文之外运行,并且对于其他代码也是异步的,因此如果您不小心,它们就是竞争条件的来源.

Kernel timers are good when you know exactly when you want something to happen, and do not want to interrupt/block a process in the meantime. They run outside process context, and they are also asynchronous with regard to other code, so they're the source of race conditions if you're not careful.

希望这会有所帮助.

这篇关于何时在 Linux 内核中使用内核线程与工作队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆