Basic fundamental of RTOS

Thread Starter

Kittu20

Joined Oct 12, 2022
482
I'm trying to understand the fundamentals of the Real Time Operating System ( FreeRTOS) .

https://www.freertos.org/RTOS-task-states.html

What I have understood after reading so far, I am describing it with the help of an example.

Example :

Task priority
Task 1 has the highest priority.
Task 2 has the lowest priority

Task Time
Task 1 must be handled and completed without failure at every 40us.
Task 2 must be completed in under 800μs.

CPU time
Task 1 takes 5us CPU time
Task 2 takes 800us CPU time.

In FreeRTOS, a task has four states:

Ready :
Task 1 and Task 2 are ready to run but currently not running on processor

Running :
Task 1 and Task 2 are both ready to run but scheduler picks Task 1 as it is a higher priority task and should be run first.

Blocked :
Task 1 runs on the processor for 5us and it goes into a blocked state for 35 us.

I don't understand any difference between suspended and blocked state. I want someone to explain me how suspended state is different from blocked state
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
suspended means some other task is running.
In my example I see three possible states ready, running and blocked

Task 1 runs on the processor for 5us and wait for 35 us to take control of CPU.

Sorry but i still don't fully understand about suspended state. When is suspended state required?
 

nsaspook

Joined Aug 27, 2009
13,418
Basically the difference is that tasks in the blocked state always have a timeout. Tasks in the suspended state are suspended indefinitely, there is no timeout. From the users point of view tasks that block on an event are in the blocked state. You have obviously gone into the code in more detail that some so have noticed that under certain circumstances the task is actually moved into a suspended queue rather than a blocked queue, but this is an implementation detail, conceptually the task is still blocked rather than suspended. In older versions of FreeRTOS is was not possible to block indefinitely for an event. You always had to specify a timeout. Newer versions take the longest possible timeout value, portMAX_DELAY, as an indication that the user actually wants to block indefinitely for the event. Under this circumstance moving the task to the suspended queue is just a trick that prevents the kernel from ever waking the task purely because its timeout had expired.
https://www.freertos.org/FreeRTOS_S...tion_about_Blocked_and_Suspended_1742153.html
When a task is suspended (by a call to vTaskSuspend()) then it is no longer blocked on the event so the event cannot wake it. The only way for a Suspended task to ever run again is for another task to resume it using vTaskResume().
Like is said above the OS task queue has a suspended queue for tasks it doesn't need to check for possible trigger events to give that task run-time. Another external task must make a explicit command to move suspended tasks from that status.
1675017064938.png
https://www.freertos.org/RTOS-task-states.html
 

WBahn

Joined Mar 31, 2012
30,239
Blocked means it is waiting for some event, suspended means some other task is running.
This is usually what the "ready" means -- tasks that are ready to run, but some other task is running.

Suspended generally means that a task has effectively been removed from consideration until further notice. It's akin to "blocked-with-prejudice". It's not a matter of waiting on some resource it needs to become available, some other task has to put it back into play.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
if the count starts from 1us then can anyone tell me when task 3 will run and when it will block

Example :

Task priority
Task 1 has the highest priority.
Task 2 has middle priority
Task 3 has the lowest priority

Task Time
Task 1 must be handled and completed without failure at every 10us.
Task 2 must be completed in under 200μs.
Task 3 must be completed in under 700μs.

CPU time
Task 1 takes 4us CPU time
Task 2 takes 10us CPU time.
Task 3 takes 5us CPU time

Total CPU time is 19us
 

BobTPH

Joined Jun 5, 2013
9,128
No, we cannot, because it depends on the scheduling algorithm of the particular RTOS you are running on.

This is the wrong way to think about multi-tasking. If the CPU has enough power to handle all of the tasks in a timely manner, why does it matter what order they are scheduled in? In designing a system, you would calculate how much CPU time you needed for all tasks, and make sure there is plenty of extra time for the RTOS itself and a margin of error if things are not scheduled optimally, Then run the system, and tweak if things are not working out.
 

BobTPH

Joined Jun 5, 2013
9,128
No, these tasks are run at different frequencies, you need to determine how many seconds of CPU time each task requires every second. Then you can add these up and if the result is more than one second, you know you are in trouble.

Determining the order the tasks will run is if no use. In a real system responding to real-tine events, this will constantly be changing.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
No, these tasks are run at different frequencies, you need to determine how many seconds of CPU time each task requires every second.
I don't understand particular reason why every task has to be checked in a one second. Why can't we check when task need to repeat?

I know the two timing
1) How much CPU time, each task take to run on processor
2) At what time interval each task should be repeat

The order in which the task will be execute depends on priority. High priority task always Run first
 

WBahn

Joined Mar 31, 2012
30,239
I don't understand particular reason why every task has to be checked in a one second. Why can't we check when task need to repeat?

I know the two timing
1) How much CPU time, each task take to run on processor
2) At what time interval each task should be repeat

The order in which the task will be execute depends on priority. High priority task always Run first
That's not what he said. He is just describing one way of determining if you require more processing time than you have.

Say you have a task that only needs to run once every ten seconds, but servicing that task takes three seconds. That task requires 30% of the total processing time available. Or, put another way, for each second of processing time available, this task requires 300 ms of it.
 

BobTPH

Joined Jun 5, 2013
9,128
I know the two timing
1) How much CPU time, each task take to run on processor
2) At what time interval each task should be repeat
You would almost never have that information. If you did, why do you need a scheduler or for that matter an RTOS You would simply run the tasks in order and you’re done.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
That's not what he said. He is just describing one way of determining if you require more processing time than you have.

Say you have a task that only needs to run once every ten seconds, but servicing that task takes three seconds. That task requires 30% of the total processing time available. Or, put another way, for each second of processing time available, this task requires 300 ms of it.
Does it mean that at a time we do not give full time to complete the task. In this case the task required 3 seconds but we are giving it 300 milliseconds per second.

But if task is completed in pieces, will it not be corrupt?
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
You would almost never have that information. If you did, why do you need a scheduler or for that matter an RTOS You would simply run the tasks in order and you’re done.
The scheduler decides task order when and which task to run on processor according to priority. Sorry if I am missing something
 
Last edited:

WBahn

Joined Mar 31, 2012
30,239
Does it mean that at a time we do not give full time to complete the task. In this case the task required 3 seconds but we are giving it 300 milliseconds per second.

But if task is completed in pieces, will it not be corrupt?
No, it doesn't mean that at all. The use of one second is a completely arbitrary reference. What matters is the fraction of available time each task requires and whether or not the total of all of them exceeds 100% (actually, some smaller threshold because you need to have some margin).

As for completing a task in pieces, your computer does that all the time. Your web browser gets to use the processor for a fraction of a second -- a very small fraction of a second, like perhaps 20 ms -- before it is yanked away and some other task gets to run for about the same amount of time. Eventually, it gets back around to your browser's turn again and it gets to run for another twenty milliseconds. In a normal OS, this is done in such a way that there is no guarantee that there won't be noticeable effects, such as your mouse becoming unresponsive for a second or two or the text that you are typing having an annoying delay before you see it on the screen for a bit. But in some systems, such delays can be catastrophic, so normal operating systems are not well-suited for those kinds of systems. That is the what RTOSs are all about -- giving the services that an OS offers in terms of things like resource management, but being designed in such a way that they can provide service guarantees to enusre that critical processes get to run frequently enough to avoid those problems. This comes at a cost in overall performance and loss of flexibility.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
No, it doesn't mean that at all. The use of one second is a completely arbitrary reference. What matters is the fraction of available time each task requires and whether or not the total of all of them exceeds 100% (actually, some smaller threshold because you need to have some margin).
sorry I still don't fully understand. I have presented some timings here to understand how the system will schedule high priority tasks as well as other priority task. Each task will have a start and stop time.

A system may have to perform a variety of tasks, some of which are of higher priority. System performs high priority tasks without missing deadlines. When the system has tasks like high priority task and low priority task, then the system executes the high priority task first.
 

WBahn

Joined Mar 31, 2012
30,239
sorry I still don't fully understand. I have presented some timings here to understand how the system will schedule high priority tasks as well as other priority task. Each task will have a start and stop time.

A system may have to perform a variety of tasks, some of which are of higher priority. System performs high priority tasks without missing deadlines. When the system has tasks like high priority task and low priority task, then the system executes the high priority task first.
Let's make an example that you can hopefully relate to.

There are 24 hours in a day. You perform a number of tasks during the day. Some of them you only do once, others you do several times. Let's make up the following list:
1) Sleep - 1 time for 8 hours.
2) Eat meals - 2 times for 30 minutes each.
3) Bathroom break - 6 times for 10 minutes each.
4) Commuting - 2 times for 1 hour each.
5) Exercise - 1 time for 1.5 hour.
6) Work - 1 time for 8 hours
7) Watch TV - 1 time for 3 hours
8) Pay bills - 1 time for 30 minutes

Do you have enough time in the day to do everything?

There are many ways to answer this. One is to do it on a per-hour basis.

Over the course of the day, you spend 20 minutes per hour sleeping. This does NOT mean that you physically sleep for 20 minutes, then wake up for 40 minutes and do some things, and then go to sleep for another 20 minutes, it merely means that IF you were evenly spread out each task, THEN you would end up spending 20 minutes of each hour sleeping. It's merely a way to do the bookkeeping. So doing this, the time spent, per hour, on each task is;

1) Sleep - 20 min
2) Eat meals - 2.5 min
3) Bathroom break - 2.5 min
4) Commuting - 5 min
5) Exercise - 3.75 min
6) Work - 20 min
7) Watch TV - 7.5 min
8) Pay bills - 1.25 min

If we add everything up, we get that you need to, on average, spend 62.5 minutes each hour doing these task, or 1.042 hours/hour.

Since this number is greater than one, you are task saturated, so something has to give.

You could have used ANY unit of time to do this. You could have written it as minutes/minute, hours/hour, days/day, or just as a fraction or percentage. The result would have been the same, namely that your tasks, by themselves, require 4% more time that you have available, even before allowing for overhead things like getting dressed or walking out to the car.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
Thank you @WBahn for explaining in a very simple language. I got the idea from your explanation.

Task Switching

When RTOS scheduler gives the control of CPU from one task to another task It's called task switching.

What is below process called task synchronization or inter process process communication ?

"Multiple tasks share resources of microcontroller like they can share CPU time. They can share the memory"
 

WBahn

Joined Mar 31, 2012
30,239
Thank you @WBahn for explaining in a very simple language. I got the idea from your explanation.

Task Switching

When RTOS scheduler gives the control of CPU from one task to another task It's called task switching.

What is below process called task synchronization or inter process process communication ?

"Multiple tasks share resources of microcontroller like they can share CPU time. They can share the memory"
Neither. It's resource sharing.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
Neither. It's resource sharing.
I don't think there is any difference between the task, process and thread. They all are same thing.

I am trying to understand the meaning of following definition for interprocess communication.

"Inter process communication is a process of real time operating system that allow one process to communicate with the another process."

I don't understand how one task/process communicate with the another task/process. I only understand task/process can share the resources like CPU time Or memory.
 
Top