0

I having confusion with understanding kernel thread, user thread, thread model due to 'kernel mode', 'user mode', 'User threads and Kernel threads are exactly the same.'. Can anyone clarify this?

Kernel thread

  • A kernel thread is a "lightweight" unit of kernel scheduling.
  • At least one kernel thread exists within each process.
  • If multiple kernel threads exist within a process, then they share the same memory and file resources.
  • Kernel threads do not own resources except for a stack, a copy of the registers including the program counter, and thread-local storage (if any), and are thus relatively cheap to create and destroy.
  • The kernel can assign one thread to each logical core in a system (because each processor splits itself up into multiple logical cores if it supports multithreading, or only supports one logical core per physical core if it does not), and can swap out threads that get blocked.
  • However, kernel threads take much longer than user threads to be swapped.

Thread, wikipedia

  • A kernel thread is a thread object maintained by the operating system.
  • It is an actual thread that is capable of being scheduled and executed by the processor.
  • The kernel thread scheduler is in charge of scheduling kernel threads.

Threads: Why must all user threads be mapped to a kernel thread?, stackoverflow

User thread

  • Threads are sometimes implemented in userspace libraries, thus called user threads.
  • The kernel is unaware of them, so they are managed and scheduled in userspace.
  • User threads as implemented by virtual machines are also called green threads.

Thread, wikipedia

  • Each user thread can't actually run on its own, and the only way for a user thread to run is if a kernel thread is actually told to execute the code contained in a user thread.

Threads: Why must all user threads be mapped to a kernel thread?, stackoverflow

Green thread

  • green threads or virtual threads[disputed – discuss] are threads that are scheduled by a runtime library or virtual machine (VM) instead of natively by the underlying operating system (OS).

Green threads, wikipedia

Thread model

Thread model

1:1 model

  • Threads created by the user in a 1:1 correspondence with schedulable entities in the kernel are the simplest possible threading implementation. OS/2 and Win32 used this approach from the start.

Thread, wikipedia

So far, my understanding was as below.

  • In window, thread model is 1:1.
  • If I call CreateThread function, 2 threads are created: 1 user thread, 1 kernel thread.
  • As far as "Each user thread can't actually run on its own" and "kernel thread is actually told to execute the code contained in a user thread", kernel thread is told to execute the code contained in a user thread, which is LPTHREAD_START_ROUTINE.
- CreateThread function requires Kernel32.dll.
- CreateThread function returns HANDLE of a thread, implying "a thread object maintained by the operating system".
- Threads created by CreateThread are executed simultaneously in multi-core environment, implying "the kernel can assign one thread to each logical core".
  • CreateThread at least creates kernel thread.

However, in this reference,

  • User threads and Kernel threads are exactly the same.
  • A User thread is one that executes user-space code.
  • A Kernel thread is one that only runs kernel code and isn't associated with a user-space process.
  • In fact, all threads start off in kernel space.
  • In processes on x86 Windows, threads alternate between the user and kernel modes (between the program and the OS/system calls). There exist kernel worker threads, but they aren't involved directly nor always in execution of processes.

Difference between user-level and kernel-supported threads?

This messed up my mind. What does this mean?

Does it mean 1 kernel thread (ex. CreateThread) is created and this kernel thread alternate between the user and kernel modes?

YoonSeok OH
  • 647
  • 2
  • 7
  • 15
  • 1
    "Does it mean 1 kernel thread (ex. CreateThread) is created and this kernel thread alternate between the user and kernel modes?" - In 1:1 Thread model there is bi-directional correspondence between an user thread and a kernel thread. So, in that model you could **imagine** both these threads as a single one, which changes operating modes between kernel and user. Note, that the concept of a "user thread" is **specific** for a **library**. Different libraries may provide different properties for their threads. – Tsyvarev May 20 '22 at 09:58
  • 2
    'If I call CreateThread function, 2 threads are created: 1 user thread, 1 kernel thread' no. If you call the Windows API 'CreateThread()', another kernel-managed thread is created. – Martin James May 20 '22 at 11:36
  • 2
    The kernel-managed threads execute user code until they make a system call or they are interrupted by hardware. – Martin James May 20 '22 at 11:39
  • Thanks for your comment Tsyvarev. "So, in that model you could imagine both these threads as a single one, which changes operating modes between kernel and user" Does imagine mean there are two threads actually? – YoonSeok OH May 20 '22 at 14:15
  • Thanks for your comment Martin James. Does "another kernel-managed thread is created" mean 1 kernel thread is created. (No user thread) And this kernel thread executes user code? Does it mean this kernel thread actually operates as user thread and kernel thread by switching over user and kernel mode? Then, where did this "A User thread is one that executes user-space code." and "A Kernel thread is one that only runs kernel code and isn't associated with a user-space process." come from? – YoonSeok OH May 20 '22 at 14:23

0 Answers0