允许调度程序切换正在执行哪些线程的机制是什么? [英] What is the mechanism that allows the scheduler to switch which threads are executing?

查看:131
本文介绍了允许调度程序切换正在执行哪些线程的机制是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道cpu调度程序使用时间片,并在指定的时间段内运行线程,然后切换线程,但是我不了解CPU是如何知道停止执行线程并切换任务的.每条指令执行完后都无法重复检查时钟吗?那将花费太多的开销,这将是非常浪费的.我不认为它在线程上使用一些确定性的计算来在某个指令上放置一个中断,假设该指令在到达该指令时已经过去了,那么如何进行上下文切换呢?在不经常检查时钟或其他东西的情况下,CPU如何停止执行?

I understand that the cpu scheduler uses time slices and has a thread run for a specified amount of time and then switches threads, but what I don't understand is how the CPU knows to stop executing a thread and switch tasks. It can't check a clock repeatedly after every instruction right? That would take so much overhead it would be very wasteful. I don't think it uses some deterministic calculations on the thread to place an interrupt at some instruction where it assumes the time will have elapsed by the time it reaches that instruction, so then how does the context switch take place? How does the CPU stop execution without constantly checking a clock or something?

推荐答案

这是计时器中断.

中断是图灵完备之外的事情之一.图灵完备的机器/语言不需要实现中断.但是,如果没有中断,您将很难实现时间分段或抢先式多任务操作系统.

Interrupts are one of those things that are beyond Turing-complete. A Turing-complete machine/language does not need to implement interrupts. But without interrupts you will have a hard time implementing a time-sliced or preemptive multitasking OS.

仍然可以在不中断的系统上实现多任务OS-使用协作多任务.旧的MacOS(在OSX之前)就是这样做的.您只是告诉应用程序开发人员,他们不时地必须通过调用yield()之类的东西将执行移交给操作系统.该产量"功能实际上是操作系统本身.协作式多任务处理当然不是理想的-您可以想象程序崩溃将导致OS永远无法执行,从而导致整个计算机瘫痪.协作式多任务处理的优点是,您可以在没有中断支持的非常简单的CPU上执行此操作.通常,它还需要少得多的RAM和CPU资源.而且,作为程序员,您可以对CPU进行更多控制-例如,如果您需要真正使用100%的CPU,则可以防止操作系统占用任何CPU时间.

It is still possible to implement a multitasking OS on a system without interrupts - you use Cooperative Multitasking. The old MacOS (before OSX) did just this. You just tell app developers that every now and then they must hand over execution to the OS by calling something like yield(). This "yield" function is actually the OS itself. Cooperative multitasking is not ideal of course - you can imagine a program crashing will cause the OS to never execute thus take down the entire machine. The advantage of cooperative multitasking is that you can do it on a very simple CPU with no interrupt support. It also typically requires a lot less RAM and CPU resources. And, as a programmer you have more control over the CPU - for example if you need to really use 100% of the CPU you can prevent the OS from taking any CPU time.

中断只是CPU的一项功能,您可以将CPU配置为在发生某些情况时调用一个函数(称为中断处理程序).对于OS程序员,最有用的是计时器中断-您可以在其中配置计时器以触发中断.触发中断后,操作系统便开始运行,并且在执行结束时,操作系统将简单地安排另一个计时器中断-这是按时间分割的多任务处理.

An interrupt is just a feature of the CPU where you can configure the CPU to call a function (called the interrupt handler) when something happens. For OS programmers, the most useful is the timer interrupt - where you can configure a timer to trigger an interrupt. The OS gets to run when the interrupt is triggered and at the end of execution the OS will simply schedule another timer interrupt - this is time-sliced multitasking.

某些操作系统(例如Linux或实时Windows)允许您配置此计时器. Linux将其称为jiffy,某些操作系统将其称为tick.

Some OSes like Linux or Real-time Windows allow you to configure this timer. Linux call it jiffy, some OSes call it tick.

如果您已完成任何javascript编程,这会让您感到熟悉-这几乎就像setTimeout()对程序员的表现.

If you have done any javascript programming this will feel familiar to you - this is almost like how setTimeout() appears to behave to a programmer.

另一种重要的中断类型是I/O中断.您的键盘实际上与I/O中断一起使用.普通PC根本不扫描键盘.取而代之的是I/O控制器(通常是USB控制器)查询键盘,如果有按键,则会向CPU发送中断信号.这将触发操作系统,并且操作系统将检查该密钥属于哪个进程,并将切换到该进程以允许其接收输入-这是抢先式多任务处理.显然,在长时间没有I/O活动的情况下,抢先式多任务处理会在后台使用时间切片器.

Another important type of interrupt is I/O interrupt. Your keyboard actually works with an I/O interrupt. A normal PC does not scan the keyboard at all. Instead the I/O controller (these days it's usually the USB controller) queries the keyboard and if there is a key press will send an interrupt signal to the CPU. This will trigger the OS and the OS will check which process the key belongs to and will switch to the process to allow it to receive the input - this is preemptive multitasking. Obviously, preemptive multitasking uses a time-slicer in the background in case there are long periods of no I/O activity.

这篇关于允许调度程序切换正在执行哪些线程的机制是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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