multithreading 内核线程和用户线程有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4985182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between kernel threads and user threads?
提问by tazim
What is the difference between kernel threads and user threads? Is it that kernel thread are scheduled and executed in kernel mode? What are techniques used for creating kernel threads?
内核线程和用户线程有什么区别?内核线程是在内核模式下调度和执行的吗?用于创建内核线程的技术有哪些?
Is it that user thread is scheduled, executed in user mode? Is it that Kernel does not participate in executing/scheduling user threads? When interrupts occur in executing user thread then who handles it?
是不是用户线程被调度,在用户态执行?是不是内核不参与执行/调度用户线程?当执行用户线程时发生中断,那么谁来处理呢?
Whenever, thread is created a TCB is created for each. now in case of user level threads Is it that this TCB is created in user's address space ?
每当创建线程时,都会为每个线程创建一个 TCB。现在在用户级线程的情况下,这个 TCB 是在用户地址空间中创建的吗?
In case of switching between two user level threads who handles the context switching ?
如果在处理上下文切换的两个用户级线程之间切换?
There is a concept of multithreading models :
有一个多线程模型的概念:
- Many to one
- One to one
- Many to Many.
- 多对一
- 一对一
- 多对多。
What are these models? How are these models practically used?
这些模型是什么?这些模型如何实际使用?
Have read few articles on this topic but still confused
Wants to clear the concept ..
已经阅读了几篇关于这个主题的文章,但仍然很困惑
想要澄清这个概念..
Thanks in advance, Tazim
提前致谢,塔齐姆
采纳答案by Jeff
What is the difference between kernel threads and user threads?
内核线程和用户线程有什么区别?
Kernel threads are privileged and can access things off-limits to user mode threads. Take a look at "Ring (Computer Security)" on Wikipedia. On Windows, user mode corresponds to Ring 3, while kernel mode corresponds to Ring 0.
内核线程具有特权,可以访问用户模式线程禁止访问的内容。看看维基百科上的“ Ring(计算机安全)”。在 Windows 上,用户模式对应 Ring 3,而内核模式对应 Ring 0。
What are techniques used for creating kernel threads?
用于创建内核线程的技术有哪些?
This is extremely dependent upon the operating system.
这非常依赖于操作系统。
now in case of user level threads Is it that this TCB is created in user's address space ?
现在在用户级线程的情况下,这个 TCB 是在用户地址空间中创建的吗?
The TCB records information about a thread that the kernel uses in running that thread, right? So if it were allocated in user space, the user mode thread could modify or corrupt it, which doesn't seem like a very good idea. So, don't you suppose it's created in kernel space?
TCB 记录有关内核在运行该线程时使用的线程的信息,对吗?因此,如果它是在用户空间中分配的,则用户模式线程可能会修改或破坏它,这似乎不是一个好主意。所以,你不认为它是在内核空间中创建的吗?
What are these models? How are these models practically used?
这些模型是什么?这些模型如何实际使用?
Wikipediaseems really clear about that.
维基百科似乎对此非常清楚。
回答by EmeryBerger
Wikipedia has answers to most if not all of these questions.
维基百科对大多数(如果不是全部)这些问题都有答案。
http://en.wikipedia.org/wiki/Thread_(computer_science)
回答by MaHuJa
Kernel thread means a thread that the kernel is responsible for scheduling. This means, among other things, that the kernel is able to schedule each thread on different cpus/cores at the same time.
内核线程是指内核负责调度的线程。这意味着内核能够同时在不同的 CPU/内核上调度每个线程。
How to use them varies a lot with programming languages and threading APIs, but as a simple illustration,
如何使用它们因编程语言和线程 API 而异,但作为一个简单的说明,
void task_a();
void task_b();
int main() {
new_thread(task_a);
new_thread(task_b);
// possibly do something else in the main thread
// wait for the threads to complete their work
}
In every implementation I am familiar with, the kernel may pause them at any time. ("pre-emptive")
在我熟悉的每个实现中,内核可能随时暂停它们。(“先发制人”)
User threads, or "User scheduled threads", make the program itself responsible for switching between them. There are many ways of doing this and correspondingly there is a variety of names for them.
用户线程或“用户调度线程”使程序本身负责在它们之间进行切换。有很多方法可以做到这一点,相应地,它们有多种名称。
On one end you have "Green threads"; basically trying to do the same thing as kernel threads do. Thus you keep all the complications of programming with real threads.
一方面,你有“绿线”;基本上试图做与内核线程相同的事情。因此,您可以保留使用真实线程进行编程的所有复杂性。
On the opposite end, you have "Fibers", which are required to yield before any other fiber gets run. This means
在另一端,您有“纤维”,它需要在任何其他纤维运行之前屈服。这意味着
- The fibers are run sequentially. There is no parallell performance gains to be had.
- The interactions between fibers is very well defined. Other code run only at the exact points you yield. Other code won't be changing variables while you're working on them.
- Most of the low-level complexities programmers struggle with in multithreading, such as cache coherency (looking at MT questions on this site, most people don't get that), are not a factor.
- 纤维按顺序运行。没有并行的性能提升。
- 纤维之间的相互作用非常明确。其他代码仅在您产生的确切点上运行。其他代码在您处理变量时不会更改变量。
- 大多数程序员在多线程中挣扎的低级复杂性,例如缓存一致性(查看本网站上的 MT 问题,大多数人不明白)不是一个因素。
As the simplest example of fibers I can think of:
作为我能想到的最简单的纤维示例:
while(tasks_not_done) {
do_part_of_a();
do_part_of_b();
}
where each does some work, then returns when that part is done. Note that these are done sequentially in the same "hardware thread" meaning you do not get a performance increase from parallellism. On the other hand, interactions between them are very well defined, so you don't have race conditions. The actual working of each function can vary. They could also be "user thread objects" from some vector/array.
每个人都做一些工作,然后在该部分完成后返回。请注意,这些是在同一个“硬件线程”中按顺序完成的,这意味着您不会从并行性中获得性能提升。另一方面,它们之间的交互非常明确,因此您没有竞争条件。每个功能的实际工作可能会有所不同。它们也可以是来自某个向量/数组的“用户线程对象”。
回答by LordDoskias
Essentially user threads run in the context of a user with the appropriate privilege levels e.g. user threads most certainly won't have access to kernel-level memory/data structures/routines etc. Whereas Kernel threads run in the context of the OS kernel thus giving them privileges to execute code which has access to low level kernel routines/memory/data structures.
本质上,用户线程在具有适当权限级别的用户上下文中运行,例如用户线程肯定不会访问内核级内存/数据结构/例程等。而内核线程在操作系统内核的上下文中运行,因此给出他们有权执行可以访问低级内核例程/内存/数据结构的代码。