调度程序如何结束正在运行的进程?
我刚刚意识到,在学习了很多关于各种调度算法、上下文切换是如何完成的等等之后,有一件事我仍然不清楚。
以单处理器系统为例:
如果进程 A 正在运行,并且它的时隙应在 5 秒内结束,那么调度程序或操作系统如何知道如何在 5 秒后结束它?当 A 运行时,操作系统的任何部分都无法运行。调度程序应该监控它,但如果它无法运行怎么办?操作系统的调度程序是否编写 ISR 并每 5 秒生成一个中断?这可能吗?即使是这样,实施起来似乎也不是一个好方法。
调度程序到底是如何做到这一点的?
I just realized that after learning a lot about various scheduling algorithms, how a context switch is done, etc. one thing still isn't clear to me.
Take a uniprocessor system:
If process A
is running and it's time slot should end in 5 seconds, how does the scheduler or the operating system know how to end it after 5 seconds? No part of the operating system can run while A is running. The scheduler is supposed to be monitoring it, but how can it if it cannot run? Does the operating system's scheduler write an ISR and have an interrupt generate every 5 seconds? Is this possible? Even if it is, it doesn't seem a good way to implement it.
How exactly does a scheduler do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这正是它在抢占式多任务系统上的工作方式(尽管在桌面系统上间隔通常更像是 10 毫秒)。
是的,还有其他方案,例如协作多任务处理,其中每个进程自行决定何时让步。
Yes, this is exactly how it works on a preemptive multitasking system (although on desktop systems the interval is usually more like 10 milliseconds).
Yes, there are other schemes, such as cooperative multitasking, where each process decides for itself when to yield.
是的,通常会触发某种计时器中断。然后内核可以运行一段时间并在需要时切换进程上下文 - 通常该中断会比每 5 秒触发一次要频繁得多。为什么这看起来不是一个实现它的好方法?
Yes, normally there is some kind of timer interrupt that fires. The kernel can then run for a bit and switch process context if it needs to - normally that interrupt would fire an awful lot more often than just once every 5 seconds though. Why doesn't it seem like a good way to implement it?