I am little confused b/w workqueues and kthread when they are created as following-
Create kthread for each online CPU and bind to 1 unique CPU
for_each_online_cpu(cpu) {
kthread = kthread_create(func, ...);
kthread_bind(kthread, cpu);
}
//Each kthread will process work in serialized manner
Create BOUND workqueue for each online CPU with @max_active as 1
for_each_online_cpu() {
wq = alloc_workqueue(name, WQ_MEM_RECLAIM, 1)
}
// queue_work_on(cpu, work) will ensure the works queued on a particular CPU are
processed in a serialized manner.
Please let me know if my understanding is correct and what are the advantages of kthread over workqueues and vice-versa.
Thanks in advance.
"Work" is some action that should complete in a reasonable time. Though it can sleep, it shouldn't do so for a long time, because multiple work items share the the same worker thread.
A thread is yours to run for as long as you want. It doesn't have to return to some caller in order to do other work, so you can put it in a loop (and that is usually done). The loop can contain arbitrary sleeps.
Work queues are used in situations when the caller cannot do the intended action itself, for instance because it is an interrupt service routine and the work is too long for an interrupt, or is otherwise inappropriate to run in an interrupt (because it requires a process context).
First thing is, Workqueue is also a kthread. Now If you are just using the default queue, you will declare the work function and schedule_work()
, which in turn will add you work function to the default workqueue for that processor.This default workqueue is nothing but a kthread which was created at the time of boot up.
Now about the advantage and disadvantage, workqueue is used in a very specific scenario : when you want to delay your work for some later time. As @Kaz mentioned one of the situation could be when you are in interrupt handler and wants to come out as soon as possible. So with workqueue you can schedule your work for some later time while normal kthread can't be delayed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With