0

Can any one help me to understand difference between below mentioned APIs in Linux kernel:

struct workqueue_struct *create_workqueue(const char *name); 
struct workqueue_struct *create_singlethread_workqueue(const char *name);

I had written sample modules, when I try to see them using ps -aef, both have created a workqueue, but I was not able to see any difference.

I have referred to http://www.makelinux.net/ldd3/chp-7-sect-6, and according to LDD3:

If you use create_workqueue, you get a workqueue that has a dedicated thread for each processor on the system. In many cases, all those threads are simply overkill; if a single worker thread will suffice, create the workqueue with create_singlethread_workqueue instead.

But I was not able to see multiple worker threads (each for a processor).

red0ct
  • 4,840
  • 3
  • 17
  • 44
sanumala
  • 201
  • 1
  • 5
  • 16

2 Answers2

1

Workqueues have changed since LDD3 was written.

These two functions are actually macros:

#define create_workqueue(name)                                          \
        alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, (name))
#define create_singlethread_workqueue(name)                             \
        alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1, (name))

The alloc_workqueue documentation says:

Allocate a workqueue with the specified parameters. For detailed information on WQ_* flags, please refer to Documentation/workqueue.txt.

That file is too big to quote entirely, but it says:

alloc_workqueue() allocates a wq. The original create_*workqueue() functions are deprecated and scheduled for removal.
[...]
A wq no longer manages execution resources but serves as a domain for forward progress guarantee, flush and work item attributes.

CL.
  • 173,858
  • 17
  • 217
  • 259
-1
if(singlethread){

    cwq = init_cpu_workqueue(wq, singlethread_cpu);
    err = create_workqueue_thread(cwq, singlethread_cpu);
    start_workqueue_thread(cwq, -1);

}else{

    list_add(&wq->list, &workqueues);
    for_each_possible_cpu(cpu) {    
                cwq = init_cpu_workqueue(wq, cpu);
                err = create_workqueue_thread(cwq, cpu);
                start_workqueue_thread(cwq, cpu);
    }
}
Oldskool
  • 34,211
  • 7
  • 53
  • 66
leesagacious
  • 182
  • 1
  • 8
  • 6
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – kayess Aug 25 '16 at 13:17