1

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.

  • @Neijwiert: If I'm not mistaken, kernel threads and posix threads are two completely different concepts, so I'm not sure if that would help Karthik – MikeMB Jan 11 '16 at 12:12
  • @MikeMB I'm also unsure what OP wants, since he/she is asking "like the one we have in user space" – Neijwiert Jan 11 '16 at 12:13
  • I am writing 16 kernel modules in which I need to create two threads each. I don't want kernel to list these threads as processes. Like new processes are not created for user threads, I want to achieve similar behavior for kernel threads too. Please get back if I am not understandable – Karthik Raj Palanichamy Jan 11 '16 at 12:22
  • 1
    `Like new processes are not created for user threads` - If by *user threads* you assume `pthreads`, then you are mistaken: in Linux `pthreads` are created with `clone`, that is every pthread is actually based on the process(or task, as noted by Matteo Italia below). Of course, parameters for `clone` are differ for `fork()` and for `pthread_create()`. If you mean by `user threads` something else, then, please, clarify. – Tsyvarev Jan 11 '16 at 12:38
  • The threads you have "in user space" on any recent Linux machine I know of are made exactly like those you describe in kernel space - they create a new "task" which shares most of the resources (address space, file table, ...) with the parent. IOW, on Linux processes and threads are handled in the same way by the kernel - they are "tasks" (schedulable entities) with some associated resources, which may (threads) or may not (processes) be shared with other tasks. – Matteo Italia Jan 11 '16 at 12:38

2 Answers2

2

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.

Ctx
  • 18,090
  • 24
  • 36
  • 51
1

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.

Chris Tsui
  • 452
  • 3
  • 10
  • as far as I know, kernel does not know even how many threads does a process have. User level threading has nothing to do with the kernel and that's why a blocking thread affects all other threads.- referred some websites and they say the same. – Karthik Raj Palanichamy Jan 12 '16 at 18:00
  • @Karthik, no, kernel knows everything. _tid_ is for thread id and _pid_ for process id. http://stackoverflow.com/questions/4517301/difference-between-pid-and-tid – Chris Tsui Jan 13 '16 at 12:12