How both tasks will schedule in free RTOS

There are two tasks in the hypothetical situation. Both are the time critical tasks. The first task is a high priority task. This must be handled and completed without failure at every 40us. it only uses 4μs of CPU time.

Second task is time consuming and uses most of the CPU resources, almost 100% if allowed to do so. It has the lowest priority but this must be completed in under 300μs.

How both tasks will schedule in free RTOS technique

FreeRTOS has a very simple scheduler, the highest priority ready task is run.

If tasks tie for priority, and the round-robin scheduling is enabled, they will cycle between themselves every tick.

FreeRTOS doesn’t know your timing requirements, so schedules are STRICTLY based on those priority rules and the task state.

For your first task to run every 40us, you will need something in the system to enable the task at that rate. You COULD make the system tick that fast, but then you will have the tick running every 40us which would use a lot of processor time.

I suppose a big question comes to how fast is your processor, and how long does it take to do a full context switch.

Personally, unless that High Priority task is doing something “special”, I would probably just do its code in an appropriate priority ISR that triggers when it is needed to run. That will minimize the context switching needed.

The low-priority task will get whatever time is left, and it meeting the 300us requirement is up to you to make sure you don’t have things that take more time than that from it. Since you imply that it is the only other thing running, that shouldn’t be a problem.

1 Like

Thanks for your explanation i got an idea.

Suppose that in addition to these two tasks, I have a third task that needs to be completed.

So first task is high priority task, second task is middle priority task and third task is low priority task.

Third task occurs every 2ms If the task is not completed withing 10us the process is suspended until the next 2ms.

Now we have three tasks. A task can be in four states, ready to run, running, blocked and suspended. I want to understand when the state of each task would be change, at what time it will change?

Nothing will automatically “suspend” the task after 10us unless you provide your own hardware timer to generate an interrupt. You could have the task block itself for the rest of the 2ms tick after it does an amount of work that you expect will take that much time.

I didn’t understand when the third task will run, when it will suspend and when it will block.

Did you already had a look into the Free RTOS Book and Reference Manual docs ?
It helps a lot to get the big picture.
In Mastering the FreeRTOS Real Time Kernel ch. 3 Task Management you’ll find explanations how task scheduling works and how tasks change their states and why.

Nothing will automatically Suspend or Block the task, YOU need to do something to make that happen.

IF you want a task to limit how much it runs, you need to handle that sort of limit yourself.

1 Like