内核线程和用户线程及其机制

发布于 2025-01-30 17:13:48 字数 3421 浏览 3 评论 0原文

由于“内核模式”,“用户模式”,“用户线程和内核线程完全相同,我都与理解内核线程,用户线程,线程模型感到困惑。 任何人都可以澄清一下吗?

内核线

  • 螺纹是内核计划的“轻量级”单元。
  • 每个过程中至少存在一个内核线
  • 如果一个过程中存在多个内核线程,则它们共享相同的内存并文件资源
  • 内核线程除了堆栈,包括程序计数器在内的寄存器的副本和线程本地存储(如果有的话),没有资源,因此创建和破坏相对便宜。
  • 内核可以为系统中的每个逻辑核心分配一个线程(因为如果每个处理器支持多线程,则每个处理器将自己分为多个逻辑内核,或者仅支持一个逻辑核心,如果不支持一个逻辑核心) ,并且可以交换被阻塞的线程。
  • 但是,内核线程的时间比要交换的用户线程更长。

thread,wikipedia

  • 内核线程是由操作系统维护的线程对象。
  • 这是一个实际线程,能够由处理器安排和执行
  • 内核线程调度程序负责调度内核线程

threads:为什么所有用户都必须是所有用户线程映射到内核线程?,stackoverflow

用户线程

  • 有时在用户空间库中实现,因此称为用户线程
  • 内核不知道它们,因此它们在用户空间中进行管理和安排。
  • 虚拟机实现的用户线程也称为绿色线程

thread,wikipedia

  • 每个用户线程实际上都无法自行运行,用户线程运行的唯一方法是实际上告诉A 内核线程执行用户线程中包含的代码

threads:为什么所有用户都必须是所有用户线程映射到内核线程?,stackoverflow

绿色线程

  • 绿色线程或虚拟线程[有争议的 - 讨论]是由运行时库或虚拟机(VM)安排的线程,而不是通过基础操作系统来定位的线程(OS)

green threads,wikipedia

线程模型

“线程模型”

1:1模型

  • 线程由用户在1:1中与内核中的可计划实体的对应关系是最简单的线程实现。 OS/2和 Win32从Start 开始使用这种方法。

thread,wikipedia

到目前为止,我的理解如下。

  • 在窗口中,线程模型为1:1。
  • 如果我调用CreateThread函数,则创建2个线程:1个用户线程,1个内核线程。
  • 至于“每个用户线程实际上不能自行运行”和“内核线程实际上被告知执行用户线程中包含的代码”,内核线程被告知执行用户线程中包含的代码,这是lpthread_start_routine。
- 创建函数需要kernel32.dll。
- 创建函数返回线程的句柄,这意味着“操作系统维护的线程对象”。
- 由CreateThread创建的线程在多核环境中同时执行,这意味着“内核可以为每个逻辑核心分配一个线程”。
  • CreateThread至少创建内核线程。

但是,在此引用中,

  • 用户线程和内核线程完全相同。
  • 用户线程是执行用户空间代码的线程。
  • 内核线程是仅运行内核代码并且与用户空间过程没有关联的线程。
  • 实际上,所有线程都从内核空间开始。
  • 在X86 Windows上的进程中,线程在用户和内核模式之间交替(在程序和OS/System调用之间)。存在内核工作线程,但它们并不直接参与进程的执行。

用户 - 级别和内核 - 支持线程之间的差异?

弄乱了我的想法。 这是什么意思?

这意味着创建了1个内核线程(Ex。CreateThread),并且此内核线程在用户和内核模式之间交替?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文