您的位置:首页 > 其它

操作系统03进程管理Process_Scheduling

2017-02-17 17:42 453 查看

2 Process Scheduling

>Type of scheduling
>Scheduling Criteria (准则)
>Scheduling Algorithm
>Real-Time Scheduling (嵌入式系统)
 

2.1 Learning Objectives

By the end of this lecture you should be able to
·Explain what is Response Time
响应时间--分时系统
   Turnaround Time 周转时间--批处理系统
   Deadlines 截止时间--实时系统
   Throughput 吞吐量--批处理系统
·理解进程调度的目标、类型、原则
·理解Decision Mode:Non-preemptive
非剥夺  Preemptive
剥夺
·理解经典的调度算法
FCFS
Round Robin
Shortest Process Next  需估计
Shortest Remaining Time Next  需估计
Highest Response Ratio Next  工程上难实现
Feed Back
·理解Real--Time System
及 类型
·理解掌握 Real--Time Scheduling
   Deadline Scheduling
   Rate Monotonic Scheduling (速度调节)
 

Type of Scheduling

Aim of Scheduling
·Response Time 响应时间 分时系统
·Throughout 系统吞吐量 批处理系统
·效率
·公平
 
2.2 Types of Scheduling
·按OS的类型划分:
批处理调度、分时调度、实时调度、多处理机调度
·按OS的层次划分
Long-term Scheduling 长程调度    作业-->进程
Medium-term Scheduling 中程调度  进程在内存中的调度
Short-term Scheduling 短程调度    Swap调度



 


  

Long-term Scheduling

频率较低,通常几分钟一次,甚至更久
又称高级调度,作业调度,它为被调度作业或用户程序创建进程,分配必要的系统资源,并将新创建的进程插入就绪队列,等待Short-term Scheduling.
·Determines which programs are admitted to the system for processing.
这取决于调度算法,如FCFS、短作业优先、基于优先权、响应比高者优先等调度算法。
·How many programs are admitted to the system?
---controls the degree of multi-programming.
·When does the scheduler be invoked?
---Each time a job terminates.
---Processor is idle exceeds a certain threshold.
 
Medium-term Scheduling
又称中级调度,它调度换出到磁盘的进程进入内存,准备执行
·中程调度配合对换技术使用;
·其目的是为了提高内存的利用率和系统吞吐量;
·在多道程序度允许的情况下,从外存选择一个挂起状态的进程调度到内存(换入)



 Short-term Scheduling
·又称为进程调度,低级调度,调度内存中的就绪状态执行。
·know as the dispatcher  决定就绪队列which进程将获得处理机
·Executes most frequently
· Invoked when an event occurs
---Clock interrupts
---IO interrupts
---Operating System calls
---Signals(信号)
 

进程调度程序主要功能
1、选择占有处理机进程;
2、进程上下文切换。
 
进程调度依据的算法与系统的设计目标相一致。
对不同的系统,通常采用不同的调度算法。
批处理:短作业进程优先  目标:增加系统吞吐量,提高系统资源利用率
分时:时间片轮转  目标:保证每个用户能容忍的响应时间
———————————————————————————————————————————————————————————————————————————— 

2.3 Scheduling Criteria
面向用户
面向系统
 
User-oriented
·Response Time 响应时间
---Elapsed time between the submission of a request until there is output.
常用于评价分时系统性能。
·Turnaround Time 周转时间
---是指从作业提交给系统开始,到作业完成为止的这段时间间隔,也称为作业周转时间,常用语评价批处理系统的性能。
·Deadlines 截止时间
---是指某任务必须开始执行的最迟时间(starting deadline)或必须完成的最迟时间(completion deadline),常用于评价实时系统的性能。
 
BEST EFFORT 原则
 
System-oriented
·Throughput 吞吐量
---单位时间内系统所完成的作业数,用于评价批处理系统的性能。
·处理机利用率
---This is the percentage of time that the processor is busy.
---Effective and efficient utilization of the processor.
·Balance Resource 资源平衡
---keep the resources of the system busy.
适用于长程调度和中程调度。
·Fairness 公平性
---Process should be treated the same, and no process should suffer starvation.
 
Priorities
·Scheduler will always choose a process of higher priority over one of lower priorities.
·Have multiple Ready Queues to represent each level of priority.
·Lower-priority may suffer starvation.
---allow a process to change its priority based on its age(生存期) or execution history.
 


  

2.4 Scheduling Algorithm

 
Decision Mode
·Non-preemptive (非剥夺方式)
---Once a process is in the running state, it will continue until it terminates or blocks itself for IO.
---主要用于批处理系统。
·Preemptive (剥夺方式)  优先原则;短进程优先原则;时间片原则
---Currently running process may be interrupted and moved to the Ready State by the operating system.
---Allows for better service since any one process cannot monopolize the processor for very long.
---主要用于实时性要求较高的实时系统 及 性能要求较高的批处理系统 和 分时系统。
 
Process Scheduling 
1、First-Come-First-Served (FCFS) 
进程一旦获得处理机,便一直进行下去,直到完成或阻塞
·Each process joins the Ready queue.
·When the current process ceases to execute, the oldest process in the Ready queue is selected.
·A short process may have to wait a very long time before it can execute.
·Favors CPU-bound processes
---IO processes have to wait until CPU-bound process completes.
FCFS is unfair to short process.
FCFS算法的实际应用
·一般,FCFS与其他调度算法混合使用
·系统可以按照不同的优先级维护多个就绪队列,每个队列内部按照FCFS算法调度。
  
2、短进程优先
3、最高响应比优先 
非剥夺调度算法  响应比 = (等待时间+要求服务的时间)/ 要求服务的时间
4、优先级
——静态优先级:进程创建时确定其优先级    进程类型:系统进程优先;    对资源的需求:需求少优先;    用户申请的优先级:付费高者优先
——动态优先级:优先级在进行期间动态调整    UNIX根据  占用处理机时间/等待处理机时间  动态调整
5、时间片轮转
6、前后台调度 用于批处理和分时相结合的系统中
分时--前台--时间片轮转  
批处理--后台--先来先服务 
仅当前台无作业时,才将处理机分配给后台作业。
7、多级反馈队列轮转算法



时间片S0<S1<...<Sn
每个进程运行2~3个时间片,若未完成,则降级。
先调度优先级高的队列,当高优先级队列为空时,调度下一队列。
只要有高优先级进程进入,则立即调度。
调度算法决定进程,分派程序完成分配处理机给该进程。

工程经验很重要    小作业 -->
大设计

 
————————————————————————————————————————————————————————————————————————————
 

2.5 Real-Time Scheduling

 

Real-Time Systems
·Correctness of the system depends not only on the logical result of the computation but also the time at which the result are produced.
·Tasks or processes attempt to control or react to events that take place in the outside world.
·These events occur in
“real time” and process must be able to keep up with them.
 
 
Apply to
·Control of laboratory experiments
·Process control plants
·Robotics
·Aircraft control
·Telecommunications
·Military command and control system
 
指能及时响应外部事件的请求,在规定的时间内完成对该事件的处理,并控制所有实时任务协调一致的运行的计算机系统。
·实时控制系统:指要求进行实时控制的系统,用于生产过程的控制,实时采集现场数据,并对采集的数据进行及时处理。如飞机的自动驾驶系统,以及导弹的制导系统。
·实时信息处理系统:指能对信息进行实时处理的系统。典型的信息处理系统有:飞机订票系统,情报 检索系统。
 
Real-Time Task
·按任务执行时是否呈现周期性来划分
>periodic (周期性)实时任务
>aperiodic (非周期性)实时任务,必须联系着一个deadline
·根据对截止时间的要求来划分
>hard real-time task 硬实时任务
---系统必须满足任务对截止时间的要求,否则可能出现难以预测的结果。
>soft real-time task 软实时任务
 
Characteristics of Real-Time Operating System
·Deterministic (确定性)
---Operations are performed at fixed, predetermined times or within predetermined time intervals.
---Concerned with how long the operating system delays before acknowledging an interrupt.
·Responsiveness (响应性)
---How long, after acknowledgment, it takes the operating system to service the interrupt.
---Includes amount of time to begin execution of the interrupt.
---Includes amount of time to perform the interrupt.
 
Features of Real-Time Operating Systems
·Fast context switch
·Small size
·Ability to respond to external interrupts quickly
·Multi-tasking with inter-process communication tools such as semaphores, signals and events
·Files that accumulate data at a fast rate
·Use of special sequential files that can accumulate data at a fast rate
·Preemptive scheduling based on priority
·Minimization of intervals during which interrupts are disabled
·Delay tasks for fixed amount of time
·Special alarms and timeouts
报警和超时处理
 
 
>Scheduling of a Real-Time process
>Real-Time Scheduling
>Deadline Scheduling
>Rate Monotonic Scheduling
 
Scheduling of a Real-Time Process
·Round Robin Preemptive Scheduler
基于时间片的轮转调度算法
·Priority-driven Preemptive Scheduler
基于优先级的剥夺调度算法
·Priority-driven Non-preemptive Scheduler
基于优先级的非剥夺调度算法
·Immediate Preemptive Scheduler
立即剥夺调度算法
 
Round Robin Preemptive Scheduler



·响应时间在秒级
·广泛应用于分时系统,也可用于一般的实时信息处理系统
·不适合于要求严格的实时控制系统
 
Priority-driven Preemptive Scheduler



·当实时任务到达后,可以在时钟中断时,剥夺正在执行的低优先级进程的执行,调度执行高优先级的任务。
·响应时间较短,一般在几十毫秒或几毫秒。
 
 
 
 
Priority-driven Non-preemptive Scheduler



·为实时任务赋予较高的优先级,将它插入就绪队列队首,只要正在执行的进程释放processor,则立即调度实时任务执行。
·响应时间一般在数百毫秒到数秒之间。
·多用于多道批处理系统,也可以用于要求不太严格的实时系统。
 
Immediate Preemptive Scheduler



·要求操作系统具有快速响应外部事件的能力,一旦出现外部中断,只要当前任务未处于临界区,便立即剥夺其执行,把处理机分配给中断请求。
·调度时延可以降至100微妙,甚至更低。
 
Aim of Real-Time Scheduling
·hard real-time task
在其规定的截止时间内完成
·尽可能使soft real-time task
也能在规定的截止时间内完成
公平性和最短平均响应时间等要求已不再重要。
但是,大多数现代实时操作系统无法直接处理任务的截止时间,它们只能尽量提高响应速度,以尽快地调度任务。
 
Real-Time Scheduling
·Static table-driven
静态表驱动调度法
---Determines at run time when a table begins execution
·Static priority-driven preemptive
静态优先级剥夺调度法
---Traditional priority-driven scheduler is used
·Dynamic planning-based
动态计划调度法
·Dynamic best effort
动态最大努力调度法

Static table-driven approaches
·用于调度周期性实时任务
·按照任务周期到达的时间、执行时间、完成截止时间(ending deadline)以及任务的优先级,制定调度表,调度实时任务。
·最早截止时间优先(EDF)调度算法即属于此类。
·此类算法不灵活,任何任务的调度申请改动都会引起调度表的修改。
·此类算法多用于非实时多道程序系统。
·优先级的确定方法很多,例如在分时系统中,可以对IO bound
和 processor bound的进程赋予不同的优先级,例如速度单调(RM)算法,即是为实时任务赋予静态优先级。
 
Dynamic planning-based approaches
·当实时任务到达后,系统为新到达的任务和正在执行的任务动态创建一张调度表。
·在当前执行进程不会错过其截止时间的条件下,如果也能使新到达任务在截止时间内完成,则立即调度执行新任务。
 
Dynamic best effort approaches
·实现简单,广泛用于非周期性实时任务调度
---当任务到达时,系统根据其属性赋予优先级,优先级高的先调度。例如最早截止时间有限EDF调度算法就采用了这种方法。这种算法总是尽最大努力今早调度紧迫任务,因此成为最大努力调度算法。
·缺点在于,当任务完成,或截止时间到达,很难知道该任务是否满足其约束时间。
 
Deadline Scheduling
·Information used
---Ready time
---Starting deadline
---Completion deadline 不会同时出现
---Processing time
---Resources requirement
---Priority
---Sub-task scheduler
一个任务可分解出强制子任务(mandatory sub-task)和非强制子任务(optional sub-task)。
只有强制子任务拥有硬截止时间 hard deadline
·Which task to schedule next?
---Scheduling tasks with the earliest deadlines minimized the fraction of tasks that miss their deadlines.
·What sort of preemptive is allowed?
---When starting deadlines are specified, then a non-preemptive scheduler make sense.
在执行完强制子任务或临界区后,阻塞自己。
---For a system with completion deadlines, a preemptive strategy is most appropriate.
 
Earliest Deadline 最早截止时间优先,简称ED
·常用调度算法
·若指定任务的starting deadline,则采用Non-preemption,当某任务的开始截止时间到达时,正在执行的任务必须执行完其强制部分或临界区,释放CPU,调度开始截止时间的任务执行。
Periodic tasks with completion deadlines
·由于此类任务是周期性的,可预测的,可采用静态表驱动之最早截止时间优先调度算法,使系统中的任务都能按要求完成。
·举例:周期性任务A和B,指定了它们的完成截止时间,任务A每隔20毫秒完成一次,任务B每隔50毫秒完成一次。任务A每次需要执行10毫秒,任务B每次需要执行25毫秒。
Execution Profile of Two Periodic Tasks
Process
Arrival Time
Execution Time
Ending Deadline
A(1)
0
10
20
A(2)
20
10
40
A(3)
40
10
60
A(4)
60
10
80
...
...
...
...
B(1)
0
25
50
B(2)
50
25
100
 
 


 
Aperiod tasks with starting deadlines
·可采用最早截止时间优先调度算法或允许CPU空闲的EDF调度算法。
·Earliest Deadline with Unforced Idle Times (允许CPU空闲的EDF调度算法)
指优先调度最早截止时间的任务,并将它执行完毕才调度下一个任务。即使选定的任务未就绪,允许CPU空闲等待,也不能调度其他任务。尽管CPU利用率不高,但这种调度算法可以保证系统中的任务都能按要求完成。
 
Rate Monotonic Scheduling
速度单调调度算法
·Assigns priorities to tasks on the basis of their periods.
·Highest-priority task is the one with the shortest period.
·Period(任务周期),指一个任务到达至下一个任务到达之间的时间范围。
·Rate(任务速度),即周期(秒)的倒数,以赫兹为单位。
·任务周期的结束,表示任务的硬截止时间、任务的执行时间不能超过任务周期。
 
 ——————————————————————————————————————————————————————————————————————————

Linux PCB 为task_struct,定义在文件include/linux/seched.h中
系统有一个task[]数组,用于存放每一个进程控制块的指针。
fork()  一个进程创建其子进程,除PID外,代码段和数据的内容相同
exec() 进程执行
wait() 等待子进程结束
exit() 结束本进程

进程调度
普通进程  SCHED_OTHER  优先级 0~99  task_struct中的counter
实时进程  SCHED_RR/SCHED_FIFO  1~99  rt_priority
实时进程优先级高于其他进程

系统的3个TASK_QUEUE:tq_timer  tq_imediate  tq_scheduler
在文件sched.c中定义,DECLARE_TASK_QUEUE

Linux具体调度流程sched.c
调用run_task_queue处理任务队列tq_scheduler,普通进程counter值,实时进程counter+1000

SPINLOCK自旋锁  spinlock.h
普通自旋锁
读/写自旋锁  允许多个读者,一个写者
希望进入此区域的进程试图将此锁(是一个整数)的初始值从0置为1。如果是1,则再次尝试,此时进程好像在一段循环代码中自旋。
对包含此锁的内存区域的存取必须是原子性的。
控制进程离开临界区时,递减自旋锁。
任何自旋进程都可读取它,最快读取的将递增此值并进入临界区。
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: