操作系统调度程序如何重新获得对 CPU 的控制权? [英] How does the OS scheduler regain control of CPU?

查看:18
本文介绍了操作系统调度程序如何重新获得对 CPU 的控制权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习 CPU 和操作系统是如何工作的,对于一个提供多任务处理的操作系统的单 CPU 机器的操作,我有点困惑.

I recently started to learn how the CPU and the operating system works, and I am a bit confused about the operation of a single-CPU machine with an operating system that provides multitasking.

假设我的机器只有一个 CPU,这意味着在任何给定时间,只能运行一个进程.

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

这篇关于操作系统调度程序如何重新获得对 CPU 的控制权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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