Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference b/w kthread and work-queues

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.

like image 346
CodeQ Avatar asked Apr 15 '14 21:04

CodeQ


2 Answers

"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).

like image 101
Kaz Avatar answered Sep 18 '22 21:09

Kaz


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.

like image 45
Rahul Avatar answered Sep 18 '22 21:09

Rahul