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

查看:613
本文介绍了何时在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?

显而易见的因素是:计时器功能和小任务无法进入睡眠状态,因此它们无法等待互斥量,条件变量等.

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之间没有争论;如果延迟的工作需要睡眠,则使用工作队列,否则使用softirqs或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天全站免登陆