How to create light weight kernel thread?

When I create a kernel thread (kthread_run), it becomes a new process.(I could see it using top command) . How can I create a light weight kernel thread(like the one we have in user space)?

If I am not wrong, kthread_create will eventually call fork() which will call clone() with appropriate configuration to create a new process/lw process. Is it possible to create lw kernel thread using clone() or similar apis? Thanks so much in advance.

Upvotes: 1

Views: 647

Answers (2)

Chris Tsui
Chris Tsui

Reputation: 472

Lightweight threads in user space are just a group of processes(or tasks) share the same address space and many other resource. Also, lightweight thread is created faster than a normal process. Linux uses 1 to 1 mapping model, that is, every thread in user space is implemented as a separate process in kernel space.

In Linux, Kernel thread is a process which does not have a valid user space. They are scheduled as normal process, but never enter user land.

So, the answer is that when you understand the meaning of lightweight, you will know that there isn't lightweight kernel thread at all. All kernel threads share the same kernel space address naturally.

Also, top is just a user program, weather appear in top output does not really reflect the nature of the underlying kernel implementation.

Upvotes: 1

Ctx
Ctx

Reputation: 18420

Kernel threads are always listed in the process table, but this is merely a cosmetical issue. They share the same address space and *-tables, so in this sense they are quite lightweight anyway (i.e. a context-switch isn't very expensive).

If your 2*16 kernel-threads mainly do the same thing, it might be worthy evaluating if the functionality can be moved into a seperate kernel-module, which exposes an API to be used by all 16 kernel-modules and doing the work in only 1 or 2 threads.

Upvotes: 2

Related Questions