标签:des style blog class c code
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 = 0;
ret = nice (10); /* increase our nice by 10 */
if (ret == ?1 && errno != 0)
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, 0);
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, 0, 10);
if (ret == ?1)
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 (0);
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 -1:
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 == ?1) {
perror ("sched_get_priority_min");
return 1;
}
max = sched_get_priority_max (SCHED_RR);
if (max == ?1) {
perror ("sched_get_priority_max");
return 1;
}
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 学习笔记(六) 进程调度,布布扣,bubuko.com
Linux System Programming 学习笔记(六) 进程调度
标签:des style blog class c code
原文地址:http://www.cnblogs.com/wwwjieo0/p/3743251.html