Kostya Krivomaz Kostya Krivomaz - 17 days ago 4x
Linux Question

Linux user space threads, kernel threads , lightweight processes

I am bit confused with all this enteties and how they interconnected in Linux. "Unix internals" book states that

lightweight process (LWP)
is kernel-supported user thread, and that kernel doesn't see threads inside processes. Is it stil true for Linux?

As I understand, user-space threads scheduled inside process, by higher level abstraction as
library, without kernel's intervention. Am I right?


In pthreads on Linux, the thread scheduling is handled by the kernel.

In Linux, a thread or process is created with the clone() system call (fork() is a special case of clone). Depending on the options passed to clone, the newly created process will be lighter weight or a heavier weight (i.e. having a separate memory space and a separate set of file descriptors etc.). pthread_create() uses clone() with a minimum amount of separation.

It is also possible to not make use of native threads at all, and instead use a fully userspace implementation of threading, using setjmp() and longjmp(). This could be used to implement some interpreted language, for example. However, I am not aware of a concrete example of a program or library that actually does implement its own userspace scheduler.

One more thing: a "kernel thread" is generally used to designate a thread that runs in kernel space, i.e. that is part of the kernel itself. In ps such threads are recognisable because they are surrounded with square brackets, like [kthreadd].