# How to know Scheduler state and which task is running?

**URL:** <https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340>\
**Category:** Kernel\
**Created:** [July 23, 2020, 11:00am UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340 "2020-07-23T11:00:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aripod](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aripod/32/328_2.png) [@aripod](https://forums.freertos.org/u/aripod)\
**Post date:** [July 23, 2020, 11:00am UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/1 "2020-07-23T11:00:06Z")

</div>

Hello,

Is it possible to know the request and grant signals that the scheduler gets/gives? Would it be possible to have a variable which says which task is currently running?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [July 23, 2020, 11:03am UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/2 "2020-07-23T11:03:41Z")

</div>

The question is a bit vague …  
Which bigger problem do you want to solve ?

---

<div class="post-metadata">

**Author:** ![RealtimeRik](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/realtimerik/32/581_2.png) [@RealtimeRik](https://forums.freertos.org/u/RealtimeRik)\
**Post date:** [July 23, 2020, 11:32am UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/3 "2020-07-23T11:32:09Z")

</div>

pxCurrentTCB is a pointer to the task currently running (pointer to the TCB of currently running task).

Not sure what you mean by:

> [@aripod](#):
>
> Is it possible to know the request and grant signals that the scheduler gets/gives?

---

<div class="post-metadata">

**Author:** ![aripod](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aripod/32/328_2.png) [@aripod](https://forums.freertos.org/u/aripod)\
**Post date:** [July 23, 2020, 11:57am UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/4 "2020-07-23T11:57:24Z")

</div>

I would like to know externally (via GPIO for example) which task is running. So, if I have 8 tasks, with 3 bits I want to know which one is currently executing.

---

<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:** [July 23, 2020, 12:10pm UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/5 "2020-07-23T12:10:03Z")

</div>

In the source code for FreeRTOS are some ‘TRACE’ macros that are called at various time that can implement this. There is a macro that is called as a task is swapped in, that you could make set the GPIOs. (Would need to look in the code to find the exact name, which I don’t have convenient at the moment). You would define that trace macro in your FreeRTOSConfig.h

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [July 23, 2020, 12:10pm UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/6 "2020-07-23T12:10:15Z")

</div>

See the [FreeRTOS trace hooks](https://www.freertos.org/rtos-trace-macros.html).

---

<div class="post-metadata">

**Author:** ![RealtimeRik](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/realtimerik/32/581_2.png) [@RealtimeRik](https://forums.freertos.org/u/RealtimeRik)\
**Post date:** [July 23, 2020, 12:13pm UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/7 "2020-07-23T12:13:32Z")

</div>

Add some code to traceTASK\_SWITCHED\_IN. Or put some code in vTaskSwitchContext or taskSELECT\_HIGHEST\_PRIORITY\_TASK.

Or you could use Percipio Trace but I am not sure what you can use for free.

---

<div class="post-metadata">

**Author:** ![aripod](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aripod/32/328_2.png) [@aripod](https://forums.freertos.org/u/aripod)\
**Post date:** [July 23, 2020, 12:24pm UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/8 "2020-07-23T12:24:05Z")

</div>

Those are good places to start. Thanks for the quick answer

---

<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:** [July 23, 2020, 1:50pm UTC](https://forums.freertos.org/t/how-to-know-scheduler-state-and-which-task-is-running/10340/9 "2020-07-23T13:50:23Z")

</div>

There are also trace tools that will provide this information. [This](https://FreeRTOS.org/trace) uses the trace macros to generate a complete trace including the information you want. There is also Segger SystemView, although I’m not sure of the detail that provides.
