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

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

问题描述

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

这是1-1型

这是N-1型.

这是混合模式.

关于内核线程,我在SO上遇到了很多问题. 链接,以进行澄清.

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

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

a=a+2;//a is my local variable in a user level C function

我的问题:

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

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

2) 内核线程实际上是由OS调度程序调度的,而不是由用户线程调度的吗?

解决方案

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

N-1模型显示了内核完全不知道的多个用户态线程,因为后者调度了进程(或在图中如何调用它,单个内核线程).因此对于内核来说,每个进程都是一个内核线程.当为进程分配了一部分处理器时间时,它会自行决定是否调度多个线程来运行多个线程.

1-1模型中,内核知道用户界面线程,并且调度程序将每个线程视为处理器时间分配对象.因此,内核无需调度整个进程,而是在进程内部的线程之间切换.

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

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

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

This is 1-1 model

This is N-1 model.

This is Hybrid model.

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

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 is my local variable in a user level C function

My question:

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) 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.

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.

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.

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.

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

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