[Chapter 3 Process]Practice 3.4 Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets.

时间:2023-12-04 15:00:08

3.4 The Sun UltraSPARC processor has multiple register sets. Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets. What happens if the new context is in memory rather than in a register set and all the register sets are in use?

答案: 因为暂时还没找到多寄存器集的处理器在上下文切换时的工作原理, 所以无法好好理解此问题。 只能根据自己的猜测来回答此题。

  在单寄存器集处理器中, 上下文切换时需要先将当前进程的上下文保存到PCB中, 并读取下一个进程的上下文。多寄存器集, 进行上下文切换时, 如果新的上下文早已经读取到一个新的寄存器集中,  处理器应该会把切换到这个新的寄存器集, 使用新的上下文继续执行进程, 旧的寄存器集同时会将上下文信息保存到其进程PCB中, 新的上下文的读取和旧的上下文的保存应该是并发进行的, 所以context switch的时间开销要比单寄存器集少很多。

  如果新的上下文是在内存中而不是寄存器里, 那么在读取新的上下文时的时间开销要比读取寄存器里的上下文时间开销大。

扩展

  上下文包含CPU寄存器里的值, 进程状态, 内存管理信息。 上下文并不等于PCB, 它只是由PCB中的部分信息构成。

  要完成上下文的奇幻, 内核要将当前执行的进程上下文存储到当前进程在内存中的PCB中, 然后将新进程的上下文从其内存的PCB中读取到CPU寄存器中。