为什么操作系统需要/维护内核线程? [英] Why does OS require/maintain kernel-land threads?

查看:22
本文介绍了为什么操作系统需要/维护内核线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我遇到的三个线程模型.基于以下 3 种架构,我很新鲜地了解到,除了作为 POSIX.1C 的一部分引入的用户线程之外,还存在称为内核线程的东西

Below are three threading models that i came across. Based on these below 3 architectures, It is new for me to understand that, there also exist something called kernel thread, apart from user thread which is introduced as part of POSIX.1C

这是 1-1 模型

这是 N-1 模型.

这是混合模型.

我已经解决了许多关于内核线程 SO 的问题.这看起来更相关链接以供澄清.

I have been through many questions on SO for kernel threads. This looks more relevant link for clarification.

在进程级别,对于Linux loader(例如)加载的每个用户进程,内核不会分配相应的内核进程来执行用户进程提出的机器指令.用户进程只请求内核模式执行,当它需要来自内核模块的工具[如 malloc()/fork()] 时.用户进程的调度由操作系统调度器完成并分配一个 CPU 内核.

At process level, For every user process that is loaded by Linux loader(say), Kernel does not allocate corresponding kernel process for executing machine instructions that a user process has come up with. User process only request for kernel mode execution, when it require a facility from kernel module[like malloc()/fork()]. Scheduling of user process is done by OS scheduler and assign a CPU core.

例如,用户进程不需要内核执行模式来执行指令

For example, User process does not require kernel execution mode to execute an instruction

a=a+2;//a 是我在用户级 C 函数中的局部变量

我的问题:

1)那么,内核级线程的目的是什么?为什么操作系统需要为用户级进程的相应用户线程维护一个内核线程(另外)?用户模式程序员是否可以通过编程为给定的用户进程选择上述三种线程模型中的任何一种?

1) So, What is the purpose of kernel level thread? Why does OS need to maintain a kernel thread(additionally) for corresponding user thread of a User level process? Does User mode programmer have any control on choosing any of the above three threading models for a given User process through programming?

在我理解了第一个问题的答案后,一个相关的补充是,

After i understand the answer to first question, one relevant supplementary is,

2)内核线程是否真的由操作系统调度程序而不是用户线程调度?

2) Does kernel thread actually get scheduled by OS scheduler but not user thread?

推荐答案

我认为在这些数字中使用内核线程这个词有点误导.我知道一本关于操作系统(设计)的书中的数字,如果我没记错的话,它们指的是操作系统如何安排工作的方式.在图中,每个进程至少分配了一个内核线程,由内核调度.

I think the use of the word kernel thread is a bit misleading in these figures. I know the figures from a book about operating system (design) and if I remember correctly, they refer to the way how work is scheduled by the operating system. In the figures, each process has at least one kernel thread assigned that is scheduled by the kernel.

N-1 模型显示了内核根本不知道的多个用户级线程,因为后者调度进程(或图中的调用方式,单个内核线程).所以对于内核来说,每个进程都是一个内核线程.当进程被分配一块处理器时间时,它自己会通过自行调度来运行多个线程.

The N-1 model shows multiple user-land threads that are not known to the kernel at all because the latter schedules the process (or how it's called in the figure, a single kernel thread) only. So for the kernel, each process is a kernel thread. When the process is assigned a slice of processor time, it itself runs multiple threads by scheduling them at its own discretion.

1-1 模型中,内核知道用户级线程,并且调度程序会考虑每个线程进行处理器时间分配.因此,内核不是调度整个进程,而是在进程内的线程之间切换.

In the 1-1 model, the kernel is aware of the user-land threads and each thread is considered for processor time assignment by the scheduler. So instead of scheduling a whole process, the kernel switches between threads inside of processes.

混合模型结合了这两个原则,其中轻量级进程实际上是内核已知的线程,并由内核调度执行.此外,它们实现了内核不知道的线程,并在用户态分配处理器时间.

The hybrid model combines both principles, where lightweight processes are actually threads known to the kernel and which are scheduled for execution by it. Additionally, they implement threads the kernel is not aware of and assign processor time in user-land.

现在完全混淆了,实际上有一个真正的内核线程 在 Linux 中.但据我了解这个概念,这些线程仅用于内核空间操作,例如当内核模块需要并行处理时.

And now to be completely confused, there is actually a real kernel thread in Linux. But as far as I understand the concept, these threads are used for kernel-space operations only, e.g. when kernel modules need to do things in parallel.

这篇关于为什么操作系统需要/维护内核线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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