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 pthread library, without kernel’s intervention. Am I right?
3 Answers 3
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] .
Understanding the concept of LWP in liunx kernel
I study the OS book[operating system concepts] written by Silberschatz recently. At chapter 5, the book describes the concept of «Contention scope», and mentions that there are two types of contention scope. One is PTHREAD_SCOPE_PROCESS and the other is PTHREAD_SCOPE_SYSTEM. And for PTHREAD_SCOPE_SYSTEM, the user level thread will bind to one LWP[virtual processor], and this LWP will attaches to one kernel thread. Actually, I’m not quite understanding the concept of LWP. On the command line terminal I can type the following command:
And I can see one process[PID] may have several LWP[thread IDs]. By doing some survey, I found that LWP seems simply mean thread or tasks in linux. like the following link: How Linux handles threads and process scheduling So I have the following two questions: Q1 : Is LWP defined as virtual processor in linux? what source code in kernel defines it? Q2: In the following picture, syslog has the PID [852] and has four threads. And these four LWPs will each bind one kernel thread? Or kernel will just schedule them directly? The above picture is taken from: http://www.softprayog.in/tutorials/ps-command-usage-examples-in-linux Thanks in advance
1 Answer 1
POSIX.1 requires that an implementation support at least one of these contention scopes. Linux supports PTHREAD_SCOPE_SYSTEM, but not PTHREAD_SCOPE_PROCESS.
In Linux, a process’ multiple threads are like separate processes that happen to share the same address space. Yes, this is a complete oversimplification, they share other resources as well — notably, signal handlers, file descriptors and various IDs — and there are other more subtle differences, but they are each full-fledged independently schedulable entities. The terminology used within the kernel is «task». Every process has at least one task. A multithreaded process has more than one.
Q1: LWP is not the same as a virtual processor (whatever you mean by that — it’s not something that’s familiar in this context). An LWP corresponds to a task in linux. Although outside kernel development, «process» and «thread» are still the more common terms.
Q2: Each LWP corresponds to a separate task (or user thread), and each is separately schedulable by the kernel. (In linux, the term «kernel thread» has a specific meaning that is quite different: a kernel thread is a thread [really task] which spends its entire life inside the kernel. They’re typically used for various internal housekeeping functions.)