IAE Academy

FOR CORE AND IT JOBS · CHAPTER 7 OF 7

Real-time systems and an RTOS in outline

This chapter explains what a deadline is, how a small real-time kernel decides which task runs, how tasks share data and signal each other safely, and how a device sleeps and is debugged. You need it because a program that is logically correct can still fail by being late.

Updated 30 Oct 2026

What you will be able to do

You will tell a hard deadline from a soft one and check whether a set of tasks meets its deadlines. You will choose between a mutex and a semaphore, and spot deadlock and priority inversion before they happen. You will estimate the battery life of a device that sleeps between jobs. You will name the tools used to find a fault on real hardware.

Before you start

  1. Chapter 4, Interrupts and program structure

  2. Adding, multiplying and dividing decimals; converting µA to mA

  3. Rounding a fraction up to the next whole number (the ceiling, written )

  4. Sigma notation for a sum, including a sum over the members of a set

  5. Averages weighted by time

Deadlines, tasks and scheduling

A real-time system is correct only if the answer arrives in time. In a hard real-time system a missed deadline is a failure, as with an airbag controller. In a soft one a late answer is merely worse, as with a dropped video frame. An RTOS replaces Chapter 4's super-loop with tasks: each task is a function with its own stack that runs as if it owned the processor, and a kernel switches between them. Each switch is a context switch: the kernel saves the running task's registers and restores another's, much as an interrupt saves context. Taking the processor from a task that could still run is preemption.

Task state

Meaning

Leaves the state when

Running

Has the processor

It is preempted (to Ready) or blocks

Ready

Could run, waits for the processor

The scheduler picks it (to Running)

Blocked

Waits for a delay, a mutex, a semaphore or data

The event it waits for happens (to Ready)

The scheduler picks from the ready tasks. In priority-based preemptive scheduling it runs the ready task of highest priority, and a higher-priority task that becomes ready preempts the running task at once. In round robin every ready task gets a time slice in turn. Round robin is fair but ignores urgency. Priorities favour the urgent task but can starve a low one.

WORST WAIT IN ROUND ROBIN

Holds when all tasks stay ready and use full slices, a newly ready task joins the back of the queue, and context switches take no time. is the number of ready tasks, the waiting one included, and the slice in ms. At worst another task has just begun its slice, so the new task waits one slice for each of the others.

LOAD TEST FOR A PERIODIC TASK SET

Necessary on one processor for periodic tasks with free context switches; the sum runs over all tasks. is task 's worst run time per job and its period, both in ms, so has no unit. If the tasks ask for more time than the processor has, so some task misses a deadline under any scheduler. alone does not prove every deadline is met.

RESPONSE TIME UNDER FIXED PRIORITIES

Holds on one processor for periodic tasks with fixed, distinct priorities, deadlines no longer than periods, no waits except as below, free context switches, and all released together at (the worst case). is task 's worst response time, a task's run time per job, its period, all in ms; the sum runs over , the set of tasks with priority above . In a window long, task is released at most times, each for . Start at and repeat until stops changing; the deadline is met if . If the task can wait on a mutex a lower-priority task holds, add that blocking time to the right side.

WORKED EXAMPLE

Task A has C = 1 ms and T = 4 ms. Task B has C = 2 ms and T = 10 ms. Task L has C = 3 ms, T = 20 ms and a deadline of 10 ms. Priorities: A highest, then B, then L. All three are released together at t = 0, share no data, and context switches take no time. Does L meet its deadline?

Solution
  1. Check the load first: . Not overloaded, but that does not prove L is on time, so compute its response time.

  2. Only A and B have higher priority, so . Start with ms, since L needs at least its own run time.

  3. Try : A releases time and B time. Each release costs its task's run time, so ms (L's 3 ms, one job of A, one job of B). The window grew, so recount.

  4. Try : A releases times, B time. New ms.

  5. Try : A still releases times and B once, so ms again. The value is stable, so it is the worst response time.

Answer: ms, which is at most 10 ms, so L meets its deadline.

Sharing data and signalling

Chapter 4 protected a shared variable by switching interrupts off briefly. Between tasks that is too blunt: it delays everything, even code that never touches the data. A mutex guards a critical section: a task locks it, uses the shared data and unlocks it. The task that locked it owns it, and only that task should unlock it. A semaphore is a counter used for signalling. Taking it lowers the count, and a task that takes it at zero blocks. Giving it raises the count, or readies a task waiting on it. An ISR must not block, so it does not take a mutex. To hand work to a task, it gives a binary semaphore, one whose count is only 0 or 1.

Two things go wrong with mutexes. Deadlock: task P locks mutex X then wants Y, while task Q locks Y then wants X. Each waits for the other forever. Make every task lock in one agreed order, X before Y, and the circular wait cannot form. Priority inversion: a low task L holds a mutex, a high task H wants it and blocks, and a medium task M that does not need the mutex becomes ready. M preempts L, so L cannot finish and H waits for M, which has lower priority than H.

BLOCKING WITHOUT PRIORITY INHERITANCE

Holds when H waits on a mutex held by L, and each task in the set of medium-priority tasks (above L, below H) becomes ready once while H waits and runs to completion without blocking. is the time L still needs in its critical section and the run time of medium task , both in ms. L cannot run while any is ready. Each further release of a medium task adds its again, so L's critical section alone does not bound the wait.

BLOCKING WITH PRIORITY INHERITANCE

Holds for one mutex that H locks once per job; tasks above H are counted in its response time, not here. While H waits, L runs at H's priority, so no medium task can preempt it. is L's remaining time in the critical section, in ms. Since is never more than , the longest critical section a lower-priority task runs holding that mutex, take in the response-time formula. Once L unlocks, it drops back to its own priority.

WORKED EXAMPLE

H needs a mutex that L holds, with r = 2 ms left. Two medium tasks (priority above L, below H), each with C = 5 ms, each become ready once while H waits and run without blocking. No task has priority above H. Find H's blocking time with and without priority inheritance.

Solution
  1. Without inheritance, L cannot run while a medium task is ready, so both medium tasks run in full: ms.

  2. L also has to run its last ms before H can lock the mutex. So ms.

  3. With inheritance, L runs at H's priority, above both medium tasks, so it runs its last 2 ms without interruption: ms.

  4. With inheritance the result no longer depends on how many medium tasks there are.

Answer: 12 ms without inheritance, 2 ms with it.

WATCH OUT FOR

Priority inheritance bounds priority inversion. It does not prevent deadlock. Test: deadlock needs a circle of tasks, each waiting for a lock another holds. Inheritance never breaks that circle.

Sleeping and debugging

A battery device spends most of its time idle, so the saving is in sleeping. In a low-power mode the core clock stops. In the deeper modes most peripheral clocks stop too, and the current can fall from milliamps to microamps. Interrupt sources that stay powered in the chosen mode, such as a low-speed timer or a pin change, can still wake the core. When one fires, the processor wakes, runs the ISR and the task it readies, then sleeps again. In an RTOS the idle task, which runs when no other task is ready, usually enters sleep, and the tick (the kernel's periodic timer interrupt, as in Chapter 4) wakes it too. The average current is the time-weighted mean of the two currents.

AVERAGE CURRENT OF A DUTY-CYCLED DEVICE

Holds when the device alternates between active and sleep, wake-up time is negligible, and the cycle repeats. and are the active and sleep currents in mA, and and the time in each state in ms.

BATTERY LIFE

Holds for an ideal battery whose whole capacity is usable, ignoring self-discharge and temperature. is the capacity in mAh and the average current in mA, so comes out in hours.

WORKED EXAMPLE

A sensor node draws 10 mA for 2 ms, then sleeps for 998 ms at 5 µA, and repeats. A battery holds 220 mAh. How long does it last?

Solution
  1. Convert the sleep current: 5 µA = 0.005 mA, so both currents are in the same unit.

  2. Weighted sum: mA·ms.

  3. Divide by the cycle time ms: mA, about 25 µA.

  4. Battery life: h, since mAh divided by mA gives hours; divided by 24 h per day, about 367 days.

Answer: About 25 µA average, so about 8,800 h (roughly one year).

A debug probe connects to dedicated debug pins on the microcontroller, using JTAG, a standard test and debug interface of four or five wires, or SWD (Serial Wire Debug), a two-wire debug interface. It lets you halt the core, read and write memory and registers, and step through code. A breakpoint halts the core at a chosen instruction address. While the core is halted, outside events keep happening, so a breakpoint changes the timing and can hide a timing fault. A logic analyser records the logic level, high or low, of several pins against time without halting the core. Use it to see what the device did on its pins, such as a bus transaction or the delay between an input and a response.

IN ONE LINE

A debugger shows what the code is doing; a logic analyser shows when the pins moved.

Check yourself

CHECK YOURSELF 1

An interrupt service routine has received a byte and must hand it to a task for processing. The ISR must not block. Which action is correct?

  1. (A)

    The ISR takes a mutex, copies the byte and unlocks the mutex.

  2. (B)

    The ISR gives a binary semaphore on which the task is waiting.

  3. (C)

    The ISR waits until the task has finished processing the byte.

  4. (D)

    The ISR locks the mutex and leaves the task to unlock it.

Answer and solution

Answer: (B)

  1. An ISR must not block, so it cannot wait for a mutex that a task may hold. That rules out A and D.

  2. Waiting for the task to finish is also blocking, so C is wrong.

  3. Giving a binary semaphore never blocks. It readies the waiting task, which then processes the byte. The answer is B.

CHECK YOURSELF 2

A round-robin scheduler has 6 ready tasks, each using a full time slice of q = 4 ms. Context switches take no time, and a newly ready task joins the back of the queue. Find the worst-case wait, in ms, before the newly ready task starts running. Counting the new task, there are 6 ready tasks.

Answer and solution

Answer: (accepted range )

  1. The worst case is that another task has just begun its slice.

  2. The new task waits one slice for each of the other tasks.

  3. ms.

CHECK YOURSELF 3

Three periodic tasks run on one processor with fixed priorities, highest first: A (C = 1 ms, T = 4 ms), B (C = 2 ms, T = 6 ms), L (C = 2 ms, T = 20 ms). Each task's deadline equals its period. All are released together at t = 0, share no data, and context switches take no time. Find the worst response time of L, in ms.

Answer and solution

Answer: (accepted range )

  1. The tasks above L are A and B. Start with .

  2. : .

  3. : .

  4. : . The value is stable, so ms.

CHECK YOURSELF 4

A device draws 20 mA for 5 ms, then sleeps for 995 ms at 10 µA, and repeats. Wake-up time is negligible. Its battery holds 100 mAh, all usable. Find the battery life in hours, to one decimal place.

Answer and solution

Answer: (accepted range )

  1. Convert the sleep current: 10 µA = 0.01 mA.

  2. Weighted sum: mA·ms.

  3. Cycle time is ms, so mA.

  4. h.

Used in these GATE topics