23-处理器调度 (xv6 上下文切换;处理器调度:机制和策略)

23-处理器调度 (xv6 上下文切换;处理器调度:机制和策略)

由用户态,执行syscall

跳转到内核态代码,寄存器保存,切换页表和栈

调度策略

1
taskset -c 3 nice -n 19 yes > /dev/null & taskset -c 3 nice -n 9 yes > /dev/null 

nice值相差10,占用cpu时间相差1倍

上下文切换是机制,策略是选择哪个进程执行

动态优先级,CPU密集型的优先级逐渐降低,交互型的优先级逐渐增加

动态优先级实现的虚拟时间是什么原理,为什么“好人”和“坏人”会相差几倍

进程的nice值越小(优先级越高),权重越大,在实际运行时间相同的情况下,虚拟运行时间越短,进程累计的虚拟运行时间增加得越慢,在红黑树中向右移动的速度越慢,被调度器选中的机会越大,被分配的运行时间相对越多。随着优先级高进程的虚拟运行时间增长,低优先级的进程也会有机会被调度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* Nice levels are multiplicative, with a gentle 10% change for every
* nice level changed. I.e. when a CPU-bound task goes from nice 0 to
* nice 1, it will get ~10% less CPU time than another CPU-bound task
* that remained on nice 0.
*
* The "10% effect" is relative and cumulative: from _any_ nice level,
* if you go up 1 level, it's -10% CPU usage, if you go down 1 level
* it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
* If a task goes up by ~10% and another task goes down by ~10% then
* the relative distance between them is ~25%.)
*/
static const int prio_to_weight[40] = {
/* -20 */ 88761, 71755, 56483, 46273, 36291,
/* -15 */ 29154, 23254, 18705, 14949, 11916,
/* -10 */ 9548, 7620, 6100, 4904, 3906,
/* -5 */ 3121, 2501, 1991, 1586, 1277,
/* 0 */ 1024, 820, 655, 526, 423,
/* 5 */ 335, 272, 215, 172, 137,
/* 10 */ 110, 87, 70, 56, 45,
/* 15 */ 36, 29, 23, 18, 15,
};

Linux CFS调度器 vruntime 的计算_linux vruntime-CSDN博客

CFS,完全公平调度,记录每个进程运行时间,每次都切换到运行时间最少的进程。

红黑树