内核,内核线程和用户线程之间的区别 [英] Difference between Kernel, Kernel-Thread and User-Thread

查看:370
本文介绍了内核,内核线程和用户线程之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定,如果我完全理解上述差异,那么我想自己解释一下,就我所知,您可以打扰我: 内核是创建内核线程的最初代码.内核线程是由内核管理的进程.用户线程是进程的一部分.如果您有单线程进程,那么整个进程本身就是一个用户线程.用户线程进行系统调用,并且此系统调用由属于调用用户线程的特定内核线程提供服务,因此对于进行系统调用的每个用户线程,内核线程是创建并在内核线程完成其工作后,将控制权交还给用户线程,然后销毁内核威胁."

i'm not sure, if i totally understand the above mentioned differences, so i'd like to explain it on my own and you can interrupt me, as far as i get wrong: "A kernel is the initial piece of code which creates kernel-threads. Kernel threads are processes managed by the kernel. user-threads are part of a process. If you have a single-threaded process, than the whole process itself would be a user-thread. User-Threads make system-calls and this system-calls are served by a specific kernel-Thread which belongs to the calling user-threads. So for ervery user-thread which make a system call, a kernel-thread is created and after the kernel-thread has done its job, it gives control back to the user-thread and then the kernel-threas is destroyed."

这样可以吗?

谢谢!

许多来自德国的问候!

推荐答案

我认为对于内核和用户而言,这不是一个很好的思维模型.我认为查看这些抽象的实现以完全理解它们很有用:

I don't think that's a very good mental model for kernel vs user. I think it's useful to look at the implementation of these abstractions in order to fully understand them:

内核基本上只是一块内存.它有足够的特权可以先加载,从而可以设置CPU的中断向量.

A kernel is basically just a piece of memory. It was privileged enough to be loaded before anything else, thereby allowing it to set the CPU's interrupt vectors.

中断控制一切,包括I/O,计时器和虚拟内存.这意味着内核可以决定如何处理所有内容.

Interrupts control everything, including I/O, timers, and virtual memory. That means that the kernel gets to decide how all that is handled.

一个库也只是一块内存,您可以很好地将内核视为系统调用库".但是因为内核代表硬件,所以该内存在每个人之间都是共享的.

A library is also just a piece of memory, and you can very well look at the kernel as the "system call library", among other things. But because the kernel represents the hardware, that piece of memory is shared among everyone.

内核模式是CPU的自然"模式,没有任何限制(在x86 CPUS上为"ring 0").用户模式(在x86 CPU上为环3")是指当使用某些指令或访问某些内存位置时,指示CPU触发中断.这样,当用户尝试访问内核内存或代表I/O端口的内存或硬件内存(例如GPU的帧缓冲区)时,内核就可以让CPU执行特定的内核代码.

Kernel mode is the CPU's "natural" mode, with no restrictions (on x86 CPUS - "ring 0"). User mode (on x86 CPUs - "ring 3") is when the CPU is instructed to trigger an interrupt whenever certain instructions are used or whenever some memory locations are accessed. This allows the kernel to have the CPU execute specific kernel code when the user tries to access kernel memory or memory representing I/O ports or hardware memory such as the GPU's frame buffer.

进程也是一块内存,由它自己的堆和库使用的内存组成,其中包括内核.

A process is also just a piece of memory, consisting of its own heap and the memory used by libraries, among which is the kernel.

一个线程(=一个调度单元)只是一个堆栈,其堆栈具有内核知道并跟踪的ID.那就是线程运行时CPU使用的调用堆栈.用户线程有2个堆栈:一个用于用户模式,一个用于内核模式-但是它们仍然具有相同的ID.

A thread (= a unit of scheduling) is just a stack with an ID that the kernel knows of and tracks. That's the call stack that the CPU uses when the thread is running. User threads have 2 stacks: one for user mode and one for kernel mode - but they still have the same ID.

由于内核控制计时器,因此它将计时器设置为关闭,例如每1毫秒一次.当计时器触发时(计时器中断"),CPU运行内核为该中断设置的回调,在该回调中,内核可以看到当前线程已经运行了一段时间,并决定取消调度,而调度另一个线程

Because the kernel controls timers, it sets up a timer to go off e.g. every 1 ms. When the timer triggers ("timer interrupt"), the CPU runs the callback that the kernel set up for that interrupt, where the kernel can see that the current thread has been running for a while and decide to unschedule it and schedule another thread instead.

虚拟内存上下文"是指CPU可以访问的所有内存.这包括进程的所有内存-包括用户模式堆和库的内存,所有进程线程的用户模式调用堆栈,系统中所有线程的内核模式堆栈,内核的堆内存,I/O端口,以及硬件内存.

By "virtual memory context" I mean all the memory that can be accessed by the CPU. This includes all the memory of the process - including the user-mode heap and memory of libraries, user-mode call stacks of all process threads, kernel-mode stack of all threads in the system, the kernel's heap memory, I/O ports, and hardware memory.

当发生中断或系统调用时,虚拟内存上下文不会更改,仅将CPU标志翻转(即从环3转到环0),并且CPU现在返回其自然"内核模式,其中它可以自由访问内核内存,I/O端口和硬件内存.

When an interrupt or a system call occur, the virtual memory context doesn't change, only a CPU flag is flipped (i.e. from ring 3 to ring 0) and the CPU is now back in its "natural" kernel mode where it can freely access kernel memory, I/O ports and hardware memory.

创建新进程时,实际发生的情况是创建了一个新线程,并为其分配了新的虚拟内存上下文.因此,每个进程都从单线程开始.该线程稍后可以通过系统调用要求内核创建更多共享虚拟内存上下文(=进程)的线程(=堆栈),或要求内核创建更多线程,每个线程都具有一个新的虚拟内存上下文(=新进程). ).

When a new process is created, what actually happens is that a new thread is created, and assigned a new virtual memory context. Therefore, every process starts as single-threaded. That thread can later ask the kernel via a system call to create more threads (= stacks) which share its virtual memory context (= process), or ask the kernel to create more threads, each with a new virtual memory context (= new processes).

与其他任何库一样,内核可以具有自己的后台线程以进行优化.当出现这种需求时(在为系统调用提供服务时,任何进程的内存上下文中都可能发生这种情况),内核将创建新线程并为它们提供特殊的内存上下文,该上下文仅包含内核内存,而没有访问任何进程的内存.

Like any other library, the kernel can have its own background threads for optimization purposes. When such a need arises (which can happen in the memory context of any process when servicing a system call), the kernel will create new threads and give them a special memory context, which is a context that only contains the kernel's memory, with no access to memory of any process.

这篇关于内核,内核线程和用户线程之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆