Real-time computing
Real-time computing is the part of computer science that studies systems that must respond within strict time limits. In such a system, a result is correct only if it is logically right and also produced on time [1]. A vehicle's automatic brake controller, for example, must detect an obstacle and apply the brakes before the vehicle reaches it [2].
Each time limit is called a deadline. Missing a hard deadline can cause very serious harm, while missing a soft deadline only makes performance worse [2].
Engineers use scheduling rules and mathematical tests to show, before a system runs, that its deadlines will be met.
Correct and on time
Time is part of correctness
Real-time computing adds time to the meaning of a correct answer. Stankovic defined real-time systems as those whose correctness depends on both the logical result of a computation and the time at which the result is produced [1]. A correct value that arrives too late can still be a failure.
Not the same as fast
One misconception Stankovic addressed is that real-time computing is the same as fast computing [1]. Fast computing tries to reduce the average response time of a set of tasks, while real-time computing tries to meet the timing requirement of each individual task [2]. However short the average is, it does not guarantee that every task meets its requirement in every situation [2].
Predictability
Real-time design therefore stresses predictability, which means knowing in advance that every critical timing requirement will be met [2]. Most methods on this page exist to make timing predictable and to check it.
Where deadlines come from
Deadlines need not be stated by the user. They can be derived from the needs of the physical system being controlled [2]. Buttazzo shows this with an automatic braking system for a vehicle, such as a train, that moves along a straight line [2].
A brake control unit runs a periodic task, one that runs again at regular intervals, with period . Each run reads range sensors, computes the distance to any obstacle, and decides whether to make an emergency stop [2]. In the worst case, three delays add up before the vehicle stops [^2]:
- The detection delay runs from the moment an obstacle appears to the moment the control unit detects it. It is at most one period .
- The transmission delay runs from the moment the control unit issues the stop command to the moment the brakes start to act.
- The braking duration is the time the vehicle needs to come to a complete stop.
During the first two delays the vehicle keeps moving at full speed and covers . Braking then covers . To guarantee that no collision occurs, the obstacle's distance must exceed the total [^2]:
Here is the vehicle's speed, is the wheel-road friction coefficient, is the acceleration due to gravity, is the sensor task's period, and is the transmission delay. Solving for gives the longest safe period [^2]:
A timing requirement on the software thus follows from speed, friction, and sensor range.
Hard, firm, and soft deadlines
Deadlines are classed by what happens when one is missed [2].
| Type | Effect of a missed deadline | Example |
|---|---|---|
| Hard | May cause catastrophic consequences for the system under control | Emergency braking [2] |
| Firm | Causes no damage, but the late result is useless | Skipping a late video frame [3] |
| Soft | The late result is still useful, but performance degrades | Handling keyboard input or updating a display [2] |
A value function shows the same idea as a graph. It gives the value that a result has for the system as a function of the time the task finishes [2]. The value after the deadline differs by task type [^2]:
| Task type | Value after the deadline |
|---|---|
| Non-real-time | Stays the same; there is no deadline |
| Soft | Falls gradually |
| Firm | Drops to zero |
| Hard | Can be treated as minus infinity |
A system that mixes these classes must guarantee that every hard task meets its deadline [2]. Soft tasks get best-effort service, in which the system tries to keep their average response time short [2].
Tasks, jobs, and timing parameters
Real-time theory describes a system's work as a set of tasks. A task is code that runs again and again, and each run is called a job or instance [2]. Tasks are grouped by how their jobs arrive [^2]:
- Periodic tasks release a job at regular intervals, called the period .
- Aperiodic tasks release jobs at irregular times.
- Sporadic tasks are aperiodic tasks with a known minimum time between arrivals, which makes it possible to give them guarantees.
Each job is described by a few times [^2]:
- The arrival time (also called the release time ) is when the job becomes ready to run.
- The computation time is the processor time it needs without interruption.
- The start time and finishing time are when it first runs and when it completes.
- The absolute deadline is the time by which it must finish.
Other quantities follow from these [^2]:
- The relative deadline is the time allowed after arrival.
- The response time is the time the job actually takes.
- The lateness is a job's finishing time minus its deadline, so it is negative when the job finishes early.
The slack, or laxity, is the longest a job can wait after arrival and still finish on time if it then runs without interruption [2].
Jitter is the variation in timing between jobs of the same periodic task, such as changes in their start or finishing times [2].
For periodic tasks that all start at time 0, the schedule repeats after the hyperperiod , the least common multiple of the periods [2]. Checking one hyperperiod therefore shows the whole schedule.
Scheduling real-time tasks
A scheduler decides which ready job runs at each moment. Most real-time schedulers use preemption, which means they can suspend a running job to run a more urgent one and resume it later [2].
Cyclic executive
The simplest method fixes the whole schedule in advance. A cyclic executive, also called timeline scheduling, divides time into equal slots called minor cycles. A timer starts each slot, and a fixed table says which tasks run in it and repeats every major cycle [2].
Buttazzo's example uses three tasks, A, B, and C, that must run at 40, 20, and 10 Hz, so their periods are 25, 50, and 100 ms [2]. The minor cycle is the greatest common divisor of the periods (the largest number that divides them all evenly, here 25 ms). The major cycle is their least common multiple (the smallest number that is a whole multiple of every period, here 100 ms).
A runs in every slot, B in every second slot, and C in every fourth slot. This works only if ms and ms [2].
The method has clear trade-offs [^2]:
- It is simple and has very low overhead (extra processing time spent on managing tasks), because a main program calls the tasks in order and no scheduler switches between them.
- Task start times barely vary, so jitter is low.
- It is fragile under overload. A task that overruns its slot can break the rest of the schedule.
- It is hard to change. A longer computation time or a new frequency may force the whole table to be rebuilt.
- It handles aperiodic work poorly.
Fixed priorities
A priority-driven scheduler always runs the ready job with the highest priority. In fixed-priority scheduling, each task has one priority that all its jobs share [2]. Two rules are standard:
| Rule | Higher priority goes to | Result |
|---|---|---|
| Rate-monotonic (RM) | Shorter periods | Optimal among fixed-priority methods when deadlines equal periods (Liu and Layland, 1973) [4] |
| Deadline-monotonic | Shorter relative deadlines | Optimal among fixed-priority methods when deadlines may be shorter than periods (Leung and Whitehead, 1982) [2] |
Optimal here means that if any fixed priority assignment can schedule a set of periodic tasks, the rule can as well [4]. Liu and Layland's proof assumes independent periodic tasks with deadlines equal to periods and constant computation times, running with preemption on one processor [4].
Dynamic priorities
With dynamic priorities, a task's priority can change from one job to the next. Earliest deadline first (EDF) scheduling always runs the ready job whose absolute deadline is nearest [2].
EDF has strong optimality results on one processor with preemption. In 1974 Horn showed that it minimizes the maximum lateness of independent jobs with arbitrary arrival times, and Dertouzos showed that if any algorithm can meet all deadlines, EDF can as well [2].
These results need preemption. When preemption is not allowed, EDF is no longer optimal [2].
Comparing RM and EDF
Consider with and , and with and , which together use of the processor [2]. Under RM, preempts at , so has done only 3 of its 4 units when its deadline passes at . Under EDF, keeps running at because its deadline (7) is earlier than that of 's new job (10), and every deadline is met [2].
Buttazzo compared the two methods in detail [5] [^2]:
| Property | RM | EDF |
|---|---|---|
| Priorities | Fixed per task, by period | Dynamic per job, by absolute deadline |
| Guaranteed utilization () | , about 0.69 for large | 1 |
| Exact test () | Pseudo-polynomial (response time analysis) | : check |
| Ready-queue insertion | with one queue per priority level, if levels are few | with a heap (a tree-shaped queue) |
| Preemptions | More | Fewer |
| Permanent overload | Lower-priority tasks may never run | All tasks run at a lower rate |
The table uses these terms:
- is the number of tasks.
- means the work is fixed no matter how many tasks there are.
- means the work grows very slowly as tasks are added.
- means the work grows in proportion to .
- Pseudo-polynomial means the work also grows with the size of the task periods, not only with their number.
EDF must compute a new absolute deadline for each job, but it causes fewer preemptions. So its total operating system overhead is lower than under RM when context switches (the work of saving one task's state and loading another's) are counted [5].
Under permanent overload, Cervin and colleagues proved that with EDF each task settles to an average period of , as if every period were stretched by the same factor [5].
Schedulability analysis
Schedulability analysis checks, before a system runs, whether a set of tasks will always meet its deadlines under a given scheduler. A task set that passes is called schedulable.
Processor utilization
The processor utilization is the fraction of processor time that a set of periodic tasks needs [^3]:
Here is the computation time of task and its period. If , the tasks need more time than exists, and no algorithm can schedule them [2].
Utilization bounds
Liu and Layland proved that RM meets all deadlines if [4]
This Liu and Layland bound holds under the assumptions listed for RM above. It falls as grows: 1.000 for one task, 0.828 for two, 0.780 for three, and 0.718 for ten [2]. Its limit for large is , which is why Liu and Layland wrote that this bound may be as low as 70 percent for large task sets [4].
The bound is a sufficient test: passing it proves the set is schedulable, but failing it does not prove the opposite [2]. Many task sets above the bound still meet all deadlines.
Lehoczky, Sha, and Ding found that RM could schedule random task sets up to an average load of about 88% [2].
EDF has an exact test. Under the same assumptions, Liu and Layland proved that EDF meets all deadlines if and only if [4].
Response time analysis
Response time analysis is an exact test for fixed priorities when deadlines do not exceed periods [2]. It computes each task's worst-case response time and checks that . With tasks numbered by priority, task 1 being the highest, is the smallest solution of
The sum is the interference from higher-priority tasks. In a window of length , task releases at most jobs, each needing . The equation is solved by repeating
starting from , the computation times of task and all higher-priority tasks. The iteration stops when the value stops changing or exceeds [2].
Take three tasks with equal to (1, 4), (2, 6), and (3, 10), deadlines equal to periods, under RM. Their utilization is about 0.88, above the three-task bound of 0.780, so the utilization test cannot decide. For the iteration runs as follows:
- , unchanged
So . The same method gives and , so the set is schedulable.
For EDF with deadlines shorter than periods, the processor demand criterion plays the same role. For periodic tasks that all start at time 0 with , EDF meets all deadlines if and only if, for every [5],
Here is the processor demand, the total computation of the jobs that arrive and must finish within . The check is needed only at absolute deadlines below a computable limit, and like response time analysis it takes pseudo-polynomial time [5].
Sharing resources and priority inversion
Tasks often share data or devices. To keep shared data consistent, they use mutual exclusion: only one task at a time may run a critical section, the code that uses the resource [2]. A semaphore or a mutex (a lock that only one task can hold at a time) enforces this.
A task waiting for a lock held by a lower-priority task is blocked [2]. Some blocking cannot be avoided, but real-time analysis needs it to be bounded.
How priority inversion happens
Priority inversion is a high-priority task being blocked by a lower-priority task for an unbounded time [6]. Buttazzo's example uses tasks (high priority, H in the figure), (medium, M), and (low, L), where and share data protected by a semaphore [^10]:
- locks the semaphore and enters its critical section.
- arrives, preempts , tries to lock the semaphore, and blocks.
- , which does not use the data, arrives and preempts .
- must now wait for all of as well as the rest of 's critical section.
Disabling preemption inside critical sections solves the problem, but it only suits very short critical sections [6]. Otherwise it delays high-priority tasks that share nothing.
The Mars Pathfinder resets
Mars Pathfinder landed on 4 July 1997. A few days later, after it began gathering weather data, the spacecraft started suffering total system resets, each losing data [7]. It ran the VxWorks real-time operating system, which schedules tasks by priority with preemption [7].
The cause was priority inversion. A high-priority bus management task and a low-priority weather data task shared an "information bus" guarded by a mutex, and on rare occasions a long-running medium-priority communications task preempted the weather task while it held the mutex and the bus task waited [7]. A watchdog timer then noticed that the bus task had not run for some time and reset the whole system [7].
The fix was a single setting. A VxWorks mutex takes a parameter at creation that turns priority inheritance (explained below) on or off, and this mutex had been created with it off [7]. JPL engineers uploaded a short C program that turned it on, and the resets stopped [7].
Resource access protocols
Sha, Rajkumar, and Lehoczky published the priority inheritance protocol (PIP) in 1990 [7]. Under PIP, when a task blocks on a resource, the task holding it inherits the blocked task's priority until it releases the resource [6]. Medium-priority tasks can then no longer preempt the holder.
Under PIP, a task can be blocked for at most the duration of critical sections, where is the number of lower-priority tasks that can block it and the number of distinct semaphores that can block it [2].
PIP has two weaknesses. It does not prevent deadlock, and a task can be blocked several times in a chain [2].
The priority ceiling protocol (PCP), by the same authors, fixes both. It gives each semaphore a ceiling equal to the highest priority of any task that may lock it. A task may enter a critical section only if its priority is higher than the ceilings of all semaphores locked by other tasks, which prevents deadlock and limits blocking to one critical section [2].
Baker's stack resource policy (SRP), from 1991, extends the ceiling idea to dynamic priorities such as EDF [2]. Like PCP, it allows at most one blocking and prevents deadlock.
Two simpler protocols are also used [^2]:
- The non-preemptive protocol (NPP) disables preemption inside critical sections.
- The highest locker priority protocol (HLP) raises a task to the semaphore's ceiling as soon as it locks it.
| Protocol | Priorities | Blocked at most | Prevents deadlock |
|---|---|---|---|
| NPP | Any | 1 critical section | Yes |
| HLP | Fixed | 1 critical section | Yes |
| PIP | Fixed | critical sections | No |
| PCP | Fixed | 1 critical section | Yes |
| SRP | Any | 1 critical section | Yes |
With a protocol in place, each task has a maximum blocking time , which is added to the response time equation [^2]:
According to Buttazzo, tests that are necessary and sufficient without blocking become only sufficient once blocking factors are included [2].
Worst-case execution time
Every test above needs each task's worst-case execution time (WCET), the longest time its code can take on the target hardware [8]. The tests are only as safe as these numbers.
Why it is hard
- In general, no method can bound the execution time of every program, since that would solve the halting problem. Real-time code avoids this by banning or bounding recursion and bounding loop counts [8].
- The input that causes the worst case is usually unknown and hard to find [8].
- Caches and pipelines make an instruction's time depend on what ran before, and that time can vary by several orders of magnitude [8].
- Many powerful processors have timing anomalies, where a local worst case such as a cache miss can lead to a shorter total time. Analysis therefore cannot just take the worst case at each step [8].
Two families of methods
| Measurement-based | Static | |
|---|---|---|
| How it works | Runs the task on the hardware with test inputs and records the times | Analyzes the code and a model of the hardware without running the task |
| Result | Longest observed time | Upper bound on all execution times |
| Main limit | Generally underestimates the WCET, so not safe for hard real-time systems | Only as accurate as the hardware model |
Newer measurement-based methods measure parts of the task and combine the results, but they still rarely guarantee a bound [8]. The longest observed time on its own is not safe for hard real-time systems [8].
Static methods use abstract interpretation to predict cache and pipeline behavior [8]. Integer linear programming then finds the largest total execution time that the program's control flow allows, which gives an upper bound [8]. Integer linear programming is an optimization method that chooses whole-number values to make a quantity as large or small as possible under a set of linear equations and inequalities.
Static bounds usually overestimate the exact WCET, because the analysis loses some information [8].
The overestimation can be small. In the ARTIST2 WCET Tool Challenge, reported by Tan in 2007, one static tool overestimated by only 7 to 8% on average [8].
Preemption adds a cost that a WCET measured alone misses. A preempting task can push another task's data out of the cache, and the preempted task must reload it when it resumes [9]. A study by Bui and colleagues found that on a PowerPC MPC7410 processor, this cache interference raised a task's WCET by as much as 33% [9].
Operating system support
A real-time operating system (RTOS) must support timing guarantees. For example, it should activate periodic tasks automatically at the start of each period and detect jobs that overrun their computation time or miss their deadlines [6]. Its own interrupt and timer handling also add delays that the analysis must include.
Interrupts
In most operating systems, an interrupt from a device runs its interrupt handler at once, preempting any application task [6]. In a control system this can be wrong, because a control task may be more urgent than the device. The number of interrupts a task may suffer is hard to bound in advance, so the delay they add is unpredictable [6].
Buttazzo describes two approaches [^2]:
- Treat the interrupt's work as a sporadic task, scheduled like the others, so its cost is included in the schedulability guarantee.
- Run the handler at once at the highest priority. This minimizes interrupt latency, the delay before the interrupt is served, but its cost must be added to the analysis of every task.
Timer overhead
Most RTOSs keep time with a periodic timer interrupt, called a tick, with period . If handling each tick takes time , the timer uses a fraction of the processor, and the utilization left for tasks is [^2]:
Here is the scheduler's utilization bound, such as 1 for EDF. With µs and , a 10 ms tick leaves 0.99, a 1 ms tick leaves 0.9, and a 200 µs tick leaves only 0.5. Finer timing resolution therefore costs processor capacity.
Handling overload
A system is overloaded when, under worst-case assumptions, no schedule can meet all deadlines, so some task will miss one [2].
Theoretical limits
Locke showed that EDF performs poorly in overload [2]. A single new job can make the jobs already in the system miss their deadlines one after another, a cascade called the domino effect [2].
There is also a limit on what any online scheduler, which decides without knowing future arrivals, can achieve. Baruah and colleagues showed that when the load can exceed 2 and each job's value is proportional to its computation time, no online scheduler can guarantee more than one quarter of the value that a scheduler knowing the future would get [2]. This is a theoretical, worst-case bound.
Practical strategies
Practical systems use several strategies [^2]:
- Admission control accepts a new task only if a test shows that all deadlines will still be met.
- Resource reservation gives each task a fixed share of the processor and stops it from using more, so an overrun only affects that task. Under EDF, the constant bandwidth server by Abeni and Buttazzo does this.
- Job skipping drops some jobs of tasks that can tolerate it, which frees time for others.
- Period adaptation lowers the load by lengthening task periods. In elastic scheduling, task periods stretch like springs within allowed ranges.
History
Real-time computing began with computers that controlled processes as they happened. MIT's Whirlwind project began in 1944 as an Office of Naval Research effort to build a universal flight trainer, and its focus later changed to building a high-speed digital computer [10]. Whirlwind was among the first machines to use fast calculation for real-time problems such as aircraft simulation and air traffic control [11].
Whirlwind pioneered computers in real-time control, in contrast to the business and scientific machines of the time, which ran batches of punched-card jobs [11]. It served as the prototype for the US Air Force's Semi-Automatic Ground Environment (SAGE) air defense system [11].
| Year | Event |
|---|---|
| 1949 | Whirlwind is first brought online at MIT [11] |
| 1973 | Liu and Layland publish utilization bounds for RM and EDF [4] |
| 1974 | Horn and Dertouzos publish optimality results for EDF [2] |
| 1982 | Leung and Whitehead show that deadline-monotonic priorities are optimal when deadlines may be shorter than periods [2] |
| 1988 | Stankovic publishes his paper on misconceptions about real-time computing [1] |
| 1990 | Sha, Rajkumar, and Lehoczky publish the priority inheritance protocols [7] |
| 1991 | Baker publishes the stack resource policy [2] |
| 1997 | Priority inversion causes resets on Mars Pathfinder, fixed by turning on priority inheritance [7] |
Open problems
Several problems remain open:
- Trusting hardware models. Static WCET tools rely on models of the processor, but vendors generally do not disclose enough detail to check them, and testing can find model errors but not prove their absence [8].
- Loop bounds and indirect calls. Automatic analyses of these do not work in all cases, so users must sometimes supply annotations, notes that give the analysis extra information about the code [8].
- Preemption costs. A task's WCET depends on how often it is preempted, and the number of preemptions depends on the WCETs, a circular dependency that is hard to solve [9].
- Preemptive versus non-preemptive scheduling. Which is better has been debated for a long time, and under fixed priorities neither dominates when both predictability and efficiency count. Limited-preemption models have been proposed as a middle path [9].
Related topics
- Least slack time scheduling, which runs the job with the least slack first
- Pinwheel scheduling
- Real-time databases
- Deterministic memory
- ARINC 653
- Data Distribution Service
- Embedded systems
- Operating systems
References
- Misconceptions about real-time computing: a serious problem for next-generation systems, J. A. Stankovic, IEEE Computer, 1988.
- Hard Real-Time Computing Systems: Predictable Scheduling Algorithms and Applications, 4th ed., G. Buttazzo, Springer, 2024.
- Firm Real-Time Processing in an Integrated Real-Time System, T. Kaldewey, C. Lin, and S. Brandt, RTAS Work-in-Progress, 2006.
- Scheduling Algorithms for Multiprogramming in a Hard-Real-Time Environment, C. L. Liu and J. W. Layland, Journal of the ACM, 1973.
- Rate Monotonic vs. EDF: Judgment Day, G. C. Buttazzo, Real-Time Systems, 2005.
- Why Real-Time Computing?, G. Buttazzo, ANIPLA, 2006.
- What really happened on Mars?, M. Jones (reporting a keynote by D. Wilner of Wind River Systems), 1997.
- The Worst-Case Execution-Time Problem: Overview of Methods and Survey of Tools, R. Wilhelm et al., ACM Transactions on Embedded Computing Systems, 2008.
- Limited Preemptive Scheduling for Real-Time Systems. A Survey, G. C. Buttazzo, M. Bertogna, and G. Yao, IEEE Transactions on Industrial Informatics, 2013.
- Milestones: Whirlwind Computer, 1944-59, IEEE / Engineering and Technology History Wiki.
- The Whirlwind Computer at CHM, G. Fedorkow, Computer History Museum, 2018.
