调度员如何工作?

发布于 2024-10-14 03:28:29 字数 147 浏览 2 评论 0原文

我最近开始了我的操作系统课程。据我所知,调度程序的工作是保存当前进程的上下文并加载下一步要运行的进程的上下文。但它是如何做到这一点的呢?当一个进程被抢占时,一旦调度程序被加载并执行(因为它也是一个程序),寄存器、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

强者自强 2024-10-21 03:28:29

简单的答案是,现代处理器提供了架构扩展,提供了可以在硬件中交换的多个寄存器组,因此最多 X 个任务可以保留其完整的寄存器集。

更复杂的答案是,调度程序在被中断触发时,会接收中断时正在运行的程序的完整寄存器集(程序计数器除外,它可能是通过双方同意的协议传播的)在“易失性”寄存器或类似的寄存器上)。因此,必须仔细编写调度程序,以将寄存器组的当前状态存储为触发后的第一个操作。简而言之,调度程序本身没有直接上下文,因此不会遇到同样的问题。

下面尝试对调度程序调用期间发生的情况进行简单描述:

  1. 当前具有上下文的程序正在处理器上运行。寄存器、程序计数器、标志、堆栈基址等都适合该程序;除了操作系统本机“保留寄存器”或类似的可能的例外之外,程序中的任何内容都不知道有关调度程序的任何信息。
  2. 触发调度程序功能的定时中断。此时(在普通架构情况下)发生的唯一事情是程序计数器立即跳转到 BIOS 中断中列出的 PC 地址。这将开始执行调度程序的“dispatch”子例程;其他一切都保持不变,因此调度程序可以看到先前执行的程序的寄存器、堆栈等。
  3. 调度程序(像所有程序一样)有一组对当前寄存器集进行操作的指令。这些指令的编写方式使得它们知道先前执行的应用程序已保留其所有状态。调度程序中的前几条指令会将这个状态存储在内存中的某个位置。
  4. 调度程序确定下一个需要 CPU 的程序应该是什么,获取所有先前存储的状态并用它填充寄存器。
  5. 调度程序跳转到任务中列出的适当的 PC 计数器,该任务现在已在 cpu 上建立了完整的上下文。

总结(过度)简化;调度程序不需要寄存器,它所做的只是将当前 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:

  1. The program that currently has context is running on the processor. Registers, program counter, flags, stack base, etc are all appropriate for this program; with the possible exception of an operating-system-native "reserved register" or some such, nothing about the program knows anything about the dispatcher.
  2. The timed interrupt for dispatcher function is triggered. The only thing that happens at this point (in the vanilla architecture case) is that the program counter jumps immediately to whatever the PC address in the BIOS interrupt is listed as. This begins execution of the dispatcher's "dispatch" subroutine; everything else is left untouched, so the dispatcher sees the registers, stack, etc of the program that was previously executing.
  3. The dispatcher (like all programs) has a set of instructions that operate on the current register set. These instructions are written in such a way that they know that the previously executing application has left all of its state behind. The first few instructions in the dispatcher will store this state in memory somewhere.
  4. The dispatcher determines what the next program to have the cpu should be, takes all of its previously stored state and fills registers with it.
  5. The dispatcher jumps to the appropriate PC counter as listed in the task that now has its full context established on the cpu.

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?

七色彩虹 2024-10-21 03:28:29

它的加载方式通常不会导致您丢失当前进程的信息。

通常,它是在当前进程的上下文中发生的中断。

因此,调度程序(或调度程序)可以将所有相关信息保存在某种任务控制块中,然后再为下一个进程加载该信息。

这包括寄存器内容、堆栈指针等。

值得注意的是,下一个进程的上下文包括其在调度程序中断本身中的状态,因此,当它从中断返回时,它会进入一个完全不同的进程。

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.

我做我的改变 2024-10-21 03:28:29

调度程序模块将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

痕至 2024-10-21 03:28:29

操作系统的主要职责是控制进程的执行。这包括确定执行模式以及为进程分配资源。

进程可能处于两种状态之一:

  1. 运行或未
  2. 运行

当操作系统创建新进程时,它会为该进程创建一个进程控制块,并将该进程进入系统进入未运行状态。操作系统已知该进程的存在,并且正在等待执行机会。

有时,当前正在运行的进程会被中断,操作系统的调度程序部分将选择一些其他进程来运行。

在执行过程中,当进程缺乏资源时,它会被阻塞。提供这些资源后,它会重新进入就绪状态,然后进入运行状态。从就绪状态到运行状态的转换是由调度程序完成的。调度程序调度进程。

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 :

  1. Running or
  2. Not Running

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文