低级编程:操作系统如何启动新的线程/进程?

时间:2021-08-27 00:25:17

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)?

每当引导程序加载操作系统时,可能只有一个程序流处于活动状态,对吧?这意味着,一个处理器保存指令指针并执行它在EIP寄存器指向的位置处发现的命令。此时,系统如何开始利用更多进程和/或线程(没有用户态线程,但是cpu线程)?

3 个解决方案

#1


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.

操作系统将以特殊角色启动(在BIOS和引导加载程序完成之后) - 因为运行它的第一个程序将直接访问所有CPU命令。

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.

实际的“进程/线程”处理将由此调度程序完成。它决定了哪些线程将被运行。它还管理所有活动线程。 CPU没有意识到所有这些事情。

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

一旦调度程序的主执行程序决定执行线程(或“进程”)A,它就会将进程数据复制到寄存器中(并将寄存器存储到最近运行的线程的InfoBlock中)。它会告诉CPU /定时器在n微秒(或其他时间单位)内引起中断。然后它将告诉cpu在非OS模式下运行“程序”(CPU唯一知道的事情)(这样它就不会在未经许可的情况下修改关键数据或注册自己的中断处理程序)。

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

当线程A正在执行时,硬件计时器将运行。一旦达到所需的时间偏移,就会导致中断。然后硬件将停止执行当前程序,并将调用注册的中断处理程序。这个处理程序将是调度程序的一个方法(确切地说是主执行程序)。

This method will then again reevaluate which Thread should be scheduled and so the scheduling continues.

然后,此方法将再次重新评估应调度哪个线程,以便继续调度。

#2


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.

正确,在引导过程中只有一个执行线程。通常情况是这样,直到OS初始化到低级别内存管理,调度程序等功能的程度。

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.

在多CP​​U系统中甚至就是这种情况 - 一个核心是处理早期启动的“主处理器”,直到基础设施开始启动其他核心。

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

最后,它是高度针对特定操作系统的;英特尔架构软件开发人员手册详细介绍了硬件规格。 (假设您正在谈论英特尔架构;其他架构可能会有很大不同。)

#3


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

(多线程)操作系统必须首先启动的是调度程序,它负责管理多个进程(因此也管理多个CPU线程,例如在多核机器上)。

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.

由该调度程序启动的第一个进程通常是某种“初始化”进程,该进程又负责随后加载其他程序/进程。

#1


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.

操作系统将以特殊角色启动(在BIOS和引导加载程序完成之后) - 因为运行它的第一个程序将直接访问所有CPU命令。

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.

实际的“进程/线程”处理将由此调度程序完成。它决定了哪些线程将被运行。它还管理所有活动线程。 CPU没有意识到所有这些事情。

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

一旦调度程序的主执行程序决定执行线程(或“进程”)A,它就会将进程数据复制到寄存器中(并将寄存器存储到最近运行的线程的InfoBlock中)。它会告诉CPU /定时器在n微秒(或其他时间单位)内引起中断。然后它将告诉cpu在非OS模式下运行“程序”(CPU唯一知道的事情)(这样它就不会在未经许可的情况下修改关键数据或注册自己的中断处理程序)。

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

当线程A正在执行时,硬件计时器将运行。一旦达到所需的时间偏移,就会导致中断。然后硬件将停止执行当前程序,并将调用注册的中断处理程序。这个处理程序将是调度程序的一个方法(确切地说是主执行程序)。

This method will then again reevaluate which Thread should be scheduled and so the scheduling continues.

然后,此方法将再次重新评估应调度哪个线程,以便继续调度。

#2


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.

正确,在引导过程中只有一个执行线程。通常情况是这样,直到OS初始化到低级别内存管理,调度程序等功能的程度。

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.

在多CP​​U系统中甚至就是这种情况 - 一个核心是处理早期启动的“主处理器”,直到基础设施开始启动其他核心。

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

最后,它是高度针对特定操作系统的;英特尔架构软件开发人员手册详细介绍了硬件规格。 (假设您正在谈论英特尔架构;其他架构可能会有很大不同。)

#3


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

(多线程)操作系统必须首先启动的是调度程序,它负责管理多个进程(因此也管理多个CPU线程,例如在多核机器上)。

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.

由该调度程序启动的第一个进程通常是某种“初始化”进程,该进程又负责随后加载其他程序/进程。