线程:为什么必须将所有用户线程映射到内核线程? [英] Threads: Why must all user threads be mapped to a kernel thread?

查看:2365
本文介绍了线程:为什么必须将所有用户线程映射到内核线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有两个问题。首先,(是的,我已经搜索了,但想要澄清),用户线程和内核线程之间的区别是什么?它只是一个由用户程序生成的,另一个由操作系统生成,后者可以访问特权指令?他们在概念上是一样的还是在线程本身有实际的差异?



第二,我的问题的真正的问题是:我使用的书说关系必须存在于用户线程和内核线程之间,接下来列出这种关系的不同模型。但该书未能清楚地解释为什么用户线程必须始终映射到特定的内核线程。为什么是这样?

解决方案

内核线程是由操作系统维护的线程对象。它是能够由处理器调度和执行的实际线程。通常,系统线程是具有权限设置,优先级等的重量级对象。内核线程调度程序负责调度内核线程。用户程序可以使他们自己的线程调度程序太。他们可以制作自己的线程,并模拟上下文切换以在它们之间切换。但是,这些线程不是内核线程。每个用户线程实际上不能独立运行,用户线程运行的唯一方法是如果内核线程实际上被告知要执行用户线程中包含的代码。也就是说,用户线程比内核线程有更大的优势。他们可以更轻量级,因为他们不一定需要有自己的优先级,可以通过单个进程(可能有更好的信息,有关什么线程需要运行),并且不创建大量的内核对象用于安全和锁定。



用户线程必须与内核线程相关联的原因是,用户线程本身只是一堆数据用户程序。内核线程是系统中的真正线程,因此对于用户线程来说,用户程序必须让它的调度器采用用户线程,然后在内核线程上运行它。用户线程和内核线程之间的映射不必是一对一(1:1)映射;你可以有多个用户线程共享相同的内核线程(每次只运行其中一个用户线程),并且你可以有一个单独的用户线程在不同的内核线程(1:n)映射之间循环。


So two questions here really. First, (and yes, I have searched this already, but wanted clarification), what is the difference between a user thread and a kernel thread? Is it simply that one is generated by a user program and the other by an OS, with the latter having access to privileged instructions? Are they conceptually the same or are there actual differences in the threads themselves?

Second, and the real problem of my question is: the book I am using says that "a relationship must exist between user threads and kernel threads," going on to list the different models of such a relationship. But the book fails to clearly explain why a user thread must always be mapped to a specific kernel thread. Why is this?

解决方案

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. Typically, system threads are heavyweight objects with permissions settings, priorities, etc. The kernel thread scheduler is in charge of scheduling kernel threads.

User programs can make their own thread schedulers too. They can make their own "threads" and simulate context-switches to switch between them. However, these threads aren't kernel threads. 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. That said, user threads have major advantages over kernel threads. They can be a lot more lightweight, since they don't necessarily need to have their own priorities, can be managed by a single process (which might have better info about what threads need to run when), and don't create lots of kernel objects for purposes of security and locking.

The reason that user threads have to be associated with kernel threads is that by itself a user thread is just a bunch of data in a user program. Kernel threads are the real threads in the system, so for a user thread to make progress the user program has to have its scheduler take a user thread and then run it on a kernel thread. The mapping between user threads and kernel threads doesn't have to be one-to-one (1 : 1) mapping ; you can have multiple user threads share the same kernel thread (only one of those user threads runs at a time), and you can have a single user thread which is rotated across different kernel threads (1 : n) mapping.

这篇关于线程:为什么必须将所有用户线程映射到内核线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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