Process Scheduling for RTS Dr. Hugh Melvin, Dept. of IT, NUI,G RTS Scheduling Approach RTS typically control multiple parameters concurrently Eg. Flight Control System Speed, altitude, inclination etc.. Eg. Power Station Temperature, Pressure, Flowrate Options Use a single process Simple/limited approach No OS required Use multiple processes More complex but more flexible/scalable Use threads Light weight relative to processes Dr. Hugh Melvin, Dept. of IT, NUI,G Cyclic Executive Approach Single Process while(){ Task ; Task ;.... Task n; } No Operating System No scheduler Manually construct cycle schedule Encapsulate all tasks within single infinite loop Dr. Hugh Melvin, Dept. of IT, NUI,G 3
Cyclic Executive Ok for very well defined / periodic tasks with bounded execution times Need to ensure that tasks cannot block and halt all others Recall: different parameters being controlled may have different physical characteristics Eg. Temperature, pressure, voltage in power station Different tasks require different sample rates and response times Above example All tasks run at same frequency Overall cycle will run as fast as processor can handle tasks May need to slow tasks down to meet particular RTS reqds Dr. Hugh Melvin, Dept. of IT, NUI,G 4 Cyclic Executive Possible Strategies Run as fast as required by highest freq task Use lower harmonics for remaining tasks Possible use of counters to control sequence Use of major and minor cycles Eg. Highest freq Task is 00 Hz Other tasks at 50Hz, 5 Hz etc Possible use of timers/interval timers to correctly schedule tasks Dr. Hugh Melvin, Dept. of IT, NUI,G 5 Cyclic Executive Task Set Major Cycle = 00 Minor Cycle = 5 Use interval timer interrupts to enable scheduler to loop through minor cycles Manually construct schedule to meet criteria Task A B C D E Period p msec 5 5 50 50 00 Exec Time msec 0 8 5 4 Dr. Hugh Melvin, Dept. of IT, NUI,G 6
CE Time Line Interrupts generated every 5 msec eg. via interval timer INT INT INT INT A B C D E Dr. Hugh Melvin, Dept. of IT, NUI,G 7 loop wait_for_int task_a task_b task_c wait_for_int task_a task_b task_d task_e wait_for_int task_a task_b task_c wait_for_int task_a task_b task_d end loop CE Pseudocode Dr. Hugh Melvin, Dept. of IT, NUI,G 8 Cyclic Executive Major cycle must be multiple of minor cycle All tasks share common address space Can pass data easily Little/no need for data protection (eg via semaphores/mutex) Only one task operates at any time no concurrent access possible Large tasks may need to be subdivided to facilitate/meet overall schedule adds to complexity Dr. Hugh Melvin, Dept. of IT, NUI,G 9 3
Cyclic Executive Requires very precise knowledge of RTS Sample rate/response time for parameter controlled by each task WCET for each task bounded Once well constructed schedulability test not needed Difficulty in constructing static schedule! Limited scope for aperiodic tasks eg. Interrupts Inflexible Adding a new task may involve a lot of work Hardware specific very limited portability Dr. Hugh Melvin, Dept. of IT, NUI,G 0 Cyclic Executive Moneypoint AS Station Example Cyclic executive approach Each AS station handles well defined tasks Execution time of each task precisely known Task schedule constructed Ensure total schedule runtime will meet sample rate/response time of each parameter being controlled Slack time in each cycle built in for interrupt processing If total task time exceeded repeatedly, timeout fault Dr. Hugh Melvin, Dept. of IT, NUI,G Multiple Process Approach Allows each parameter within RTS to be controlled by separate process Advantages Each process is much more simple as handling a small subset of overall RTS More robust and flexible Facilitates process priorities More scalable Can run on multiprocessor machine Modular Individual process code more portable Protected Each process operates in isolation with dedicated memory less risk of overall system failure Dr. Hugh Melvin, Dept. of IT, NUI,G 4
Multiple Process Approach Downside Complexity Requires an OS to schedule multiple processes Task priorities may need to be assigned More memory reqd for process overhead Context switch overhead May be slower Communication between processes has to be explicitly done (advantage!) Impact of complexity on reliability? Operating envelope for a RTS can usually be very well defined Benefits of MP approach achieved through complexity often not that relevant CE approach still widespread within Hard RTS Dr. Hugh Melvin, Dept. of IT, NUI,G 3 Multiple Process Generic Process States Dormant : Process created but not eligible to execute Ready: Process released and eligible to execute but not doing so May be preempted May have been blocked for resources move to ready May have reached end of timeslice Executing: Process being executed Suspended (Blocked): Process waiting for resource to be freed or self-suspended Terminated: Process finished execution Dr. Hugh Melvin, Dept. of IT, NUI,G 4 Process State Diagram Ready Schedule Task Pre-empt or Timeslice allocated Preempted, timeslice up Dormant Executing Resource freed Blocked/ Self suspended Resource freed Blocked Process terminated Terminated Dr. Hugh Melvin, Dept. of IT, NUI,G 5 5
Scheduling for RTS Fundamental OS function for any Computer System Fairness and time-sharing are usual scheduling objectives of non RTS Scheduling much more critical for RTS Allocating resources and scheduling processes to ensure that deadlines are met Note Terminology Confusion In CE approach, we saw that multiple tasks control multiple parameters and are contained within a single process In MP approach, we saw that multiple processes, each perhaps controlling a single parameter coexist using an OS However, much of literature on schedulability analysis for MP systems refers to tasks rather than processes should be clear from the context Dr. Hugh Melvin, Dept. of IT, NUI,G 6 Scheduling for RTS A schedule is feasible iff all the tasks/processes start after their release time and complete before their deadlines Scheduling Policy may be determined Pre-run-time Schedule created offline Not unlike Cyclic Executive approach Run-time Schedule determined online as tasks arrive Needs to be done quickly! Can have static or dynamic process priority Dr. Hugh Melvin, Dept. of IT, NUI,G 7 Scheduling for RTS Run-time Static versus Run-time Dynamic Priority Static Priority Scheduling Alg. Task priority does not change Rate Monotonic Alg. Dynamic Priority Scheduling Alg. Priority can change over time Earliest Deadline First (EDF) Preemptive versus non-preemptive Preemptive Schedule Task can be preempted by other tasks Penalty of context switches Non preemptive Task runs to completion unless blocked over resource Dr. Hugh Melvin, Dept. of IT, NUI,G 8 6
Task Characteristics T i Precedence Constraints Other tasks reqd before task T i can run Release Time of task T i = r i Phase Φ i of task T i = release time of st instance of task T i Execution time e i of task T i = time (worst case) for task to complete (WCET) Period p i = interval between consecutive instances of task T i (presuming periodic) Absolute Deadline D i = instant by which task must be complete Relative Deadline d i.. Relative to release time r i Dr. Hugh Melvin, Dept. of IT, NUI,G 9 Task Characteristics T i p i d i e i r i Φi D i Dr. Hugh Melvin, Dept. of IT, NUI,G 0 Simplifications All tasks are periodic Relative deadline is equal to period No precedence constraints No task has any non-preemptible sections Cost of preemption is zero Non CPU resources are infinite eg. Memory / I/O limited use in the real world! Dr. Hugh Melvin, Dept. of IT, NUI,G 7
Rate Monotonic Scheduling Run time, Static priority and Preemptive Priority inversely related to period Eg. Given task T i and T j where p i < p j Priority of task T i greater than T j In real world, the more critical RTS parameters tend to require faster sample rate/response times of processes controlling those parameters RM is a good match in this regard Scheduling decision made when Current task execution complete New task released n Recall task T i utilisation u i = e i / p i Overall CPU Utilisation U = u i i= Dr. Hugh Melvin, Dept. of IT, NUI,G RM Example Task T T T 3 e 5 p 4 5 0 u 0.5 0.4 0.5 All Tasks released at time 0 Priority T > T > T 3 Overall U = 0.9 Sequence st inst Task runs to completion st inst Task runs to completion st inst Task 3 runs for unit..at Eu=4, Task released preempts Task 3 nd inst Task runs to completion..at Eu =5, Task released nd inst Task runs to completion st inst Task 3 runs for unit.. At Eu = 8, Task released preempts Task3 3 rd inst Task runs to completion st inst Task 3 runs for unit.. At Eu = 0, 3 rd inst of Task released preempts 3.. At Eu = 5, st inst Task 3 completes.. CPU idle Eu 8-0 At Eu = 0, all 3 tasks released.. Cycle repeats 3 3 3 3 3 0 3 4 5 6 7 8 9 0 3 4 5 6 7 8 9 0 Execution Units Eu Dr. Hugh Melvin, Dept. of IT, NUI,G 3 RM Scheduling General schedulability test If U <= n( /n -) where n = no of tasks RM will definitely produce feasible schedule No need for further analysis However RM may produce feasible schedule where U > n( /n -) i.e. Sufficient but not necessary condition Recall Example: CPU U = 0.9 but still schedulable Depends on particular task characteristics If U > n( /n -) need to perform further schedulability analysis As n increases, bound 69% Dr. Hugh Melvin, Dept. of IT, NUI,G 4 8
Utilisation Bound for RM Alg. Bound 0.83 0.78 0.73 0.69 3 4 5 6 0 No of Tasks Dr. Hugh Melvin, Dept. of IT, NUI,G 5 Eg. Taskset T T T 3 T 4 where p < p < p 3 < p 4 Task Highest priority.. never preempted Will run immediately once released For Task to be feasibly scheduled Only condition is that e <= p Include Task in task set Can only preempted by Task Will be executed iff can find sufficient time e over period [0, p ] Say Task completes at time t within [0, p ] How many times did Task run over [0,t]? Each iteration must also complete in this interval [0,t] Dr. Hugh Melvin, Dept. of IT, NUI,G 6 Over interval [0,t], Task completes Must satisfy condition t = e + e t p i Need to find t over interval [ 0, p ] Only need to check this when new instances of task are released Find integer k such that: k p >= k e + e k p <= p t p i Dr. Hugh Melvin, Dept. of IT, NUI,G 7 9
Include Task 3 Can be preempted by Task and Need to find t over [0,p 3 ] such that = t p e + Need to check only at multiples of p and/or p Similar analysis for Task 4 Can be preempted by Task,,3 t e e t p 3 + Dr. Hugh Melvin, Dept. of IT, NUI,G 8 General Rule W i (t) = i e t j p j= j = total work carried out by tasks T T T 3... T i initiated in interval [0,t] If W i (t) <= t, then schedule is feasible (W i (t) / t) <= W i (t) only changes at finite no of points when tasks are released Check points defined by p τ = = = i i lp j j,.., i; l,.., p j Dr. Hugh Melvin, Dept. of IT, NUI,G 9 Consider Task set Check points W: {00} W: {00,80} W3: {00,80,00,40} W4: {00,80,00,40,300,360,400} i 3 4 e i 0 30 80 00 p i 00 80 40 400 n = 4 n( /n -) = 0.76 Note U = 0.95 (0.+0.66+0.33+0.5) further analysis Dr. Hugh Melvin, Dept. of IT, NUI,G 30 0
Task is RM Schedulable iff e <=00 (True) Task / is RM Schedulable iff e + e <= 00 or.(true: 50) e + e <= 80. (True: 70) Task //3 is RM Schedulable iff e + e + e 3 <= 00 or. (False: 30) e + e + e 3 <= 80 or.. (True:50) e + e + e 3 <= 00 or (True:80) 3 e + e + e 3 <= 40. (True:00) Dr. Hugh Melvin, Dept. of IT, NUI,G 3 Task //3/4 is RM Schedulable iff e + e + e 3 + e 4 <= 00 or. (False:30) e + e + e 3 + e 4 <= 80 or.. (False:50) e + e + e 3 + e 4 <= 00 or (False:80) 3e + e + e 3 + e 4 <= 40 or. (False:300) 3e + e + e 3 + e 4 <=300 or. (False:380) 4e + e + e 3 + e 4 <=360 or. (False:400) 4e + 3e + e 3 + e 4 <=400. (False:430) By includingtask 4, not RM schedulable Can also plot results Check whether W i (t) falls on or below W i (t) =t line Dr. Hugh Melvin, Dept. of IT, NUI,G 3 i W i (t) = e t j p j= j W Interval [0,00] W (t) = e = 0 W : checkpoints{00,80} Interval [0,00] ; W (t) = e =0() +30() = 50 Interval [0,80] ; W(t)= e 0() +30() = 70 00 + e 00 p p 80 + e 80 p p Dr. Hugh Melvin, Dept. of IT, NUI,G 33
W3 : checkpoints{00,80,00,40} W 3 (t)= e t + e t + e p 3 p Interval [0,00] W 3 (t) = 0() +30() +80() = 30 Interval [0,80] 0() + 30()+ 80() = 50 Interval [0,00] 0() + 30() + 80() = 80 Interval [0,40] 0(3) + 30() + 80() = 00 p3 t Dr. Hugh Melvin, Dept. of IT, NUI,G 34 W 00 00 Wi(t) =t 70 50 W 0 Time 00 Time 00 80 Dr. Hugh Melvin, Dept. of IT, NUI,G 35 W3 Wi(t) =t 430 W4 00 80 50 30? Wi(t) =t 00 Time 80 40 Time 400 Dr. Hugh Melvin, Dept. of IT, NUI,G 36
Sporadic Tasks So far have only considered periodic tasks unrealistic Can view sporadic task as infrequent periodic task if can specify Minimum interarrival time between release of successive sporadic tasks Maximum execution time Simply treat as additional task in RM analysis Dr. Hugh Melvin, Dept. of IT, NUI,G 37 Earliest Deadline First (EDF) Run-time, Dynamic and Preemptable Ready task whose absolute deadline is the earliest is given highest priority Task priorities are re-evaluated when tasks released/completed EDF is an optimal uni-processor sched alg If all tasks are periodic n Task n ; CPU U = u i i= If U <=, then task set is EDF schedulable Dr. Hugh Melvin, Dept. of IT, NUI,G 38 Task T T T 3 EDF Example e 5 p 4 5 0 u 0.5 0.4 0.5 All Tasks released at time 0 Overall U = 0.9 Sequence st inst Task runs st as earliest deadline of 4 st inst Task runs to completion st inst Task 3 runs for unit..note: Deadline is 0..at Eu=4, Task rel. preempy Task 3 as deadline is 8 nd inst Task runs to completion..at Eu =5, Task released nd inst Task runs to completion as deadline is 0 st inst Task 3 runs for unit.. At Eu = 8, Task released preempts Task3 3 rd inst Task runs to completion st inst Task 3 runs for unit.. At Eu =0, Task rel.. Preempts task 3 as deadline is 5.. At Eu =, Task runs as deadline 6 < 0 At Eu =5 Task released and runs with deadline 0 At Eu =6 Task rel with deadline 0 no preemption 3 3 3 3 3 0 3 4 5 6 7 8 9 0 3 4 5 6 7 8 9 0 Execution Units Eu Dr. Hugh Melvin, Dept. of IT, NUI,G 39 3
Task T T T 3 EDF Example e p 3 5 0 u 0.33 0.4 0. All Tasks released at time 0 Overall U = 0.93 Sequence st inst Task runs st as earliest deadline of 3 st inst Task runs to completion.. At Eu=3, nd inst Task rel & runs as deadline is 6 st inst Task 3 runs..at Eu=5, Task rel. doesn t preempt Task 3 as deadline is also 0.. Task 3 completes.. At Eu = 6, Task released deadline 9 runs.. At Eu = 7 Task runs to completion.. At Eu =9, Task rel deadline..runs.. At Eu =0, Task 3 rel. deadline 0, Task also rel with deadline 5 Task runs.. At Eu =5, Task and released with deadlines 6,0 Task runs.. At Eu=6, Task runs.. At Eu = 8 Task rel with deadline but Task 3 has deadline 0 Task 3 runs 3 3 3 3 3 3 0 3 4 5 6 7 8 9 0 3 4 5 6 7 8 9 0 Execution Units Eu Dr. Hugh Melvin, Dept. of IT, NUI,G 40 EDF vs RM With RM, priorities fixed Lowest period tasks guaranteed proc time Only higher period tasks will miss deadlines In overload conditions, same lower priority tasks lose out Bound on CPU utilisation must be considered Necessary but not sufficient EDF, dynamic priority More flexible.. Less predictable In overload conditions, Aall tasks may miss deadlines Schedulable if CPU U <= Dr. Hugh Melvin, Dept. of IT, NUI,G 4 Conventional OS Scheduling Time Sharing Emphasis Balance needs of interactive and CPU intensive traffic Complex alg Constantly adjust process priority CPU intensive processes lose priority as they run As a process waits, priority is increased will get CPU slot sooner Unix command nice nice -0 process_high_priority nice +0 process_low_priority nice will work but underlying sched alg unchanged Windows RealTime Threads Somewhat equivalent Dr. Hugh Melvin, Dept. of IT, NUI,G 4 4