Article count:10804 Read by:13623255

Account Entry

Interesting information | Is the size of the RTOS task stack related to the amount of code?

Latest update time:2022-09-03
    Reads:

A friend asked this question: I have a task with a lot of code. Do I need to allocate a large stack for this task?


In fact, the larger the amount of code, the more stack space is allocated. It mainly depends on the "temporary variables" contained in your task.

1 RTOS task stack allocation

Many RTOS tasks on the market require the stack size to be allocated in advance, that is, the stack size is allocated when the task is created.
For example, uCOS creates a Check task:
// 任务优先级#define TASK_CHECK_PRIO                        6
// 任务堆栈大小#define TASK_CHECK_STK_SIZE 128
// 堆栈OS_STK TaskCheckStk[TASK_CHECK_STK_SIZE];
// 创建任务 - 信号检测OSTaskCreateExt((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));
// 任务应用实现void AppTaskCheck(void *p_arg){  // 代码···
(void)p_arg;
for(;;) {    // 代码··· }}
FreeRRTOS creates a task similarly, also allocating the stack size when creating:
#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, there are also message queues, message mailboxes, etc. that also need to allocate stacks in advance.
For example, FreeRTOS creates a CLI message queue:
#define CLI_QUEUE_NUM             256                      //CLI接收队列数#define CLI_PACKAGE_LEN           2                        //CLI数据包长度
QueueHandle_t xCLIRcvQueue = NULL;
/* 创建队列 */if(xCLIRcvQueue == NULL){  xCLIRcvQueue = xQueueCreate(CLI_QUEUE_NUM, CLI_PACKAGE_LEN);}

This is the allocation stack for creating tasks (or queues). As for the specific amount of allocation, it depends on your actual situation. I will describe it in the following chapters.

2 Task code amount

The amount of code for a task is the code called in your task.
For example, the code in the above example:
// 任务应用实现void AppTaskCheck(void *p_arg){  // 代码···
(void)p_arg;
for(;;) { // 代码··· }}

There may be thousands of lines of code written here, or hundreds of functions called, each of which contains a lot of code.

In this way, the amount of code for this task is very large.

3 Is there a relationship between the amount of task code and the stack size?

Many people have a question: when a task is suspended, it is necessary to temporarily save the task in the stack. If the task has a large amount of code, does it require a large stack space?

Answer: It does not necessarily require a large stack space, and the amount of task code is not directly related to the stack.

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

The stack mainly saves the variables (control blocks) of the task itself, as well as temporary variables and other key variable information, rather than saving all the code.

4 How big is the appropriate size for stack allocation?

The size of the task stack mainly depends on the number of [temporary variables] in your task.
Note: Temporary variables include temporary variables in all nested functions in your code.
For processors with relatively large RAM resources, you can try to allocate more stack resources.
However, in many cases, our RAM resources are relatively tight. At this time, you need to strike a comprehensive balance.
For example, static local variables:
void AppTaskCheck(void *p_arg){  static uint8_t aaa;  //静态局部变量
(void)p_arg;
for(;;) { // 代码··· }}
The aaa variable here will not occupy the stack space of the task, but it will occupy the global variable (RAM) space.
Whether to use static local variables or temporary variables depends on the specific situation of your project, such as RAM resources, code running efficiency, etc. (Temporary variables will also have a data copy process)
Therefore, how to allocate the stack and whether to use static or temporary variables requires comprehensive consideration of the situation of your project.


Add WeChat and reply " join group"

Invite you to join the technical exchange group!

Domestic chips|Automotive electronics|Internet of Things|New energy|Power supply|Industry|Embedded...

Reply to any content you want to search in the official , such as question keywords, technical terms, bug codes, etc., and you can easily get feedback on related professional and technical content . Go and try it!


If you want to see our articles more often, you can go to our homepage, click the "three dots" in the upper right corner of the screen, and click "Set as Star".

Welcome to scan the QR code to follow us


Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号