简体   繁体   English

一对一多线程模型

[英]one-to-one multi-threading model

In silberschatz "Operating System Concepts" book, section 4.3.2 says that在 silberschatz 的“操作系统概念”一书中,第 4.3.2 节说

one-to-one model provides more concurrency than the many-to-one model by allowing another thread to run when a thread makes a blocking system call.一对一模型通过在一个线程进行阻塞系统调用时允许另一个线程运行来提供比多对一模型更多的并发性。 It also allows multiple threads to run in parallel on multiprocessors.它还允许多个线程在多处理器上并行运行。

I have two questions here:我在这里有两个问题:

  1. How can one thread be blocked and other mapped on kernel thread?如何阻塞一个线程并将其他线程映射到内核线程? Dont we know that if one thread is blocked, entire process of that user-level thread is blocked?难道我们不知道如果一个线程被阻塞,那么该用户级线程的整个进程都被阻塞了吗?
  2. The OS considers user-level threads as one thread only.操作系统仅将用户级线程视为一个线程。 It cant be assigned to multiple processors/cores.它不能分配给多个处理器/内核。 Isn't the below given line contradicting that idea?下面给出的行是否与该想法相矛盾?

    It also allows multiple threads to run in parallel on multiprocessors它还允许多个线程在多处理器上并行运行

Your understanding of user level threads and kernel level threads is not correct, in particular you need to understand how user level threads are mapped to kernel level threads.您对用户级线程和内核级线程的理解不正确,特别是您需要了解用户级线程如何映射到内核级线程。 So first lets define some terms所以首先让我们定义一些术语

Kernel thread内核线程

A thread ( schedulable task ) that is created and managed by the kernel.由内核创建和管理的线程(可调度任务)。 Every kernel level thread is represented by some data structure which contains information related to the thread.每个内核级线程都由一些数据结构表示,其中包含与线程相关的信息。 In the case of Linux it is task_struct .对于 Linux,它是task_struct Kernel threads are the only threads that are considered by the CPU scheduler for scheduling.内核线程是 CPU 调度程序考虑进行调度的唯一线程。

Note: Kernel thread is a bit of misnomer as Linux kernel doesn't distinguish between a thread and a process, schedulable task would better describe this entity .注意:内核线程有点用词不当,因为 Linux 内核不区分线程和进程,可调度任务最好描述这个实体

User thread用户线程

A thread that is created and managed by some library such as JVM above kernel level.由某些库(例如内核级别的 JVM)创建和管理的线程。 The library that creates these threads is responsible for their management that is which thread runs and when.创建这些线程的库负责它们的管理,即哪个线程运行以及何时运行。

User level to kernel level mapping用户级到内核级的映射

Now you can create as many user level threads as you want but to execute them you need to create some kernel level thread ( task_struct ).现在您可以根据需要创建任意数量的用户级线程,但要执行它们,您需要创建一些内核级线程 ( task_struct )。 This creation of kernel level threads can be done in many ways这种内核级线程的创建可以通过多种方式完成

One to one model一对一模型

In this case whenever you create a user level thread your library asks the kernel to create a new kernel level thread.在这种情况下,每当您创建用户级线程时,您的库都会要求内核创建一个新的内核级线程。 In the case of Linux your library will use clone system call to create a kernel level thread.对于 Linux,您的库将使用克隆系统调用来创建内核级线程。

Many to one model多对一模型

In this case your library creates only one kernel level thread ( task_struct ).在这种情况下,您的库仅创建一个内核级线程 ( task_struct )。 No matter how many user level threads you create they all share the same kernel level thread, much like the processes running on a single core CPU.无论您创建多少用户级线程,它们都共享同一个内核级线程,就像在单核 CPU 上运行的进程一样。 The point to understand is that your library here acts much like the CPU scheduler, it schedules many user level threads on single kernel level thread.要理解的一点是,您的库在这里的行为很像 CPU 调度程序,它在单个内核级线程上调度许多用户级线程。

Now to your question现在回答你的问题

The OS considers user-level threads as one thread only.操作系统仅将用户级线程视为一个线程。 It can't be assigned to multiple processors/cores.它不能分配给多个处理器/内核。 Isn't the below given line contradicting that idea?下面给出的行是否与该想法相矛盾?

If you were using many to one model, in that case you will have only one kernel level thread for all of your user level threads and hence they cannot run on different CPU's.如果您使用多对一模型,那么在这种情况下您将只有一个内核级线程用于所有用户级线程,因此它们不能在不同的 CPU 上运行。

But if you are using a one to one model then each of your user level threads has a corresponding kernel level thread that can be scheduled individually and hence user level threads can run on different CPU's given that you have more than one CPU.但是如果你使用的是一对一模型,那么你的每个用户级线程都有一个相应的内核级线程,可以单独调度,因此用户级线程可以在不同的 CPU 上运行,因为你有多个 CPU。

You are suffering from a confusing book.你正在为一本令人困惑的书而苦恼。

There are real threads (aka kernel threads, 1 to 1 model) and there are simulated threads (aka user threads, many to 1 model).有真实线程(也称为内核线程,一对一模型)和模拟线程(也称为用户线程,多对一模型)。

Some books make this more confusing by throwing a hypothetical many to many model.有些书通过抛出一个假设的多对多模型使这更加混乱。

User threads are obsolete.用户线程已过时。 Any operating system book worth reading these days would treat them that way and describe them in historical terms.如今,任何值得一读的操作系统书籍都会以这种方式对待它们,并以历史术语来描述它们。

How can one thread be blocked and other mapped on kernel thread?如何阻塞一个线程并将其他线程映射到内核线程? Dont we know that if one thread is blocked, entire process of that user-level thread is blocked?难道我们不知道如果一个线程被阻塞,那么该用户级线程的整个进程都被阻塞了吗?

You either have user threads or kernel thread.您要么有用户线程,要么有内核线程。 An application that did both would be royally screwed up.两者兼有的应用程序将被彻底搞砸。

The OS considers user-level threads as one thread only.操作系统仅将用户级线程视为一个线程。 It cant be assigned to multiple processors/cores.它不能分配给多个处理器/内核。 Isn't the below given line contradicting that idea?下面给出的行是否与该想法相矛盾?

In ye olde days a process was considered to be an execution stream and an address space.在过去,一个进程被认为是一个执行流和一个地址空间。 There were no threads.没有线程。 When threads became necessary (largely due to the need for Ada support), they were simulated using timers.当线程变得必要时(主要是由于需要 Ada 支持),它们被使用计时器模拟。 The behavior of threads varied by operating system.线程的行为因操作系统而异。

In Eunuchs variants, blocking calls block the process entirely.在 Eunuchs 变体中,阻塞调用会完全阻塞进程。 Thus in simulated (user) threads a blocking call in one thread would block all threads.因此,在模拟(用户)线程中,一个线程中的阻塞调用将阻塞所有线程。 This is not true on all operating systems.并非所有操作系统都如此。

Now, a process is one or more execution streams and an address space.现在,一个进程是一个或多个执行流和一个地址空间。 That is what you ought be learning;那是你应该学习的; not a bunch of technobabble.不是一堆技术废话。

A book that talks about threads in terms of 1-to-1 or many-to-1 models is only fit to line cat boxes.以 1 对 1 或多对 1 模型谈论线程的书只适用于 line cat boxes。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM