图解Linux内核调度系统

图解Linux内核调度系统

目录

1.进程调度系统

Linux进程调度是操作系统内核中的一个重要组成部分,它负责决定哪个进程可以在CPU上运行。

进程调度的目标是合理分配CPU资源,提高系统的整体运行效率和响应能力。

Linux设计了一套进程调度系统方便的实现进程调度目标。

Linux进程调度系统主要由:调度类,调度策略,调度实体,调度器,CPU运行队列组成。

图解Linux内核调度系统 图1

2.调度类

调度类在Linux内核中的定义:

struct task_struct {

const struct sched_class *sched_class; //调度类

struct sched_entity se; //完全公平调度实体

struct sched_rt_entity rt; //实时调度实体

struct sched_dl_entity dl; //截止调度实体

unsigned int policy; //调度策略

};

struct sched_class结构体定义

struct sched_class {

void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);

void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);

void (*yield_task) (struct rq *rq);

bool (*yield_to_task)(struct rq *rq, struct task_struct *p);

void (*check_preempt_curr)(struct rq *rq, struct task_struct *p, int flags)

struct task_struct *(*pick_next_task)(struct rq *rq);

void (*put_prev_task)(struct rq *rq, struct task_struct *p);

void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);

void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);

void (*task_fork)(struct task_struct *p);

void (*task_dead)(struct task_struct *p);

void (*switched_from)(struct rq *this_rq, struct task_struct *task);

void (*switched_to) (struct rq *this_rq, struct task_struct *task);

void (*prio_changed) (struct rq *this_rq, struct task_struct *task,

int oldprio);

unsigned int (*get_rr_interval)(struct rq *rq,

struct task_struct *task);

void (*update_curr)(struct rq *rq);

……

};

Linux内核定义的5种调度类(优先级由高到低)

图解Linux内核调度系统 图2

3.调度策略

struct task_struct {

unsigned int policy; //调度策略

};

图解Linux内核调度系统 图3

4.调度实体

进程调度实体是操作系统中负责进行进程调度的组件或对象。它通常是一个数据结构,用于存储进程的相关信息,包括进程的标识符、状态、优先级以及其他调度所需的属性。

调度类在Linux内核中的定义:

struct task_struct {

struct sched_entity se; //完全公平调度实体

struct sched_rt_entity rt; //实时调度实体

struct sched_dl_entity dl; //截止调度实体

};

图解Linux内核调度系统 图4

5.调度器

Linux进程调度通过主调度器和周期性调度器发起。

主调度器:schedule()

周期性调度器:schedule_tick()

6.CPU运行队列

Linux为每个CPU分配一个运行队列。

struct rq {

……;

struct cfs_rq cfs;

struct rt_rq rt;

struct dl_rq dl;

};

图解Linux内核调度系统 图5

← 返回文章列表