From last lecture • A preemptive method where the priority of the process determines whether it continues to run or it is disrupted TDDC47: Concurrent and real-time programming Lecture 6: Scheduling II Si i Nadjm-Tehrani Simin N dj T h i ”Most important process first!” Real-time Systems Laboratory Department of Computer and Information Science Linköping University Undergraduate course on Real-time Systems Linköping University Undergraduate course on Real-time Systems Linköping University 34 pages Autumn 2010 2 of 34 Autumn 2010 RMS Priorities • Each process has a period Ti that is the shortest interval between its release times Rate Monotonic Scheduling: • On-line • Preemptive • Priority-based with fixed (static) priorities Undergraduate course on Real-time Systems Linköping University • Processes are assigned priorities dependent on length of Ti – The shorter Ti the higher the priority Undergraduate course on Real-time Systems Linköping University 3 of 34 Autumn 2010 Consider following scenario: Example (4) Period (Ti) WCET (Ci) Priority P1 P2 20 10 high 50 30 10 5 low medium arrival time 0 20 30 40 50 60 P3 preemption 0 Undergraduate course on Real-time Systems Linköping University 5 of 34 Autumn 2010 4 of 34 Autumn 2010 10 20 30 40 Undergraduate course on Real-time Systems Linköping University process P1, P2, P3 P1 P3 P1 P2 P1, P3 ... 50 60 80 90 time 6 of 34 Autumn 2010 1 Schedulability test For this example U = 10/20+10/50+5/30 = 0,87 Theorem: (sufficient condition) n=3 For n processes, processes RMS will guarantee their schedulability if the total utilisation U = C1/T1 + ... + Cn/Tn does not exceed the guarantee level G = n (2 1/n -1) Undergraduate course on Real-time Systems Linköping University Schedulability is not guaranteed! (but processes may still meet their deadlines...) Undergraduate course on Real-time Systems Linköping University 7 of 34 Autumn 2010 arrival time 0 20 30 40 50 60 • Try with the critical instant: Assume that all processes are released simultaneously at time 0, and then arrive according to their periods • Check whether each process meets its deadline for all releases before the first deadline for the process with lowest priority 10 20 30 • P Process sett schedulable h d l bl if Ri ≤ Ti for f all ll processes 40 ... 50 60 90 time 10 of 34 Autumn 2010 Response time analysis • Response time: the time between the release and the completion time • Tasks suffer interference from higher priority tasks Ri Ci I i Ri Ci jhp ( i ) Ri Cj Tj • Iterative formula for calculating response time • Assumptions? win 1 Ci Undergraduate course on Real-time Systems Linköping University 80 Undergraduate course on Real-time Systems Linköping University Exact analysis • Mathematical equations for computing worst case response times Ri for each process process P1, P2, P3 P1 P3 P1 P2 P1, P3 preemption 0 9 of 34 Autumn 2010 8 of 34 Autumn 2010 For example 4 scenario: When the test fails Undergraduate course on Real-time Systems Linköping University G = 3(2 1/3 -1) = 0,78 11 of 34 Autumn 2010 Undergraduate course on Real-time Systems Linköping University win Cj jhp ( i ) T j 12 of 34 Autumn 2010 2 Not schedulabe task set When response time analysis gives a “no” answer: • Optimality: RMS is optimal among methods with fixed priority (in what sense?) • Lowest upper bound: For arbitrarily large n, it suffices that processor utilisation is < 0.69 • Change U by reducing Ci (code optimisation, faster processor, ...) or • Increase Ti for some process (can one do this?) Undergraduate course on Real-time Systems Linköping University Theorems [Nice proofs in Buttazzo book – can lend if interested] 13 of 34 Autumn 2010 Undergraduate course on Real-time Systems Linköping University 14 of 34 Autumn 2010 What does the test mean? Utilisation based test: G = n ( 2 1/n - 1) Example (2) Period (Ti) WCET (Ci) For a given n, the highest ceiling under which we only find schedulable task sets P1 P2 P3 20 7 50 10 30 5 U = 7/20 + 10/50 + 5/30 = 0,72 >0,69 but... < G = 0,78 (irrespective of release times, with all possible Ci, Ti) The schedulability of this task set is guaranteed! Undergraduate course on Real-time Systems Linköping University 15 of 34 Autumn 2010 Undergraduate course on Real-time Systems Linköping University Recall: Better methods for... • Processes with long WCET 16 of 34 Autumn 2010 Dynamic priorities • Allow – processes with long Ti and short deadline – Process dependencies: when processes share resources and must be synchronised • Next scheduling algorithm: change priorities dynamically – RMS does not require splitting the code • Sporadic events – RMS only runs them when they arrive • Processes with long period but short deadline – Can run a variant of RM when Di Ti Deadline monotonic • Dealing with overruns – RMS makes lower priority processes suffer most Undergraduate course on Real-time Systems Linköping University 17 of 34 Autumn 2010 Undergraduate course on Real-time Systems Linköping University 18 of 34 Autumn 2010 3 Earliest deadline first (EDF) Process sets • Event that leads to release of process Pi appears with minimum inter-arrival interval Ti • Pi has a max computation time Ci • The p process must be finished before its deadline Di Ti • Processes are independent (do not share resources) • Online • Preemptive • Dynamic D i priorities i iti Policy: Always run the process that is closest to its deadline Undergraduate course on Real-time Systems Linköping University • EDF: The process with nearest absolute deadline (di) will run first Undergraduate course on Real-time Systems Linköping University 19 of 34 Autumn 2010 20 of 34 Autumn 2010 Example (3) Consider following processes: P1 WCET (Ci) 5 Deadline (Di = Ti) 20 Arrival times (ri) 0, 20,... 0, P2 10 12 12,... Compare to RMS For same task set: WCET (Ci) Deadline (Di = Ti) Arrival times (ri) P1 5 20 0 20,... 0, 20 P2 10 12 0 12,... 0, 12 ...? Preemption 0 10 15 20 time 25 Undergraduate course on Real-time Systems Linköping University 21 of 34 Autumn 2010 0 10 15 ...? 20 Undergraduate course on Real-time Systems Linköping University Theorem A set of periodic tasks P1,...,Pn for which Di = Ti is schedulable with EDF iff U= C1/T1+...+Cn/Tn time 25 22 of 34 Autumn 2010 Example (4) Consider following task set: WCET (Ci) Deadline (Di = Ti) P1 2 5 P2 4 7 Is it schedulable? U = 2/5 + 4/7 = 0,97 For Example 3: C1/T1 + C2/T2 = 5/20 + 10/12 = 1,08! Undergraduate course on Real-time Systems Linköping University Yes! 23 of 34 Autumn 2010 Undergraduate course on Real-time Systems Linköping University 24 of 34 Autumn 2010 4 EDF vs. RMS Sharing resources • EDF gives higher processor utilisation (Example 4 not schedulable with RMS!) • EDF has simpler exact analysis for the mentioned type of task sets • RMS can be implemented to run faster at run-time (if we ignore the time for context switching) Undergraduate course on Real-time Systems Linköping University • Assume that processes synchronise using semaphores • We schedule the processes with fixed priorities but relax the independence requirement i t Undergraduate course on Real-time Systems Linköping University 25 of 34 Autumn 2010 26 of 34 Autumn 2010 Priority Inversion How to avoid it? • A low priority process (P1) locks the resource • A high priority process (P2) has to wait on the semaphore (blocked state) • A medium priority process (P3) preempts P1 and runs to completion before P2! Undergraduate course on Real-time Systems Linköping University • When P2 is blocked by P1 one raises the priority of P1 to the same level as P2 temporarily • Afterwards, when the semaphore is released l db by P1, it goes back b k to t its it prior i priority level • P3 can not interrupt P1 any more! Undergraduate course on Real-time Systems Linköping University 27 of 34 Autumn 2010 28 of 34 Autumn 2010 Priority inheritance • Guarantees upper bound for blocking time, since high priority process P2 is blocked only under the time that P1 uses the resource • Is I transitive t iti Example Let P1 have lower priority than P2. Preemption S1 P1 S1? Blocked But ... Does not avoid deadlock! Undergraduate course on Real-time Systems Linköping University Blocked S2? S2 P2 Inheritance time 0 Here Si denotes the process locks semaphore Si. 29 of 34 Autumn 2010 Undergraduate course on Real-time Systems Linköping University 30 of 34 Autumn 2010 5 Ceiling Protocols Terminology e.g. Immediate priority Ceiling Protocol (ICP): Note that: • blocked – when waiting for a resource (other than CPU) • not dispatched or preempted when waiting for CPU Undergraduate course on Real-time Systems Linköping University • A process that obtains its first resource inherits the resource’s ceiling priority - the highest priority among all processes that can possibly p y claim that resource • Dynamic priority for a process is the max of own (fixed) priority and the ceiling values of all resources it has locked • When a resource is released, the process priority returns to the normal level (or to another engaged resource’s ceiling) 31 of 34 Autumn 2010 Undergraduate course on Real-time Systems Linköping University Properties • A process is blocked max once by another process with lower priority 32 of 34 Autumn 2010 ICP & Deadlock • The ICP prevents deadlocks (How?) We will prove that! • Moreover, it prevents starvation (How?) • The blocking delay is a function of the length of the critical section • Do not even need to use semaphores! Undergraduate course on Real-time Systems Linköping University 33 of 34 Autumn 2010 Undergraduate course on Real-time Systems Linköping University 34 of 34 Autumn 2010 6