CS4514 Real Time Scheduling Jose M. Garrido Fall 2015 Department of Computer Science 1
Periodic Tasks Typical real-time application has many tasks that need to be executed periodically Reading sensor data Computation Sending data to actuators Communication Standard formulation: Given a set of tasks T 1, T n. Each task T i has Period P i Computation time C i Deadline D i 2
Schedulability Problem Can all the tasks be scheduled so that every task Ti can execute in the CPU for Ci units in every interval of length Pi? If yes, then the schedule is said to be feasible, Assume that these tasks execute on the same processor. 3
Standard (non-rt) Scheduling Policies FCFS SPN RR SRTF PS PPS Others 4
Periodic Tasks Example 1: Task T1 with period 10 and compute time 3 Task T2 with period 10 and compute time 1 Task T3 with period 15 and compute time 8 Possible schedule: repeats every 30 sec T1 from 0 to 3, 12 to 15, 24 to 27 T2 from 3 to 4, 15 to 16, 27 to 28 T3 from 4 to 12, 16 to 24 Simple test: Task T i needs to use CPU for C i /P i fraction per unit Utilization = Sum of C i /P i Task set is schedulable if and only if utilization is 1 or less. 5
RT Scheduling Example2 Pi has (c, p, d) Two processes: P1 (1,2,2) and P2 (2, 5, 5) priority (P1) > priority (P2) No preemption In this schedule, there is 1 unit of idle time Schedule can be repeated at time t = 10 6
RT Scheduling Example3 If the priorities are reversed, prio(p2) > prio(p1) P2 starts at t = 0 P2 will compute for 2 time units, which is the entire first cycle of P1 P1, will therefore, miss its deadline This schedule is not feasible. 7
RR with Preemption Example 4 Two processes: P1 (1, 2, 2) and P2 (2, 5, 5) The time slice = 1 Assume that P2 starts first There is 1 time unit of CPU idle time 8
RR Scheduling Example 5 Two processes: P1 (3, 4, 4) and P2 (2, 8, 8) using RR scheduling with time slice = 1 time units. At time t = 4, P1 has executed only 2 time units, therefore misses its deadline 9
RT Scheduling Example 6 Two processes: P1 (3, 4, 4) and P2 (2, 8, 8) using priority scheduling. P1 has higher priority than P2. P2 is preempted by P1 at t = 4 10
Real-Time Scheduling Fixed Priorities Rate Monotonic Scheduling (RMS) Least Compute Time First (LCT) Dynamic priorities Earliest Deadline First (EDF) Shortest Completion Time (SCT) Least Slack Time (LST) 11
Rate Monotonic Scheduling Based on fixed priorities The shorter the period of the task, the higher the priority assigned Example: P1 = (3, 4, 4) P2 = (2, 8, 8) 12
RM Scheduling Example 7 P1 = (3, 4, 4) higher priority P2 = (2, 8, 8) 13
Least Compute Time (LCT) Also based on fixed priorities The shorter the compute time of the task, the higher the priority assigned Example: P1 = (3, 4, 4) P2 = (2, 8, 8) P2 will have higher priority. P1 will missed its first deadline A LCT schedule is not feasible, for this workload 14
RT Scheduling with Dynamic Priorities Earliest Deadline First (EDF) Shortest Completion Time (SCT) Least Slack Time (LST) These scheduling techniques can change the priorities of processes at period boundaries 15
Earliest Deadline First Scheduling: EDF Based on dynamic priorities. Choose the task with the earliest (current) deadline Preemptive: scheduling decision made when a task finishes as well as when a new task arrives Theorem: If there is a possible schedule, then EDF will find one Example: Two processes: P1 (2, 4, 4) and P2 (5, 10, 10) 16
EDF Example 8 P1 (2, 4, 4) and P2 (5, 10, 10) At times t=4 and t=12, P2 is preempted 17
EDF Example 2 Task T1 with period 10 and CPU time 3 Task T2 with period 10 and CPU time 1 Task T3 with period 15 and CPU time 8 T1 T2 T3 T3 T1 T2 T3 T1 T2 T3 0 3 4 10 12 1516 20 2324 28 18
Scheduling Algorithm: RMS Rate Monotonic Scheduling (Liu and Layland, 1973) Based on static priorities. Preemptive: scheduling decision made when a task finishes as well as when a new task arrives Scheduling algorithm: Choose the task with smallest period (among ready tasks) It may happen that a set of tasks is schedulable by EDF, but not by RMS Theorem: If utilization is smaller than 0.7, then RMS is guaranteed to find one If utilization is between 0.7 to 1, RMS may or may not succeed Example: 19
SCT Scheduling Priority is given to the process with the shortest remaining compute time Example: P1 (1,2,2) and P2 (2, 5, 5) 20
Scheduling Sporadic Processes 21
Priority Inversion Can occur when process interactions are included in the model Process synchronization Direct communication Manipulate shared data Utilize non-preemtable resources A major issue is blocking: suspension to force waiting of a process interacting with another process A low priority process can cause a higher priority process to block for an unbounded time interval 22
The Priority Inversion Problem Priority order: T1 > T2 > T3 failed attempt to lock R lock(r) unlock(r) T1 T2 lock(r) unlock(r) T3 T2 is causing a higher priority task T1 to wait! 23
Priority Inversion 1. T1 has highest priority, T2 next, and T3 lowest 2. T3 comes first, starts executing, and acquires some resource (say, a lock). 3. T1 comes next, interrupts T3 as T1 has higher priority 4. But T1 needs the resource locked by T3, so T1 gets blocked 5. T3 resumes execution (this scenario is still acceptable so far) 6. T2 arrives, and interrupts T3 as T2 has higher priority than T3, and T2 executes till completion 7. In effect, even though T1 has priority than T2, and arrived earlier than T2, T2 delayed execution of T1 8. This is priority inversion!! Not acceptable. 9. Solution T3 should inherit T1 s priority at step 5 24
Priority Inheritance Protocols Temporarily raise the priority of a process only if and when it actually blocks a higher priority process On leaving the critical section, the process priority reverts to its original value 25
Priority Inheritance Protocol lock R fails lock(r) unlock(r) T1 T2 lock(r) T2 arrives unlock(r) T3 T3 blocks T2 T3 has priority of T1 T3 directly blocks T1 26