调度员如何工作?
我最近开始了我的操作系统课程。据我所知,调度程序的工作是保存当前进程的上下文并加载下一步要运行的进程的上下文。但它是如何做到这一点的呢?当一个进程被抢占时,一旦调度程序被加载并执行(因为它也是一个程序),寄存器、PSW 等中先前进程的上下文就会丢失。在加载自身之前如何保存上下文?
I have recently started my OS course. As far as i know the work of dispatcher is to save the context of current process and load context of process to be run next. But how does it do that? When a process is preempted then as soon as dispatcher will be loaded and executed ( as it is also a program ) the context of previous process in registers, PSW etc will be lost. How is it going to save the context before loading itself ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的答案是,现代处理器提供了架构扩展,提供了可以在硬件中交换的多个寄存器组,因此最多 X 个任务可以保留其完整的寄存器集。
更复杂的答案是,调度程序在被中断触发时,会接收中断时正在运行的程序的完整寄存器集(程序计数器除外,它可能是通过双方同意的协议传播的)在“易失性”寄存器或类似的寄存器上)。因此,必须仔细编写调度程序,以将寄存器组的当前状态存储为触发后的第一个操作。简而言之,调度程序本身没有直接上下文,因此不会遇到同样的问题。
下面尝试对调度程序调用期间发生的情况进行简单描述:
总结(过度)简化;调度程序不需要寄存器,它所做的只是将当前 cpu 状态写入预定的内存位置,从预定的内存位置加载另一个进程的 cpu 状态,然后跳转到该进程停止的位置。
这样是不是更清楚了?
The simple answer is that modern processors offer architectural extensions providing for several banks of registers that can be swapped in hardware, so up to X tasks get to retain their full set of registers.
The more complex answer is that the dispatcher, when triggered by an interrupt, receives the full register set of the program that was running at the time of interrupt (with the exception of the program counter, which is presumably propagated through a mutually-agreed-upon 'volatile' register or some such). Thus, the dispatcher must be carefully written to store the current state of register banks as its first operation upon being triggered. In short, the dispatcher itself has no immediate context and thus doesn't suffer from the same problem.
Here is an attempt at a simple description of what goes on during the dispatcher call:
To (over)simplify in summary; the dispatcher doesn't need registers, all it does is write the current cpu state to a predetermined memory location, load another processes' cpu state from a predetermined memory location, and jumps to where that process left off.
Does that make it any clearer?
它的加载方式通常不会导致您丢失当前进程的信息。
通常,它是在当前进程的上下文中发生的中断。
因此,调度程序(或调度程序)可以将所有相关信息保存在某种任务控制块中,然后再为下一个进程加载该信息。
这包括寄存器内容、堆栈指针等。
值得注意的是,下一个进程的上下文包括其在调度程序中断本身中的状态,因此,当它从中断返回时,它会进入一个完全不同的进程。
It doesn't generally get loaded in such a way that you lose the information on the current process.
Often, it's an interrupt that happens in the context of the current process.
So the dispatcher (or scheduler) can save all relevant information in a task control block of some sort before loading up that information for the next process.
This includes the register contents, stack pointer and so on.
It's worth noting that the context of the next process includes its state of being in the dispatcher interrupt itself so that, when it returns from the interrupt, it's to a totally different process.
调度程序模块将CPU的控制权交给短期调度程序选择的进程;这涉及:
切换上下文,
切换到用户模式,
跳转到用户程序中的正确位置以重新启动该程序
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:
switching context,
switching to user mode,
jumping to the proper location in the user program to restart that program
操作系统的主要职责是控制进程的执行。这包括确定执行模式以及为进程分配资源。
进程可能处于两种状态之一:
当操作系统创建新进程时,它会为该进程创建一个进程控制块,并将该进程进入系统进入未运行状态。操作系统已知该进程的存在,并且正在等待执行机会。
有时,当前正在运行的进程会被中断,操作系统的调度程序部分将选择一些其他进程来运行。
在执行过程中,当进程缺乏资源时,它会被阻塞。提供这些资源后,它会重新进入就绪状态,然后进入运行状态。从就绪状态到运行状态的转换是由调度程序完成的。调度程序调度进程。
The operating system's principal responsibility is controlling the execution of processes. This includes determining the pattern for execution and allocating resources to the processes.
A process may be in one of the two states :
When the OS creates a new process, it creates a process control block for the process and enters that process into the system into the Not Running state. The process exists is known to OS and is waiting for an opportunity to execute.
From time to time, the currently running processes will be interrupted and the dispatcher portion of the OS will select some other processes to run.
During execution when the process is devoid of resources, it gets blocked. Provided those resources it re-enters the ready state and then into running state. This transition from ready to running state is done by dispatcher. Dispatcher dispatches the process.