Embedded Systems Introduction to RealTime Systems Laboratory for
Embedded Systems Introduction to Real-Time Systems Laboratory for Perceptual Robotics – Department of Computer Science
Real-Time Systems Definition: Real-time systems are those in which the correctness of the system depends not only on the logical results of the computation but also on the time at which the results are produced. Hard real-time systems: time critical - it is imperative that the system respond within a specified time period. Soft real-time systems: the system will function correctly, although at a lower overall utility if deadline are occasionally missed. Laboratory for Perceptual Robotics – Department of Computer Science 2
Real-Time Systems timing constraints: periodic --- once period T, precisely T units apart aperiodic --- start time, finish time, or both Laboratory for Perceptual Robotics – Department of Computer Science 3
Real-Time Programming Concurrent, task-based approach • multi-tasking RTOS (Vx. Works) • scheduling theory • industrial state-of-the-art Synchronous approach • reactive kernel represented as an automata • formal properties • special-purpose programming languages - Esterel • Discrete-Event Dynamic Systems Laboratory for Perceptual Robotics – Department of Computer Science 4
Logical Concurrency A concurrent program can be viewed as a collection of autonomous sequential processes executing (logically) in parallel. Each process has a single thread of control. multiplex on a single processor; • multiplex on a multi-processor with shared memory; or • multiplex on several processors without share memory (distributed systems) Laboratory for Perceptual Robotics – Department of Computer Science 5
Processes A process (task) can be in one of the following process states: Ready: Processes that are ready to execute. Stored in a Ready. Queue (a linked list) Running: The currently executing process (the first process in the Ready. Queue) Blocked: Processes waiting for an event, e. g. , an external event (Temp < Tmin) The switch from one running process to another is called a context switch The process state consists of: • program counter, • process status information (priority, enabled interrupts), and • the contents of the programmable registers. Process administration is handled by the RT kernel Laboratory for Perceptual Robotics – Department of Computer Science 6
Interactive C - Multitasking process - a function with a unique pid, local program stack (256 bytes default) for return addresses, local variables, arguments, etc, and a processor allocation in milliseconds (default 5 msec)---processes communicate via global variables process table - a set of processes that are logically concurrent Laboratory for Perceptual Robotics – Department of Computer Science 7
Handy. Board Process Control • int start_process(function-call(…), [TICKS], [STACK-SIZE]) returns the process id • int kill_process(int pid) returns 0 if successfully killed, 1 otherwise (process not found) • kill_all() stops all running processes • ps processor status • void hog_processor() allocates an additional 256 msec to the function currently running-called repeatedly, system will wedge requiring reset • void defer() Forces a context shift Laboratory for Perceptual Robotics – Department of Computer Science 8
Example: Multitasking void check_sensor(int n) { while(1) printf(“sensor %d is %dn”, n, digital(n)); } void main() { starts check_sensor() int pid; and then kills it pid = start_process(check_sensor(2)); 1 second later sleep(1. 0); kill_process(pid); } Q: how often does check_sensor() actually run? Laboratory for Perceptual Robotics – Department of Computer Science 9
Example: Multitasking void check_sensor(int n) { while(1) printf(“sensor %d is %dn”, n, digital(n)); } void main() { starts check_sensor() int pid; and then kills it pid = start_process(check_sensor(2)); 1 second later sleep(1. 0); kill_process(pid); } Q: how often does check_sensor() actually run? A: assuming nothing else is in the process table, this process runs ~continuously in about 200 consecutive 5 msec chunks. Laboratory for Perceptual Robotics – Department of Computer Science 10
Scheduling. . . to ensure that the run-time scheduler dispatches the runnable processes in an order that will result in all deadlines being met… orders the Ready. Queue allocates priorities BOTH STATIC AND DYNAMIC PRIORITY ALLOCATIONS ARE POSSIBLE Laboratory for Perceptual Robotics – Department of Computer Science 11
Scheduling Operations Research • job shop scheduling and shop-flow problems • schedule physical devices and processes - machines, workcells, maintenance, orders, batches, projects • mostly (not exclusively) static schedules Computer Science • schedule tasks in a uni- or multiprocessor environment • mostly (not exclusively) dynamic schedules Laboratory for Perceptual Robotics – Department of Computer Science 12
Ok, back to work! Laboratory for Perceptual Robotics – Department of Computer Science 13
Scheduling-Problem Formulation Events - anything that requires a response in the system---computation, work, communication • periodic, aperiodic, sporadic, and stochastic events • interrupts Time - execution times for event handling---how much time will it take to handle an event? • How much CPU time does a periodic process require? • How much time will it take to change a bearing on the milling machine? Deadline • upper bound on the time required to service an event Scheduling • reflects decisions about which event to process at a given time Laboratory for Perceptual Robotics – Department of Computer Science 14
Priority-Based Preemptive Scheduling How should processes be scheduled in order to maximize the utilization of the system while guaranteeing deadlines? priority reflects the severity of the time demands high low 0 255 The Ready. Queue is sorted by priority: A blocked process that becomes ready and has higher priority than the currently running process causes a context shift. This task preempts the running task. Laboratory for Perceptual Robotics – Department of Computer Science 15
Priorities and Scheduling Priority Inversion Consider tasks T 1, T 2, and T 3 with corresponding priorities P 1>P 2>P 3 (that is, task T 1 has the highest priority). If task T 1 depends on task T 3, it may be suspended in order to wait for a result from T 3. However, a priority-based preemptive scheduler will choose to run task T 2, thus further blocking task T 1 indirectly by way of task T 3 even though P 1>P 2. Laboratory for Perceptual Robotics – Department of Computer Science 16
Priorities and Scheduling Priority Inheritance The priority of a process is the max of its priority and the priorities of all other processes that depend on it. therefore: task T 3 inherits priority P 1 while T 1 is waiting for a T 3 event T 3 will run in preference to T 2. For example, a synchronous communication protocol (rendezvous) requires the sender to wait until contact is established and the receiver is verified before the message is sent. In ADA, the priority of the rendezvous itself is the highest priority of the two participating processes. Laboratory for Perceptual Robotics – Department of Computer Science 17
Schedulability Given a single processor, a set of n-independent, periodic processes with only execution time deadlines (no start/stop constraints) are schedulable if the overall processor utilization is less than n(21/n-1) For large n this function approaches a utilization of 0. 693. This is a very conservative estimate, based on worst-case relationships between the periods of the processes. Liu and Layland (1973) Laboratory for Perceptual Robotics – Department of Computer Science 18
Schedulability Given a single processor, a set of n-independent, periodic processes with only execution time deadlines (no start/stop constraints) are schedulable if the overall processor utilization is less than n(21/n-1) non-repeating prime # periods for n processes For large n this function approaches a utilization of 0. 693. This is a very conservative estimate, based on worst-case relationships between the periods of the processes. Liu and Layland (1973) Laboratory for Perceptual Robotics – Department of Computer Science 19
Schedulability Consider two tasks, T 1 and T 2, with P 1>P 2 (T 1 is more “important”). T 1 has a period of 50 units of time and requires 10 units of time to run. Task T 2 has a period of 10 units of time and requires 2 units of time to run. Under a priority-based preemptive schedule, T 1 will execute first and when it completes, T 2 will have already missed its first deadline! The problem is related to the use of “preference” priorities. Laboratory for Perceptual Robotics – Department of Computer Science 20
Schedulability Consider two tasks, T 1 and T 2, with P 1>P 2 (T 1 is more “important”). T 1 has a period of 50 units of time and requires 10 units of time to run. Task T 2 has a period of 10 units of time and requires 2 units of time to run. schedulability n(21/n-1) 2(21/2 -1) = 0. 818 Under a priority-based preemptive schedule, T 1 will execute first and when it completes, T 2 will have already missed its first deadline! The problem is related to the use of “preference” priorities. Laboratory for Perceptual Robotics – Department of Computer Science 21
Schedulability Consider two tasks, T 1 and T 2, with P 1>P 2 (T 1 is more “important”). T 1 has a period of 50 units of time and requires 10 units of time to run. Task T 2 has a period of 10 units of time and requires 2 units of time to run. schedulability n(21/n-1) 2(21/n-1) = 0. 818 utilization P 1 P 2 10 + (5 x 2) 50 =40% Under a priority-based preemptive schedule, T 1 will execute first and when it completes, T 2 will have already missed its first deadline! The problem is related to the use of “preference” priorities. Laboratory for Perceptual Robotics – Department of Computer Science 22
Schedulability Consider two tasks, T 1 and T 2, with P 1>P 2 (T 1 is more “important”). T 1 has a period of 50 units of time and requires 10 units of time to run. Task T 2 has a period of 10 units of time and requires 2 units of time to run. schedulability n(21/n-1) 2(21/n-1) = 0. 818 utilization P 1 P 2 10 + (5 x 2) 50 =40% Threshold utilization for worst case schedulability 0. 818 > 0. 40 actual utilization …so we know it’s schedulable… Laboratory for Perceptual Robotics – Department of Computer Science 23
Rate Monotonic Scheduling Assign fixed priorities inversely proportional to the period for the tasks Pi ~ 1/ i, so that the shorter the period, the higher the priority. optimal static schedule for independent periodic processes: if a process can be scheduled by any static scheduling algorithm, then it can also be scheduled by the rate monotonic algorithm. Laboratory for Perceptual Robotics – Department of Computer Science 24
Extensions to Rate Monotonic Scheduling Soft RT Systems - if all deadlines cannot be met, the most “important” processes must be run first. If the Ready. Queue is schedulable, then the process with the shortest period should be first. “Specification Aliases” - transforming specifications for the most important tasks. If T 1 ( =50, and execution time=10) were respecified e. g. , T 1'( =5, and execution time=1), then relative preference is consistent with its rate-based priority. …the details are non-trivial… Periodic Servers - incorporate aperiodic, interupts, and important soft-deadline tasks into the rate monotonic schedule. reserve time in the periodic server to handle worst-case arrival rates global schedule---even for tasks with random arrival rates! at the expense of utilization Laboratory for Perceptual Robotics – Department of Computer Science 25
Other Scheduling Algorithms Earliest Deadline - need to know about deadlines Least Slack Time - need deadlines and complete times when deadlines can be met, ED and LST are equivalent to Rate Monotonic Laboratory for Perceptual Robotics – Department of Computer Science 26
Periodic Tasks Revisited A feedback control process is the canonical periodic task with a fixed period---the sampling rate. between sampling intervals, the system is ballistic Laboratory for Perceptual Robotics – Department of Computer Science 28
Periodic Tasks Revisited in practice, the control delay is not constant--variations are due to: • preemptions from higher priority tasks • variation in the execution times (branching, data dependencies) • communication delays in distributed controllers jitter can result from: • preemptions from higher priority tasks • improper useage of timing primitives in the RT kernel Laboratory for Perceptual Robotics – Department of Computer Science 29
- Slides: 28