操作系统03进程管理Process_Scheduling
2 Process Scheduling
>Type of scheduling
>Scheduling Criteria (準則)
>Scheduling Algorithm
>Real-Time Scheduling (嵌入式系統(tǒng))
?
2.1 Learning Objectives
By the end of this lecture you should be able to
·Explain what is Response Time 響應時間--分時系統(tǒng)
???Turnaround Time 周轉時間--批處理系統(tǒng)
???Deadlines 截止時間--實時系統(tǒng)
???Throughput 吞吐量--批處理系統(tǒng)
·理解進程調(diào)度的目標、類型、原則
·理解Decision Mode:Non-preemptive 非剝奪 ?Preemptive 剝奪
·理解經(jīng)典的調(diào)度算法
FCFS
Round Robin
Shortest Process Next ?需估計
Shortest Remaining Time Next ?需估計
Highest Response Ratio Next ?工程上難實現(xiàn)
Feed Back
·理解Real--Time System 及 類型
·理解掌握 Real--Time Scheduling
???Deadline Scheduling
???Rate Monotonic Scheduling (速度調(diào)節(jié))
?
Type of Scheduling
Aim of Scheduling
·Response Time 響應時間 分時系統(tǒng)
·Throughout 系統(tǒng)吞吐量 批處理系統(tǒng)
·效率
·公平
?
2.2 Types of Scheduling
·按OS的類型劃分:
批處理調(diào)度、分時調(diào)度、實時調(diào)度、多處理機調(diào)度
·按OS的層次劃分
Long-term Scheduling 長程調(diào)度 ? ?作業(yè)-->進程
Medium-term Scheduling 中程調(diào)度 ?進程在內(nèi)存中的調(diào)度
Short-term Scheduling 短程調(diào)度 ? ?Swap調(diào)度
?
??
Long-term Scheduling
頻率較低,通常幾分鐘一次,甚至更久
又稱高級調(diào)度,作業(yè)調(diào)度,它為被調(diào)度作業(yè)或用戶程序創(chuàng)建進程,分配必要的系統(tǒng)資源,并將新創(chuàng)建的進程插入就緒隊列,等待Short-term Scheduling.
·Determines which?programs are admitted to the system for processing.
這取決于調(diào)度算法,如FCFS、短作業(yè)優(yōu)先、基于優(yōu)先權、響應比高者優(yōu)先等調(diào)度算法。
·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
又稱中級調(diào)度,它調(diào)度換出到磁盤的進程進入內(nèi)存,準備執(zhí)行
·中程調(diào)度配合對換技術使用;
·其目的是為了提高內(nèi)存的利用率和系統(tǒng)吞吐量;
·在多道程序度允許的情況下,從外存選擇一個掛起狀態(tài)的進程調(diào)度到內(nèi)存(換入)
?Short-term Scheduling
·又稱為進程調(diào)度,低級調(diào)度,調(diào)度內(nèi)存中的就緒狀態(tài)執(zhí)行。
·know as the dispatcher ?決定就緒隊列which進程將獲得處理機
·Executes most frequently
· Invoked when an event occurs
---Clock interrupts
---IO interrupts
---Operating System calls
---Signals(信號)
?
進程調(diào)度程序主要功能
1、選擇占有處理機進程;
2、進程上下文切換。
?
進程調(diào)度依據(jù)的算法與系統(tǒng)的設計目標相一致。
對不同的系統(tǒng),通常采用不同的調(diào)度算法。
批處理:短作業(yè)進程優(yōu)先 ?目標:增加系統(tǒng)吞吐量,提高系統(tǒng)資源利用率
分時:時間片輪轉 ?目標:保證每個用戶能容忍的響應時間
————————————————————————————————————————————————————————————————————————————?
2.3 Scheduling Criteria
面向用戶
面向系統(tǒng)
?
User-oriented
·Response Time 響應時間
---Elapsed time between the submission of a request until there is output. 常用于評價分時系統(tǒng)性能。
·Turnaround Time 周轉時間
---是指從作業(yè)提交給系統(tǒng)開始,到作業(yè)完成為止的這段時間間隔,也稱為作業(yè)周轉時間,常用語評價批處理系統(tǒng)的性能。
·Deadlines 截止時間
---是指某任務必須開始執(zhí)行的最遲時間(starting deadline)或必須完成的最遲時間(completion deadline),常用于評價實時系統(tǒng)的性能。
?
BEST EFFORT 原則
?
System-oriented
·Throughput 吞吐量
---單位時間內(nèi)系統(tǒng)所完成的作業(yè)數(shù),用于評價批處理系統(tǒng)的性能。
·處理機利用率
---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. 適用于長程調(diào)度和中程調(diào)度。
·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.
---主要用于批處理系統(tǒng)。
·Preemptive (剝奪方式) ?優(yōu)先原則;短進程優(yōu)先原則;時間片原則
---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.
---主要用于實時性要求較高的實時系統(tǒng) 及 性能要求較高的批處理系統(tǒng) 和 分時系統(tǒng)。
?
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與其他調(diào)度算法混合使用
·系統(tǒng)可以按照不同的優(yōu)先級維護多個就緒隊列,每個隊列內(nèi)部按照FCFS算法調(diào)度。
??
2、短進程優(yōu)先
3、最高響應比優(yōu)先?
非剝奪調(diào)度算法 ?響應比 = (等待時間+要求服務的時間)/ 要求服務的時間
4、優(yōu)先級
——靜態(tài)優(yōu)先級:進程創(chuàng)建時確定其優(yōu)先級 ? ?進程類型:系統(tǒng)進程優(yōu)先; ? ?對資源的需求:需求少優(yōu)先; ? ?用戶申請的優(yōu)先級:付費高者優(yōu)先
——動態(tài)優(yōu)先級:優(yōu)先級在進行期間動態(tài)調(diào)整 ? ?UNIX根據(jù) ?占用處理機時間/等待處理機時間 ?動態(tài)調(diào)整
5、時間片輪轉
6、前后臺調(diào)度?用于批處理和分時相結合的系統(tǒng)中
分時--前臺--時間片輪轉 ?
批處理--后臺--先來先服務?
僅當前臺無作業(yè)時,才將處理機分配給后臺作業(yè)。
7、多級反饋隊列輪轉算法
時間片S0<S1<...<Sn
每個進程運行2~3個時間片,若未完成,則降級。
先調(diào)度優(yōu)先級高的隊列,當高優(yōu)先級隊列為空時,調(diào)度下一隊列。
只要有高優(yōu)先級進程進入,則立即調(diào)度。
調(diào)度算法決定進程,分派程序完成分配處理機給該進程。
工程經(jīng)驗很重要 ???小作業(yè) --> 大設計
?
————————————————————————————————————————————————————————————————————————————
?
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
?
指能及時響應外部事件的請求,在規(guī)定的時間內(nèi)完成對該事件的處理,并控制所有實時任務協(xié)調(diào)一致的運行的計算機系統(tǒng)。
·實時控制系統(tǒng):指要求進行實時控制的系統(tǒng),用于生產(chǎn)過程的控制,實時采集現(xiàn)場數(shù)據(jù),并對采集的數(shù)據(jù)進行及時處理。如飛機的自動駕駛系統(tǒng),以及導彈的制導系統(tǒng)。
·實時信息處理系統(tǒng):指能對信息進行實時處理的系統(tǒng)。典型的信息處理系統(tǒng)有:飛機訂票系統(tǒng),情報 檢索系統(tǒng)。
?
Real-Time Task
·按任務執(zhí)行時是否呈現(xiàn)周期性來劃分
>periodic (周期性)實時任務
>aperiodic (非周期性)實時任務,必須聯(lián)系著一個deadline
·根據(jù)對截止時間的要求來劃分
>hard real-time task 硬實時任務
---系統(tǒng)必須滿足任務對截止時間的要求,否則可能出現(xiàn)難以預測的結果。
>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 基于時間片的輪轉調(diào)度算法
·Priority-driven Preemptive Scheduler 基于優(yōu)先級的剝奪調(diào)度算法
·Priority-driven Non-preemptive Scheduler 基于優(yōu)先級的非剝奪調(diào)度算法
·Immediate Preemptive Scheduler 立即剝奪調(diào)度算法
?
Round Robin Preemptive Scheduler
·響應時間在秒級
·廣泛應用于分時系統(tǒng),也可用于一般的實時信息處理系統(tǒng)
·不適合于要求嚴格的實時控制系統(tǒng)
?
Priority-driven Preemptive Scheduler
·當實時任務到達后,可以在時鐘中斷時,剝奪正在執(zhí)行的低優(yōu)先級進程的執(zhí)行,調(diào)度執(zhí)行高優(yōu)先級的任務。
·響應時間較短,一般在幾十毫秒或幾毫秒。
?
?
?
?
Priority-driven Non-preemptive Scheduler
·為實時任務賦予較高的優(yōu)先級,將它插入就緒隊列隊首,只要正在執(zhí)行的進程釋放processor,則立即調(diào)度實時任務執(zhí)行。
·響應時間一般在數(shù)百毫秒到數(shù)秒之間。
·多用于多道批處理系統(tǒng),也可以用于要求不太嚴格的實時系統(tǒng)。
?
Immediate Preemptive Scheduler
·要求操作系統(tǒng)具有快速響應外部事件的能力,一旦出現(xiàn)外部中斷,只要當前任務未處于臨界區(qū),便立即剝奪其執(zhí)行,把處理機分配給中斷請求。
·調(diào)度時延可以降至100微妙,甚至更低。
?
Aim of Real-Time Scheduling
·hard real-time task 在其規(guī)定的截止時間內(nèi)完成
·盡可能使soft real-time task 也能在規(guī)定的截止時間內(nèi)完成
公平性和最短平均響應時間等要求已不再重要。
但是,大多數(shù)現(xiàn)代實時操作系統(tǒng)無法直接處理任務的截止時間,它們只能盡量提高響應速度,以盡快地調(diào)度任務。
?
Real-Time Scheduling
·Static table-driven 靜態(tài)表驅動調(diào)度法
---Determines at run time when a table begins execution
·Static priority-driven preemptive 靜態(tài)優(yōu)先級剝奪調(diào)度法
---Traditional priority-driven scheduler is used
·Dynamic planning-based 動態(tài)計劃調(diào)度法
·Dynamic best effort 動態(tài)最大努力調(diào)度法
Static table-driven approaches
·用于調(diào)度周期性實時任務
·按照任務周期到達的時間、執(zhí)行時間、完成截止時間(ending deadline)以及任務的優(yōu)先級,制定調(diào)度表,調(diào)度實時任務。
·最早截止時間優(yōu)先(EDF)調(diào)度算法即屬于此類。
·此類算法不靈活,任何任務的調(diào)度申請改動都會引起調(diào)度表的修改。
·此類算法多用于非實時多道程序系統(tǒng)。
·優(yōu)先級的確定方法很多,例如在分時系統(tǒng)中,可以對IO bound 和 processor bound的進程賦予不同的優(yōu)先級,例如速度單調(diào)(RM)算法,即是為實時任務賦予靜態(tài)優(yōu)先級。
?
Dynamic planning-based approaches
·當實時任務到達后,系統(tǒng)為新到達的任務和正在執(zhí)行的任務動態(tài)創(chuàng)建一張調(diào)度表。
·在當前執(zhí)行進程不會錯過其截止時間的條件下,如果也能使新到達任務在截止時間內(nèi)完成,則立即調(diào)度執(zhí)行新任務。
?
Dynamic best effort approaches
·實現(xiàn)簡單,廣泛用于非周期性實時任務調(diào)度
---當任務到達時,系統(tǒng)根據(jù)其屬性賦予優(yōu)先級,優(yōu)先級高的先調(diào)度。例如最早截止時間有限EDF調(diào)度算法就采用了這種方法。這種算法總是盡最大努力今早調(diào)度緊迫任務,因此成為最大努力調(diào)度算法。
·缺點在于,當任務完成,或截止時間到達,很難知道該任務是否滿足其約束時間。
?
Deadline Scheduling
·Information used
---Ready time
---Starting deadline
---Completion deadline 不會同時出現(xiàn)
---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.
在執(zhí)行完強制子任務或臨界區(qū)后,阻塞自己。
---For a system with completion deadlines, a preemptive strategy is most appropriate.
?
Earliest Deadline 最早截止時間優(yōu)先,簡稱ED
·常用調(diào)度算法
·若指定任務的starting deadline,則采用Non-preemption,當某任務的開始截止時間到達時,正在執(zhí)行的任務必須執(zhí)行完其強制部分或臨界區(qū),釋放CPU,調(diào)度開始截止時間的任務執(zhí)行。
Periodic tasks with completion deadlines
·由于此類任務是周期性的,可預測的,可采用靜態(tài)表驅動之最早截止時間優(yōu)先調(diào)度算法,使系統(tǒng)中的任務都能按要求完成。
·舉例:周期性任務A和B,指定了它們的完成截止時間,任務A每隔20毫秒完成一次,任務B每隔50毫秒完成一次。任務A每次需要執(zhí)行10毫秒,任務B每次需要執(zhí)行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
·可采用最早截止時間優(yōu)先調(diào)度算法或允許CPU空閑的EDF調(diào)度算法。
·Earliest Deadline with Unforced Idle Times (允許CPU空閑的EDF調(diào)度算法)
指優(yōu)先調(diào)度最早截止時間的任務,并將它執(zhí)行完畢才調(diào)度下一個任務。即使選定的任務未就緒,允許CPU空閑等待,也不能調(diào)度其他任務。盡管CPU利用率不高,但這種調(diào)度算法可以保證系統(tǒng)中的任務都能按要求完成。
?
Rate Monotonic Scheduling
速度單調(diào)調(diào)度算法
·Assigns priorities to tasks on the basis of their periods.
·Highest-priority task is the one with the shortest period.
·Period(任務周期),指一個任務到達至下一個任務到達之間的時間范圍。
·Rate(任務速度),即周期(秒)的倒數(shù),以赫茲為單位。
·任務周期的結束,表示任務的硬截止時間、任務的執(zhí)行時間不能超過任務周期。
?
?——————————————————————————————————————————————————————————————————————————
Linux PCB 為task_struct,定義在文件include/linux/seched.h中
系統(tǒng)有一個task[]數(shù)組,用于存放每一個進程控制塊的指針。
fork() ?一個進程創(chuàng)建其子進程,除PID外,代碼段和數(shù)據(jù)的內(nèi)容相同
exec() 進程執(zhí)行
wait() 等待子進程結束
exit() 結束本進程
進程調(diào)度:
普通進程 ?SCHED_OTHER ?優(yōu)先級 0~99 ?task_struct中的counter
實時進程 ?SCHED_RR/SCHED_FIFO ?1~99 ?rt_priority
實時進程優(yōu)先級高于其他進程
系統(tǒng)的3個TASK_QUEUE:tq_timer ?tq_imediate ?tq_scheduler
在文件sched.c中定義,DECLARE_TASK_QUEUE
Linux具體調(diào)度流程sched.c
調(diào)用run_task_queue處理任務隊列tq_scheduler,普通進程counter值,實時進程counter+1000
SPINLOCK自旋鎖 ?spinlock.h
普通自旋鎖
讀/寫自旋鎖 ?允許多個讀者,一個寫者
希望進入此區(qū)域的進程試圖將此鎖(是一個整數(shù))的初始值從0置為1。如果是1,則再次嘗試,此時進程好像在一段循環(huán)代碼中自旋。
對包含此鎖的內(nèi)存區(qū)域的存取必須是原子性的。
控制進程離開臨界區(qū)時,遞減自旋鎖。
任何自旋進程都可讀取它,最快讀取的將遞增此值并進入臨界區(qū)。
?
?
總結
以上是生活随笔為你收集整理的操作系统03进程管理Process_Scheduling的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 操作系统02进程管理Process_De
- 下一篇: 操作系统04进程同步与通信