1

Based on my understanding from Understanding the Linux Kernel, is it correct that:

  • kernel threads and light weight processes in Linux kernel are both represented by task_struct structure in C.

  • kernel threads always run in kernel space, while light weight processes can run in both user space and kernel space

  • kernel threads and light weight processes are two concepts at the same level. They do not depend on each other? In particular, a light weight process is not created and run based on a kernel thread?

Then why does the following quote from Operating System Concepts mean by saying that a light weight process is attached to a kernel thread? (Note that this book is for general OS concepts, and the quote doesn't say it is for Linux, but most parts of the book are applicable to Linux)

A final issue to be considered with multithreaded programs concerns communication between the kernel and the thread library, which may be required by the many-to-many and two-level models discussed in Section 4.3.3. Such coordination allows the number of kernel threads to be dynamically adjusted to help ensure the best performance.

Many systems implementing either the many-to-many or the two-level model place an intermediate data structure between the user and kernel threads. This data structure—typically known as a lightweight process, or LWP—is shown in Figure 4.13. To the user-thread library, the LWP appears to be a virtual processor on which the application can schedule a user thread to run. Each LWP is attached to a kernel thread, and it is kernel threads that the operating system schedules to run on physical processors. If a kernel thread blocks (such as while waiting for an I/O operation to complete), the LWP blocks as well. Up the chain, the user-level thread attached to the LWP also blocks.

enter image description here

where several concepts are defined in the book as following, in particular the definitions of kernel threads and user threads might be different from those that I mentioned in my questions at the beginning of this post:

support for threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads. User threads are supported above the kernel and are managed without kernel support, whereas kernel threads are supported and managed directly by the operating system.

The many-to-one model (Figure 4.5) maps many user-level threads to one kernel thread. Thread management is done by the thread library in user space, so it is efficient

enter image description here

The many-to-many model (Figure 4.7) multiplexes many user-level threads to a smaller or equal number of kernel threads. The number of kernel threads may be specific to either a particular application or a particular machine (an application may be allocated more kernel threads on a multiprocessor than on a single processor).

enter image description here

Thanks.

  • Actually, these concept are very confusing, depending on context, could be anything that looks like "process or thread" you learn from the general operating system class. – 炸鱼薯条德里克 Oct 2 '18 at 4:07
1

Kernel threads always run in kernel space. User threads are run in user space, i.e they have a user mode address space. User threads can be scheduled either with or without the support of the kernel. That's what the latter part of your question means by "kernel threads". In short, the same name is used for two things.

  • Thanks. (1) "That's what the latter part of your question means by "kernel threads". In short, the same name is used for two things." What do you mean by "the latter part of your question" and "That"? (2) My post is about relation between light weight process and kernel threads, not just about the meanings of user threads and kernel threads. – Tim Sep 29 '18 at 17:21
  • The three bullets at the top talks about the kind of kernel threads that are completely contained in the kernel, i.e. they are threads that are scheduled by the kernel and have an address space that does not have a user space part. These are helper threads running in the kernel and typically have names starting with the letter k: ksoftirqd, khugepaged etc. The "latter part" starts after the bullets. In the quoted text the term kernel thread means "scheduled by the kernel", i.e. the kernel decides when the thread runs and for how long. These "kernel threads" are unrelated to the first type. – Johan Myréen Sep 29 '18 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.