RTOS tasks
On this page
A nurse on a ward has many jobs, but when an alarm sounds she leaves the paperwork at once and returns to it later. A small processor can work in the same way. An RTOS task is one job with its own priority, and the RTOS (real-time operating system) always gives the processor to the most urgent task that is ready to run.
See it
A device has two tasks. The display task (priority 2) starts 14 ms of drawing work at 4 ms. The sampling task (priority 1, which is lower) must read a sensor every 10 ms, and each read takes 2 ms. A read is due at 10 ms. When does it start?
Each row is one task. A filled block means the task is running. A dashed block means it is ready but a more urgent task has the processor. The display task always has priority 2. The small marks on the top row show when a sample is due.
The idea
A task is one job written as its own small program: read the sensor, draw the screen, send the data. Each task has a priority, a number that says how urgent it is. In FreeRTOS, a larger number is more urgent.
A task is running when it has the processor. It is ready when it could run but a more urgent task has the processor. It is blocked when it waits for something: a time, a message, a button.
The scheduler is the part of the RTOS that chooses. Its rule: the ready task with the highest priority runs. When a more urgent task becomes ready, the scheduler stops the running task at once and gives the processor to the urgent one. This is preemption. The stopped task loses nothing and continues later from the same place.
Tasks of equal priority take turns of about 1 ms. When no task is ready, a task called idle runs. Each change of task costs about 5 to 15 millionths of a second on a small microcontroller: little, but not zero. See the lesson on tasks and scheduling.
One worked number
The display task (priority 2) starts 14 ms of work at 4 ms. The sampling task now has priority 3. It is due at 10 ms and needs the processor for 2 ms. When does the sample start, and when does the display work end?
Show the worked solution, 3 steps
- Find who is ready at 10 ms
Both tasks are ready. The sampling task has the higher priority, so it starts at 10 ms, on time, and ends at 12 ms.
- Count the display work already done
From 4 ms to 10 ms the display task did 6 ms of work. 14 ms − 6 ms = 8 ms remain.
- Add the remaining work after the sample
12 ms + 8 ms = 20 ms. The display ends 2 ms later than before, and the sample is 8 ms earlier. Check it in the figure: set the slider to 3.
The mistake to avoid
Check yourself
A sampling task has the highest priority. It runs for 2 ms in every 10 ms and is blocked for the rest of the time. What share of the processor is left for all the other tasks?
Someone adds a task at the highest priority that checks a button in a loop and never waits. The screen and the network of the device stop responding. Why?
The display task (priority 2) is running. The sampling task (priority 3) is blocked while it waits for its 10 ms timer. The timer ends. What happens next?
Where you meet it
- An energy loggerOne task samples voltage and current on time. A second writes to a slow memory card and cannot delay the first.
- A motor driveThe task that controls the motor current runs many times each second above everything else.
- A pump controller with a screenThe task that watches for a dry run or an overload preempts the task that draws the menu.
- The ready task with the highest priority runs. The others wait.
- A more urgent task that becomes ready takes the processor at once: preemption.
- Priority is an order, not a speed. Every task must block when it has no work.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.