OS调度程序如何重新获得对CPU的控制? [英] How does the OS scheduler regain control of CPU?
问题描述
我最近开始学习CPU和操作系统的工作方式,并且对单CPU机器与提供多任务处理的操作系统的操作感到有些困惑.
I recently started to learn how the CPU and the operating system works, and I am bit confused about the operation of a single-CPU machine with an operating system that provides multitasking.
这样,假设我的机器只有一个CPU,这意味着在任何给定时间只能运行一个进程.
As such, supposing my machine has a single CPU, this would mean that, at any given time, only one process could be running.
现在,我只能假设操作系统用来控制对宝贵CPU时间的访问的调度程序也是一个过程.
Now, I can only assume that the scheduler used by the operating system to control the access to the precious CPU time is also a process.
因此,在此计算机上,用户进程或调度系统进程在任何给定时间点都在运行,但不是两者都在运行.
Thus, in this machine, either the user process or the scheduling system process is running at any given point in time, but not both.
所以这是一个问题:
一旦调度程序将CPU的控制权交给了另一个进程,它如何重新获得CPU时间来再次运行自身以进行调度工作?我的意思是,如果当前正在运行的任何给定进程没有放弃(让步)CPU,那么调度程序本身又将如何再次运行并确保适当的多任务处理?
Once the scheduler gives up control of the CPU to another process, how can it regain CPU time to run itself again to do its scheduling work? I mean, if any given process currently running does not relinquish (yield) the CPU, how could the scheduler itself ever run again and ensure proper multitasking?
到目前为止,我一直在想,如果用户进程通过系统调用请求I/O操作,那么在系统调用中,我们可以确保调度程序再次分配了一些CPU时间.但是我什至不确定这是否可以通过这种方式进行.
So far, I had been thinking, well, if the user process requests an I/O operation through a system call, then in the system call we could ensure the scheduler is allocated some CPU time again. But I am not even sure if this works in this way.
另一方面,如果所讨论的用户进程本质上是受CPU限制的,那么从这个角度来看,它可以永远运行,从不让其他进程运行,甚至调度程序也无法再次运行.
On the other hand, if the user process in question were inherently CPU-bound, then, from this point of view, it could run forever, never letting other processes, not even the scheduler run again.
假设按时间分段进行调度,我不知道调度程序如何在甚至没有运行的情况下为执行另一个进程削减时间?
Supposing a time-sliced scheduling, I have no idea how the scheduler could slice the time for the execution of another process, when it is not even running?
我真的很感谢您在这方面可以提供的任何见识或参考.
I would really appreciate any insight or references that you can provide in this regard.
推荐答案
操作系统设置了硬件计时器(可编程间隔计时器或PIT),每N毫秒生成一次中断.该中断被传递到内核,并且用户代码被中断.
The OS sets up a hardware timer (Programmable interval timer or PIT) that generates an interrupt every N milliseconds. That interrupt is delivered to the kernel and user-code is interrupted.
它的工作原理与其他任何硬件中断一样.例如,您的磁盘在完成IO后将强制切换到内核.
It works like any other hardware interrupt. For example your disk will force a switch to the kernel when it has completed an IO.
这篇关于OS调度程序如何重新获得对CPU的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!