调度器简介

内核中用来安排进程执行的模块称为调度器(scheduler),它可以切换进程状态(process state)。例如执行、可中断睡眠、不可中断睡眠、退出、暂停等。

 

调度器是CPU中央处理器的管理员,主要负责完成做两件事情:

一、选择某些就绪进程来执行,

二是打断某些执行的进程让它们变为就绪状态。调度器分配CPU时间的基本依据就是进程的优先级。上下文 切换(context switch):将进程在CPU中切换执行的过程,内核承担此任务,负责重建和存储被切换掉之前的CPU状态。

 

调度优先级

linux采用next记录调度优先级,共有40个,每个nice相差10%,意思是高一位的优先级比低一位优先级多执行10%的时间,

如果上升1级,CPU使用率为-10%,如果下降1级CPU使用率为+10%。

为了实现这一点,我们使用了1.25的乘数。如果一项任务上升约10%,

而另一项任务下降约10%,则它们之间的相对距离约为25%。

 * 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调度器以模块的方式提供,允许不同类型进程可以又在针对性选择调度算法。这种模块称为调度器类,每个调度器类都有一个优先级,他会按照优先级顺序遍历调度器类,拥有一个可执行进程的最高优先级的调度器类胜出,去选择下面要执行的那一个程序。

sched_class结构体表示调度类,定义在kernel/sched/sched.h。

 

Linux 调度类:dl_sched_class 、rt_sched_class 、fair_sched_class 及idle_sched_class 等。

 

 本图的调度器类优先级从上到下降序。每个进程都有一个调度策略,每个调度策略对应一个调度类(每个调度类可以对应多个策略)。

 

调度策略

 

 

 rt_sched_class  实时调度器

  调度策略 SCHED_FIFO  先进先出 

  SCHED_RR 增加了时间片的先进先出

fair_sched_class 类 完全按公平调度器

  SCHED_NORMAL :用于普通进程通过CFS调度器实现。

SCHED_IDLE:优先级最低,在系统空闲时才执行这类进程。

SCHED_BATCH 用于非交互处理器消耗型进程

 

 

 

实时进程永远比普通进程先运行。

 

posted on 2022-05-12 16:22  thotf  阅读(1221)  评论(0)    收藏  举报

导航