为什么上下文切换是一个昂贵的过程并且在什么方面是昂贵的?

Why is context-switching an expensive procedure and expensive in terms of what?

在我的操作系统 class 中,考试要知道的问题之一是 "Why is context switching expensive?" 昂贵是什么意思,昂贵在什么方面?就执行任务所需的时间而言?

时间

A context switch is the process of storing and restoring the state (context) of a process.

所以 OS 实际上确实获取了 "a photograph" 的过程,存储它并能够在以后恢复它,但这是昂贵的。

该过程也会占用一些 space,但最昂贵的方面是时间。

"Why is context switching expensive?" What do they mean by expensive, and expensive in terms of what? In terms of time it takes to execute the task?

上下文切换的开销有几个原因。

首先,保存上下文的步骤本身就很费力。必须保存所有过程寄存器。这包括通用寄存器、进程状态寄存器和某些系统上的专用寄存器(例如,浮点数)。

其次,为新进程恢复上下文同样耗时。

第三,必须为新进程刷新和重新加载内存缓存。

第四,确定下一步要执行的进程存在开销。

(可能还有一些我没有列出的)。

所有这些都需要时间。