Is the Stack Size of RTOS Tasks Related to the Amount of Task Code?

Recently, a colleague raised the following question:“I have a lot of code in my task; does this mean that the stack for this task needs to be allocated larger?”Below, we will describe the relationship between the amount of task code and stack allocation.

1RTOS Task Stack Allocation

Many RTOSs on the market require the stack size to be allocated in advance, meaning that the stack size is determined when creating the task.For example, in uCOS, to create a check task:

// Task priority#define TASK_CHECK_PRIO                        6
// Task stack size#define TASK_CHECK_STK_SIZE                    128
// StackOS_STK TaskCheckStk[TASK_CHECK_STK_SIZE];
// Create task - signal detectionOSTaskCreateExt((void (*)(void *)) AppTaskCheck,                (void           *) 0,                (OS_STK         *)&TaskCheckStk[TASK_CHECK_STK_SIZE-1],                (INT8U           ) TASK_CHECK_PRIO,                (INT16U          ) TASK_CHECK_PRIO,                (OS_STK         *)&TaskCheckStk[0],                (INT32U          ) TASK_CHECK_STK_SIZE,                (void           *) 0,                (INT16U          )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));
// Task application implementationvoid AppTaskCheck(void *p_arg){  // Code···
  (void)p_arg;
  for(;;)  {    // Code···  }}

Creating a task in FreeRTOS is similar; the stack size is also allocated at creation:

#define TASK_CHECK_PRIO                        6#define TASK_CHECK_STK_SIZE                    128
BaseType_t xReturn;
xReturn = xTaskCreate(AppTaskCheck, "AppTaskCheck", TASK_CHECK_STK_SIZE, NULL, TASK_CHECK_PRIO, NULL);

In addition to the stack, other resources like message queues and mailboxes also require pre-allocation of stack space.

For example, creating a CLI message queue in FreeRTOS:

#define CLI_QUEUE_NUM             256                      // Number of CLI receive queues#define CLI_PACKAGE_LEN           2                        // Length of CLI data packet
QueueHandle_t xCLIRcvQueue = NULL;
/* Create queue */if(xCLIRcvQueue == NULL){  xCLIRcvQueue = xQueueCreate(CLI_QUEUE_NUM, CLI_PACKAGE_LEN);}

This is the stack allocation for creating tasks (or queues); the specific allocation depends on your actual situation.

2Task Code Volume

The code volume of a task refers to the code that is called within that task.

For example, in the code above:

// Task application implementationvoid AppTaskCheck(void *p_arg){  // Code···
  (void)p_arg;
  for(;;)  {    // Code···  }}

This may contain thousands of lines of code or call hundreds of functions, each with considerable code inside.

Thus, the code volume for this task can be quite large.

3Is There a Relationship Between Task Code Volume and Stack Size?

Many people have a doubt:“When a task is suspended, it needs to temporarily save the task in the stack. If this task has a large amount of code, doesn’t it require a larger stack space?”

The answer: Not necessarily; the task code volume and stack size do not have a direct relationship.

Many beginners may have a misconception: saving a task means saving all the code of that task (in the stack).

The stack primarily saves the task’s own variables (control blocks) and other critical variable information, rather than saving all the code.

4How Much Stack Should Be Allocated?

The size of the task stack mainly depends on the number of temporary variables in your task.

Note: Temporary variables include all temporary variables from nested functions in your code.

For processors with relatively large RAM resources, you can allocate a bit more stack space.

However, often our RAM resources are relatively tight. In this case, you need to balance comprehensively.

For example, static local variables:

void AppTaskCheck(void *p_arg){  static uint8_t aaa;  // Static local variable
  (void)p_arg;
  for(;;)  {    // Code···  }}

The variable aaa here will not occupy the stack space of the task, but it will occupy global variable (RAM) space.

Whether to use static local variables or temporary variables depends on your project’s specific situation, such as: RAM resources, code execution efficiency, etc. (temporary variables will also involve a data copy process).

Therefore, how to allocate the stack, whether to use static or temporary variables, needs to be considered based on your project’s circumstances.

ENDAuthor: strongerHuangSource: Embedded ColumnCopyright belongs to the original author. If there is any infringement, please contact for deletion.Recommended ReadingHow is high-quality code written?Why are there more “top programmers” in Russia than in China?A freshman girl’s programming skills went viral, and everyone is now hooked…→ Follow for more updates ←

Leave a Comment