# Analysing tasks management and priorities 

**URL:** <https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048>\
**Category:** Kernel\
**Created:** [March 11, 2018, 12:08am UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048 "2018-03-11T00:08:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 11, 2018, 12:08am UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/1 "2018-03-11T00:08:56Z")

</div>

**[xavierpacheco](https://sourceforge.net/u/xavierpacheco/)** wrote on [Sunday, March 11, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#34fe):

A task has been created that reads a value of the ADC of a temperature sensor of a battery every 100 ms, calculate the value of it from the reading and update the value of a variable with it. My first iteration of the routine is as follows:

`#define DELAY\_TICKS 100 // tick period 1 ms =\> 100 ms delay  
volatile float battery\_temperature;

void temperature\_task(void\* parameters) {

TickType\_t current\_tick\_count, expired\_tick\_count;

while (1) {  
// Read ADC  
unsigned short adc\_value = read\_temperature\_adc();

// Compute temperature  
float temperature = compute\_temperature(adc\_value);

// Update variable  
battery\_temperature = temperature;

// Get current system tick count and compute expired count  
current\_tick\_count = xTaskGetTickCount();  
expired\_tick\_count = current\_tick\_count + DELAY\_TICKS;  
// Wait until the number of ticks has happen (100 ms)  
while (xTaskGetTickCount() \< expired\_tick\_count) {} } }`

Once incorporated into the system, other tasks have begun to have problems because they are receiving less processing time, and even one of them has stopped working completely.

The task has been created with “normal” priority and the tick of the system is 1 ms (time of change between concurrent tasks). And the function to read the ADC works waiting for the reading, but it only takes a few nanoseconds and is not considered to impact the system, like the routine that computes the temperature.

So these are my questions:

What happens with tasks that have been receiving less processing time, but still running? Specifically about their priority and the way they use their time.

What happened to the task that has stopped running completely?

What is the problem that I have caused with my routine and how can I solve it?

P.D.: I’m using FreeRTOS Cortex M3

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [March 11, 2018, 1:57am UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/2 "2018-03-11T01:57:39Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Sunday, March 11, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#d60f):

In this loop:

```auto
    while (xTaskGetTickCount() < expired_tick_count) {
    }

```

your task consumes all CPU time, depending on it priority.

Please have a look at [vTaskDelayUntil()](https://www.freertos.org/vtaskdelayuntil.html)  
and [vTaskDelay()](https://www.freertos.org/a00127.html)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 11, 2018, 2:38pm UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/3 "2018-03-11T14:38:36Z")

</div>

**[xavierpacheco](https://sourceforge.net/u/xavierpacheco/)** wrote on [Sunday, March 11, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#d60f/7ee1):

But, if that loop consumes all CPU time, how can other tasks still execute?

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [March 11, 2018, 3:36pm UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/4 "2018-03-11T15:36:42Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Sunday, March 11, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#4f9e):

That is a very good question! I thought about it while I composed my answer here above.

The loop doesn’t block in any way, so that task is non-stop **runnable**.

As long as a task with priority P is runnable, only tasks of priority P and higher will run.

These three defines determine the behaviour of the FreeRTOS scheduler:

```auto
    configUSE_PREEMPTION
    configUSE_TIME_SLICING
    configIDLE_SHOULD_YIELD

```

I think you find good documentation [here](https://freertos.org/Documentation/RTOS_book.html). Although it might not be up-to-date with the latest newest 10.x release. The principles of FreeRTOS scheduling have never changed. … as far as I know.

I can answer your question, but then I’d have to see your code, most importantly `FreeRTOSConfig.h`, and also the priorities of the other tasks, and see if and how they block.

About timing: note that `TickType_t` is a 32-bit number. It will overflow, and also this calculation will, some day, overflow:

```auto
    expired_tick_count = current_tick_count + DELAY_TICKS; 

```

When it overflows, your timing will be incorrect.

[Here](https://www.freertos.org/xTaskCheckForTimeOut.html) you find safe method, along with an example.

Regards,

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 11, 2018, 4:37pm UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/5 "2018-03-11T16:37:08Z")

</div>

**[xavierpacheco](https://sourceforge.net/u/xavierpacheco/)** wrote on [Sunday, March 11, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#4f9e/5520):

Thank you. I just want to understand something in this example: I know that the while loop doesn’t block, so it takes as much time as it is possible. But, how can other tasks get less processing time and still executing? I mean, in this example, a task with higher priority will pre-empts my ADC task without taking into account that ADC task doesn’t block. But, if the other tasks have the same priorities, I think they might execute really fast and then get pre-empted by my ADC task again, right?

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [March 11, 2018, 4:53pm UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/6 "2018-03-11T16:53:21Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Sunday, March 11, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#e9e4):

> I know that the while loop doesn’t block, so it takes as  
> much time as it is possible.  
> But, how can other tasks get less processing time and  
> still executing?

It looks like the other tasks have an equal priority and thus there is time-slicing.

But before guessing more, I’d like to see your `FreeRTOSConfig.h`, and also the priorities of all tasks, and see if they block.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [March 11, 2018, 4:54pm UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/7 "2018-03-11T16:54:59Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Sunday, March 11, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#73e8):

Assuming you have the proper options enable in FreeRTOSConfig.H, task of the same priority will time share in a round robbin fashion, switching which ready task will run on each Tick interrupt. If the other task do something to block, then they will give up the processor, and this task will eventually get run, and continue until the next Tick.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 11, 2018, 5:20pm UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/8 "2018-03-11T17:20:26Z")

</div>

**[xavierpacheco](https://sourceforge.net/u/xavierpacheco/)** wrote on [Sunday, March 11, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#e9e4/c0e7):

The problem is about guessing, because the approach doesn’t give more information. That’s all it says. That’s a problem that I found in an article and I was interested in analysing it. So, we can guess what it sounds more reasonable. As you say, we can assume that other tasks have same priorities. But, as the problem says, there is a task that stopped executing, so it has to be the iddle task. So, as the ADC task doesn’t block, the only way that other tasks are able to execute, is either if they have higher priorities and they block or if they have the same priorities and don’t block, so there is time-slicing and that’s why they receive less time than the ADC task.

Is it my approch good?  
Now, the way to solve this is replacing the while loop with a TaskDelay or TaskDelayUntil function.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [March 13, 2018, 2:08am UTC](https://forums.freertos.org/t/analysing-tasks-management-and-priorities/7048/9 "2018-03-13T02:08:36Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Tuesday, March 13, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/65379fe7/#d60f/7ee1/f7d7):

Because it can’t actually take ALL the time, higher priority tasks can still get time when then become ready, and equal priority too if you have configUSE\_TIME\_SLICING enabled.
