# Which task runs

**URL:** <https://forums.freertos.org/t/which-task-runs/11722>\
**Category:** Kernel\
**Created:** [February 7, 2021, 5:26am UTC](https://forums.freertos.org/t/which-task-runs/11722 "2021-02-07T05:26:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mission](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mission](https://forums.freertos.org/u/mission)\
**Post date:** [February 7, 2021, 5:26am UTC](https://forums.freertos.org/t/which-task-runs/11722/1 "2021-02-07T05:26:15Z")

</div>

Hello,  
1)Tow idle tasks Task\_A and Task\_B. A third Task\_C is higher priority.  
Task\_C preempted Task\_B and ran in 25% of time assigned to Task\_B, then blocked itself. Would Task\_B run to finish it’s time slot or Task\_A would run?  
2) if configUSE\_TIME\_SLICING 0 what’s the side effect on the schedule in switching the tasks?  
3)In VxWorks RTOS, round robin assigns count(counter) for each task. If high priority task interrupted the lower, it runs and when it blocks itself the interrupted task runs to finish the count . Here in FreeRTOS, in every tick, the time slice switch form one to the other. Can please clarify?  
4) “calling taskYIELD() will never result in a switch to a higher priority task”. My question is switching to which priority would occur ?  
I’m wondering between richard\_damon comment here:

> **[configUSE\_TIME\_SLICING - FreeRTOS](https://freertos.org/FreeRTOS_Support_Forum_Archive/December_2013/freertos_configUSE_TIME_SLICING_278999daj.html)**
>
> configUSE\_TIME\_SLICINGPosted by dragonflight1 on December 11, 2013First, I want to confirm how configUSETIMESLICING = 0 works. Assuming two processes A and B at the same priority, I understand (I think) as long as A calls no FreeRTOS functions or...

and the definition in taskYIELD() here:

> **[FreeRTOS - Task Control Functions and Macros for the Free Open Source RTOS...](https://www.freertos.org/a00020.html#taskYIELD)**
>
> Describes the free RTOS kernel control API, including starting the RTOS scheduler, ending the RTOS scheduler, and suspending and resuming the scheduler.

Regarding TaskYIELD() and switching.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [February 7, 2021, 5:18pm UTC](https://forums.freertos.org/t/which-task-runs/11722/2 "2021-02-07T17:18:12Z")

</div>

The below information can also be obtained from the free to download book.

> [@mission](#):
>
> Task\_C preempted Task\_B and ran in 25% of time assigned to Task\_B, then blocked itself. Would Task\_B run to finish it’s time slot or Task\_A would run?

Task\_A. If task A and B are the same priority they will take turns.

> [@mission](#):
>
> 1. if configUSE\_TIME\_SLICING 0 what’s the side effect on the schedule in switching the tasks?

The configUSE\_TIME\_SLICING setting will have not effect in the case you describe because the first context switch it due to a higher priority task preempting a lower priority task (not a tick interrupts), and the second context switch is the higher priority task blocking and so allowing a lower priority task to run (also not a tick interrupt). configUSE\_TIME\_SLICING only has an effect on scheduling between tick interrupts when there are tasks of equal priority sharing the MCU.

> [@mission](#):
>
> 1. “calling taskYIELD() will never result in a switch to a higher priority task”. My question is switching to which priority would occur ?

FreeRTOS will always run the highest priority task that is able to run so assuming you are using preemption, taskYIELD() can never yield to a higher priority task because if there were a higher priority task then it would already be running. taskYIELD() can then only yield to a task of the same priority - which might be itself if the calling task is the only task at that priority.

---

<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:** [February 7, 2021, 6:45pm UTC](https://forums.freertos.org/t/which-task-runs/11722/3 "2021-02-07T18:45:31Z")

</div>

Slight correction, calling taskYIELD() might switch to a higher priority task if configUSE\_PREEMPTION is set to 0, or if an ISR is written incorrectly and it doesn’t activate the scheduler after waking a higher priority task. Both of these cases ‘break’ the rule that FreeRTOS will always be running the highest priority ready task. With configUSE\_PREEMPTION it is an explicit request to not do context switches except at the specific points (like a taskYIELD) to allow the program to use a more relaxed view of state sharing.

---

<div class="post-metadata">

**Author:** ![mission](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mission](https://forums.freertos.org/u/mission)\
**Post date:** [February 7, 2021, 7:06pm UTC](https://forums.freertos.org/t/which-task-runs/11722/4 "2021-02-07T19:06:04Z")

</div>

Hi Richard,  
" If configUSE\_TIME\_SLICING is set to 0 then the RTOS scheduler will still run the highest priority task that is in the Ready state, but will not switch between tasks of equal priority just because a tick interrupt has occurred."  
Regarding equal priority, how and when the switching would occur?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [February 7, 2021, 7:22pm UTC](https://forums.freertos.org/t/which-task-runs/11722/5 "2021-02-07T19:22:35Z")

</div>

Again - you can get this information yourself from the free to download book.

If you are not using time slicing then a context switch will occur any time a higher priority task is enters the ready state (assuming you are using preemption) or any time the currently running task enters the blocked state.

---

<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:** [February 7, 2021, 7:40pm UTC](https://forums.freertos.org/t/which-task-runs/11722/6 "2021-02-07T19:40:59Z")

</div>

Note, I am talking about configUSE\_PREEMPTION, not configUSE\_TIME\_SLICING.

configUSE\_TIME\_SLICING means that as long as a given task continues to run, it keeps it place at the front of its current priority level line.

One thing that definitely puts it to the end of the line is if the task blocks or yields, then it moves to the back of the line.

I would need to look at the code, but will trust Richard Barry about the behavior if it gets interrupted by a higher priority task.

Basically, configUSE\_TIME\_SLICING (which basically needs configUSE\_PREEMPTION) does is insert a call to taskYIELD() at every tick interrupt into the currently running task.

---

<div class="post-metadata">

**Author:** ![mission](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mission](https://forums.freertos.org/u/mission)\
**Post date:** [February 7, 2021, 8:20pm UTC](https://forums.freertos.org/t/which-task-runs/11722/7 "2021-02-07T20:20:41Z")

</div>

> [@richard-damon](#):
>
> Basically, configUSE\_TIME\_SLICING (which basically needs configUSE\_PREEMPTION) does is insert a call to taskYIELD() at every tick interrupt into the currently running task.

Can you please re-read to ensure that what you meant?

---

<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:** [February 7, 2021, 10:20pm UTC](https://forums.freertos.org/t/which-task-runs/11722/8 "2021-02-07T22:20:40Z")

</div>

What configUSE\_TIME\_SLICING does is every system tick (assuming configUSE\_PREEMPTION is enabled) is automatically run the scheduler, even if no higher priority task was woken from some delay/timeout expiring on that tick. This effectively makes the currently running task do a taskYIELD() at the point it is at, since that is basically just what a taskYIELD() call does. If the scheduler is currently suspended, then this yield is remembered and happens with the scheduler is resumed.

---

<div class="post-metadata">

**Author:** ![mission](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mission](https://forums.freertos.org/u/mission)\
**Post date:** [February 8, 2021, 3:13am UTC](https://forums.freertos.org/t/which-task-runs/11722/9 "2021-02-08T03:13:42Z")

</div>

When ,

```auto
configUSE_TIME_SLICING 1
configUSE_PREEMPTION 1

```

Then on every tick, the running task would call taskYIELD() even if the running task is higher in the priority than the others. _ **Is that correct** _?

---

<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:** [February 8, 2021, 3:30am UTC](https://forums.freertos.org/t/which-task-runs/11722/10 "2021-02-08T03:30:14Z")

</div>

Every tick the scheduler will run, and the highest priority task will be selected. If the currently running task is the only task ready at its priority level, it will continue (just like if it call taskYEILD) but if there is another ready task at that priority level then they will time slice between them.
