Skip to content
You Start with one site
Amounts in Start with one site
Embedded Systems · Concept

RTOS tasks

One idea Advanced 5 min read
Found this useful?

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.

Finish what you startedThe screen work continues to its end, and the sensor waits.The reading is late
Take turnsOne millisecond each. Fair, and nobody is on time.Late by a small amount
The urgent one goes firstThe sensor read takes the processor, then gives it back.This is priority

See it

Predict first

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?

See it: who has the processor

Sample due at 10 ms starts at
Late by
Display work ends at

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 question

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
  1. 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.

  2. 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.

  3. 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

A high priority does not make a task faster. Priority decides only who goes first when two tasks are ready. A task of high priority that never waits keeps the whole processor, and every task below it stops. Give a high priority to work that is short and has a deadline, and make every task block when it has nothing to do.

Check yourself

Question 1

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?

Question 2

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?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.