我有一个这样的问题:
对某个系统的测量表明,在 IO 阻塞之前,进程平均运行时间 T。进程切换需要时间 S,这实际上被浪费了(开销)。对于量子 Q 的循环调度,给出以下各项的 CPU 效率公式
( a ) Q = INFINITY
( b ) Q > T
( c ) S < Q < T
( d ) Q = S
( e ) Q -> 0
我知道如何做 a、b、d 和 e,但对于 c,答案是 T/(T + S * T/Q) = Q/(Q + S)。这意味着上下文切换发生的总次数是T/Q,这让我很困惑,假设T = 3,Q = 2,进程运行2个单位并切换到另一个进程,然后又切换回执行并完成,然后再次切换到另一个进程,所以是2次切换,即 = Roof(T/Q);但根据答案,只有1次切换,所以跑1轮和2轮没有什么区别?谁能给我解释一下 CPU 效率到底是什么。
最佳答案
您的问题没有说明有关 IO 阻塞时调度程序切换的任何信息,因此我不认为您提供的答案是正确的。它没有考虑到进程被IO阻塞时CPU被浪费的事实。让我们看一个有 2 个进程的示例:
repeat floor(T/Q) times:
Process 1 runs (Q units of time)
Context switch to process 2 (S units of time)
Process 2 runs (Q units of time)
Context switch to process 1 (S units of time)
if T mod Q > 0
Process 1 runs (T mod Q units of time) then blocks to IO
CPU is idle (Q - T mod Q units of time)
Context switch to process 2 (S units of time)
Process 2 runs (T mod Q units of time) then blocks to IO
CPU is idle (Q - T mod Q units of time)
Context switch to process 1 (S units of time)
Total time elapsed = 2(Q+S)*ceiling(T/Q)
Total time processes were running = 2T
Efficiency = T/((Q+S)*ceiling(T/Q))
如果调度程序在进程被阻塞后切换,则:
repeat floor(T/Q) times:
Process 1 runs (Q units of time)
Context switch to process 2 (S units of time)
Process 2 runs (Q units of time)
Context switch to process 1 (S units of time)
if T mod Q > 0
Process 1 runs (T mod Q units of time) then blocks to IO
Context switch to process 2 (S units of time)
Process 2 runs (T mod Q units of time) then blocks to IO
Context switch to process 1 (S units of time)
Total time elapsed = 2T + 2*S*ceiling(T/Q)
Total time processes were running = 2T
Efficiency = T/(T+S*ceiling(T/Q))
因此,如果我们假设调度程序在阻塞时进行切换,那么您得到的答案就是缺少天花板()部分。如果我们假设 T 始终是 Q 的倍数,那么您甚至不需要它。但不确定你的问题说明了什么。
顺便说一句,我认为您重复计算了上下文切换,因为您是从单个进程的角度来看待它的。当您考虑调度多个进程时,每个运行的量程都应该有一个上下文切换,这一事实变得更加清楚。
关于multithreading - CPU效率公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9180150/