低级编程:操作系统如何启动新线程/进程?
每当引导加载程序加载操作系统时,大概只有一个程序流处于活动状态,对吗? 这意味着,一个处理器持有指令指针并执行它在 EIP 寄存器指向的位置找到的命令。 系统何时以及如何开始利用更多进程和/或线程(没有用户态线程,而是 cpu 线程)?
Whenever the bootloader loads the operating system there is presumably only ONE program flow active, right? This would mean, one processor holds the instruction pointer and executes the commands it founds at the position the EIP register points to. At which point and how does the system start to exploit more processes and/or threads (no userland threads, but cpu threads)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
操作系统将以特殊角色启动(BIOS 和引导加载程序完成后) - 作为第一个运行的程序,它将可以直接访问所有 CPU 命令。
因此它将设置系统的各个部分 - 例如设置中断处理程序(或中断服务例程) 。 完成此操作后,它就有能力创建“调度程序”。
实际的“进程/线程”处理将由该调度程序完成。 它决定将运行哪些线程。 它还管理所有活动线程。 CPU 不知道所有这些事情。
一旦调度程序的主执行程序决定执行线程(或“进程”)A,它将进程数据复制到寄存器中(并将寄存器存储到最近运行的线程的信息块中)。 它将告诉CPU/定时器在n微秒(或其他时间单位)内引发中断。 然后它会告诉CPU在非操作系统模式下运行“程序”(CPU唯一知道的事情)(这样它就不会在未经许可的情况下修改关键数据或注册自己的中断处理程序)。
当线程 A 现在正在执行时,硬件计时器将运行。 一旦达到所需的时间偏移,就会引发中断。 然后硬件将停止当前程序的执行,并调用注册的中断处理程序。 该处理程序将是调度程序(准确地说,又是主执行程序)的一个方法。
然后,此方法将再次重新评估应调度哪个线程,以便调度继续。
The OS will boot (after the BIOS and the bootloader are done) in a special role - as the first program to run it will have direct access to all the CPUs commands.
So it will setup various parts of the system - like setting up Interrupt Handlers (or Interrupt Service Routines). Having done this it has the ability to create a "scheduler".
The actual "process/thread" handling will be done by this scheduler. It decides, which threads will be run. Also it manages all the active threads. The CPU is unaware of all these things.
Once the scheduler's main-executive decides to execute Thread (or "Process") A, it copys the processes data into the registers (and stores the registers into the recently running thread's InfoBlock). It will tell the CPU / a timer to cause an interrupt in n microseconds (or other timeunit). Then it will tell the cpu to run the "program" (the only thing the CPU knows about) in the non-OS mode (so that it may not modify critical data or register own Interrupt Handlers without permission).
While Thread A is executing now, the hardware timer will run. Once it hits the desired time-offset, it will cause an Interrupt. The hardware will then stop execution of the current program, and will invoke the registred
Interrupt Handler
instead which. This handler will be a method of the scheduler (the main-executive again, to be precise).This method will then again reevaluate which Thread should be scheduled and so the scheduling continues.
正确,在启动过程中只有一个执行线程。 通常情况是这样,直到操作系统初始化到低级内存管理、调度程序等功能正常为止。
甚至在多 CPU 系统中也是如此 - 一个核心是处理早期启动的“主处理器”,直到基础设施启动其他核心为止。
最后,它是高度特定于操作系统的; 英特尔架构软件开发人员手册包含硬件规格的详细信息。 (假设您正在谈论英特尔架构;其他架构可能会有很大差异。)
Correct, during the boot process there is only one execution thread. Usually this is the case until the OS has initialized to the point where low-level memory management, scheduler etc. are functional.
This is even the case in multi-CPU systems - one core is the "master processor" handling the early startup until the infrastructure is there to kickstart the other cores.
In the end it's highly OS-specific; the Intel Architecture Software Developer's Manuals have the details of the hardware specs. (Assuming you're talking about Intel architecture; other architectures might differ wildly.)
(多线程)操作系统必须启动的第一件事就是调度程序,它负责管理多个进程(因此也管理多个 CPU 线程,例如在多核机器上)。
由该调度程序启动的第一个进程通常是某种“init”进程,该进程随后负责加载其他程序/进程。
One of the first things a (multithreaded) OS has to start is the scheduler which is responsible for managing multiple processes (and therefore also manages multiple CPU threads eg. on multicore machines).
The first process started by this scheduler is typically some sort of "init" process which in turn is responsible for loading the other programs/processes subsequently.