2
votes

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 pthread library, without kernel's intervention. Am I right?

3

3 Answers

4
votes

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].

0
votes

Indeed, for threads implemented in user-space, the kernel isn't aware of threads. So if one thread blocks, the other threads belonging to the same process block as well, as the OS isn't aware of threading.

0
votes

a LWP(lightweight processes) runs in user space on top of a single kernel thread and shares its address space and system resources with other LWPs within the same process.

FROM: https://en.wikipedia.org/wiki/Light-weight_process

In linux, lwp is on top of kernel thread, and share resources. some language like golang(coroutine) have user thread by himself scheduler.