linux内核设计与实现读书笔记 第四章

进程调度

进程调度器的作用

The process scheduler decides which process runs, when, and for how long.

By deciding which process runs next, the scheduler is responsible for best utilizing the system and giving users the impression that multiple processes are executing simultaneously.

调度器分类

Multitasking operating systems come in two flavors: cooperative multitasking and preemptive multitasking.

nice值

In other Unix-based systems, such as Mac OS X, the nice value is a control over the absolute timeslice allotted to a process; in Linux, it is a control over the proportion of timeslice.

Instead of using the nice value to calculate a timeslice, CFS uses the nice value to weight the propor- tion of processor a process is to receive

cfs的“时间片”

对于cfs,时间片是一个整体概念,针对当前所有的进程来说的,而不是针对具体某一个进程

To calculate the actual timeslice, CFS sets a target for its approximation of the “infinitely small” scheduling duration in perfect multitasking.This target is called the targeted latency.CFS imposes a floor on the timeslice assigned to each process.This floor is called the minimum granularity.(最小粒度)

Put generally, the proportion of processor time that any process receives is determined only by the relative difference in niceness between it and the other runnable processes.

vruntime

The vruntime variable stores the virtual runtime of a process, which is the actual runtime (the amount of time spent running) normalized (or weighted) by the number of runnable processes

对于normal process(cfs来调度),还有一个idealruntime的概念,当vruntime>idealruntime是,会设置一个flag参数(need_resched),这样下次在允许进行schedule的时候会发生线程(进程)的切换

cfs进程选择机制

Pick the task with the smallest vruntime

调度入口(schedule函数)

首先linux系统里根据不同的优先级有不同的调度器,比如对于real time进程和normal进程,它们对应的调度器是不同的

在schedule函数的实现中,会按照调度器的优先级选取合适的进程来运行,只有高优先级的调度器没有对应的进程时,才会有低优先级的调度器选取它的进程

sleeping and waking up

linux内核设计与实现读书笔记 第四章_第1张图片

Preemption and Context Switching

It is called by schedule() when a new process has been selected to run. It does two basic jobs:

  1. Calls switch_mm(), which is declared in , to switch the vir- tual memory mapping from the previous process’s to that of the new process.
  2. Calls switch_to(), declared in , to switch the processor state from the previous process’s to the current’s.This involves saving and restoring stack infor- mation and the processor registers and any other architecture-specific state that must be managed and restored on a per-process basis.

User Preemption

  1. When returning to user-space from a system call
  2. When returning to user-space from an interrupt handler

Kernel Preemption

  1. When an interrupt handler exits, before returning to kernel-space
  2. When kernel code becomes preemptible again
  3. If a task in the kernel explicitly calls schedule()
  4. If a task in the kernel blocks (which results in a call to schedule())

 

你可能感兴趣的:(操作系统)