Kernel thread and user thread and their mechanism

878 views Asked by At

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?

0

There are 0 answers