1. 进程调度

the process scheduler is the component of a kernel that selects which process to run next.
进程调度器需要使 处理器使用率最大化,并且提供 使多个进程并发执行的虚拟
 
Deciding which processes run, when, and for how long is the process scheduler's fundamental responsibility.
 
时间片:the scheduler allocates the process a “slice” of the processor's time
 
Linux进程调度算法CFS( Completely Fair Scheduler):enforce fair access to a resource among contending consumers
 

2. 时间片

如果时间片过大,那么 挂起进程 开始执行前的等待时间过长,这将减小 并发执行的粒度,甚至用户觉察到延迟
如果时间片过小,那么 系统在进程之间切换的时间花销将很大,时间局部性的优势将丧失
 
CPU密集型:processes are hungry for CPU time,比如科学计算、数学计算、图像处理
I/O密集型:spend more time blocked waiting for some resource than executing,often issuing and waiting for file or network I/O, blocking on keyboard input
 

3. CFS调度

传统Unix进程调度中,有两个最基础的变量:优先级和时间片
CFS引进 fair scheduling 算法,CFS赋予每个进程一定比例的处理器时间,而不是时间片
开始时,CFS赋予N个进程等同的 1/N 处理器时间,然后权衡每个进程比例进行动态调整
Processes with the default nice value of zero have a weight of one, so their proportion is unchanged
Processes with a smaller nice value (higher priority) receive a larger weight, increasing their fraction of the processor
process with a larger nice value (lower priority) receive a smaller weight, decreasing their fraction of the processor
 
为了确定每个进程每次运行的实际时间,CFS needs to divide the proportions into a fixed period
例如:20毫秒,2个相同优先级的进程,则每个进程赋予相同的权重和处理器时间比例,即每个进程10毫秒
如果是5个进程,则每个进程分配4毫秒,如果是200个进程呢?
按照常理应该是 每个进程分配100微秒,但是由于 上下文切换开销巨大,并且 局部性优势丧失,这将严重影响系统的吞吐率
 
这时CFS引入另一个变量:minimum granularity (最小粒度)
minimum granularity是任一进程运行时间长度的下限,这将保证 上下文切换开销占 系统总时间开销的比例 不会过大
 
By assigning proportions of the processor and not fixed timeslices,CFS is able to enforce fairness: each process gets its fair share of the processor
 

4. nice val

processes are assigned priorities that affect how long they run,Unix has historically called these priorities nice values
Legal nice values range from −20 to 19 inclusive, with a default value of 0, nice值越大,优先级越低,nice值越小,优先级越高
 
/* increments a process's nice value by inc and returns the newly updated value */
#include <unistd.h> int nice (int inc);
Passing 0 for inc is an easy way to obtain the current nice value 
nice函数错误时返回-1,但是 新的nice值也可能就是-1,为了区分 函数错误 和 返回nice新值为-1,则如下使用:
int ret;
errno = ;
ret = nice (); /* increase our nice by 10 */
if (ret == − && errno != )
perror ("nice");
else
printf ("nice value is now %d\n", ret);

获取/设置优先级:

#include <sys/time.h>
#include <sys/resource.h>
int getpriority (int which, int who);
int setpriority (int which, int who, int prio);
/* returns the current process’s priority */
int ret;
ret = getpriority (PRIO_PROCESS, );
printf ("nice value is %d\n", ret);
/* sets the priority of all processes in the current process group to 10 */
int ret;
ret = setpriority (PRIO_PGRP, , );
if (ret == −)
perror ("setpriority");

5. 处理器关联

the process scheduler must decide which processes run on each CPU
如果一个进程在一个CPU核上被调度,the process scheduler should aim to schedule it on the same CPU in the future
因为 进程从一个CPU核迁移到另一个CPU核的代价是巨大的(主要是缓存影响)
 
if a process moves to a new CPU and writes new data into memory, the data in the old  CPU's cache can become stale
这样,进程调度器必须在 进程迁移CPU花销 和 多个CPU负载均衡 之间取得平衡
 
The Linux scheduler attempts to schedule the same processes on the same processors for as long as possible, migrating a process from one CPU
to another only in situations of extreme load imbalance. This allows the scheduler to minimize the  cache effects of migration but still ensure that
all processors in a system are evenly  loaded
 

6. 实时系统

硬实时和软实时
A hard real-time system requires absolute adherence to operational deadlines
A soft real-time system does not consider over‐running a deadline to be a critical failure
 

7. Linux实时调度策略

FIFO调度:A FIFO-classed process will continue running so long as no higher-priority process becomes runnable
时间片轮转:When an RR-classed process exhausts its timeslice, the scheduler moves it to the end of the list of processes at  its priority
注意:这两种实时调度策略中,如果存在更高优先级的进程,那么低优先级进程将不会运行
int policy;
/* get our scheduling policy */
policy = sched_getscheduler ();
switch (policy) {
case SCHED_OTHER:
printf ("Policy is normal\n");
break;
case SCHED_RR:
printf ("Policy is round-robin\n");
break;
case SCHED_FIFO:
printf ("Policy is first-in, first-out\n");
break;
case -:
perror ("sched_getscheduler");
break;
default:
fprintf (stderr, "Unknown policy!\n");
}
Linux implements a range of 1 to 99 inclusive for the two real-time scheduling policies
Linux provides two system calls for retrieving the range of valid priority values:
int min, max;
min = sched_get_priority_min (SCHED_RR);
if (min == −) {
perror ("sched_get_priority_min");
return ;
}
max = sched_get_priority_max (SCHED_RR);
if (max == −) {
perror ("sched_get_priority_max");
return ;
}
printf ("SCHED_RR priority range is %d - %d\n", min, max);
实时调度的注意事项:
设计实时程序必须格外小心,can easily bring down the entire system
(1) 如果系统中没有更高优先级的实时进程,CPU密集型 循环程序将会 一直运行到结束
(2) Take care not to starve the rest of the system of processor time
(3) If a real-time process busy-waits for a resource held by a lower-priority process, the real-time process will busy-wait forever.
(因为低优先级进程不会运行从而释放资源)

Linux System Programming 学习笔记(六) 进程调度的更多相关文章

  1. Linux System Programming 学习笔记(十一) 时间

    1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...

  2. Linux System Programming 学习笔记(七) 线程

    1. Threading is the creation and management of multiple units of execution within a single process 二 ...

  3. Linux System Programming 学习笔记(四) 高级I/O

    1. Scatter/Gather I/O a single system call  to  read or write data between single data stream and mu ...

  4. Linux System Programming 学习笔记(二) 文件I/O

    1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...

  5. Linux System Programming 学习笔记(一) 介绍

    1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...

  6. Linux System Programming 学习笔记(十) 信号

    1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0)   内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...

  7. Linux System Programming 学习笔记(九) 内存管理

    1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...

  8. Linux System Programming 学习笔记(八) 文件和目录管理

    1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...

  9. Linux System Programming 学习笔记(五) 进程管理

    1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...

随机推荐

  1. 使用notepad++远程编辑Linux文档

    上一篇中,我写了如何使用使用ftp服务器实现很方便的通信,这一篇我分享一个使用notepad++的一个NPPFTP插件远程编辑Linux中的文档的小技巧. 首先要确保你的Linux的ftp服务已经打开 ...

  2. mysql绿色版下载及应用

    一.mysql绿色版下载 第一歩:打开下载网址:https://www.oracle.com 点击Menu-->Database and Technologies-->Databases- ...

  3. 【kindle】【转发】kindle链接WIFI自动断开问题

    在电脑上新建一个新文件,名为“WIFI_NO_NET_PROBE”,同时把后缀名删掉,让它变成一个无格式文件.Kindle 连接电脑,把新建的文件放进Kindle的根目录,断开Kindle之后重启Ki ...

  4. select2插件+ajax笔记

    目录 手册 思路 1. 如果是自己写的ajax这样就可以了. html里 控制器里 2. 如果是ecshop里,需要改写call方法为JQuery的ajax方法,才可以select2需要JQuery支 ...

  5. Confluence 导出为 PDF 格式 - 导出多个页面或者整个空间

    使用 Confluence 的空间导出功能,你可以将多个页面或者整个 Confluence 站点转换为 PDF 文件. 希望使用空间导出功能,你需要 导出空间(Export Space)权限.请查看 ...

  6. python解析库之 XPath

    1. XPath (XML Path Language) XML路径语言 2. XPath 常用规则: nodename    选取此节点的所有子节点 /                    从当前 ...

  7. 在SCIKIT中做PCA 逆变换 -- 新旧特征转换

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...

  8. cygwin的使用

    参考资料: 对话 UNIX: 在 Windows 上使用 Cygwin  Cygwin使用指南

  9. py文件转exe时包含paramiko模块出错解决方法

    问题描述:python代码中包含paramiko模块的远程登录ssh,在用pyInstaller转为exe时报错, 报错提示为“No handlers could be found for logge ...

  10. BZOJ 3351: [ioi2009]Regions

    对于一个询问(x,y)对y出现次数分类,若<=lim,在儿子处统计答案,若>lim则y的种类肯定<lim,在祖先处统计(仿佛要去重?但是没去重也过了,那个时限仿佛怎么做都能过) #i ...