[SOLVED]Task Memory management

Thread Starter

Pushkar1

Joined Apr 5, 2021
401
I found that schendular can be written in many ways. I am curious to know when should we use malloc I think if we are designing scheduler for 8 bit micro then we should create task statically Because there is less space in the 8 bit microcontroller. If we are using 32 bit micro and we have more memory then we can use malloc function. does this really happen?
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
401
Embedded apps are typically not creating and deleting tasks on the fly, so static allocation is appropriate.

Bob
It seems to me that your statement is true only for 8 bit microcontrollers. Tasks can be created dynamically for 32bit micros. In FreeRTOS I see that malloc function is used to create tasks dynamically. is this statement true
 

BobTPH

Joined Jun 5, 2013
4,030
It has nothing to do with 8-bit vs 32-bit micros. Either you need to create tasks on the fly or you don’t. Just because the OS offers it doesn’t mean you should use it.

Bob
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
401
It has nothing to do with 8-bit vs 32-bit micros. Either you need to create tasks on the fly or you don’t. Just because the OS offers it doesn’t mean you should use it.

Bob
In your view it seems that if we want to design preemptive scheduler then tasks have to be created statically.

I think Arrays or structures is a better way to create tasks statically.
 

Papabravo

Joined Feb 24, 2006
17,240
In your view it seems that if we want to design preemptive scheduler then tasks have to be created statically.

I think Arrays or structures is a better way to create tasks statically.
A preemptive scheduler can work with either static or dynamically allocated task data. This is never about the code which is always static and reentrant. Arrays and structures can be allocated statically as you seem to be aware, but they can also be allocated and used dynamically. They are not one or the other, and for the most part you seem to have a tendency to state opinions that have no basis in reality. Try to be open to the possibility that things you cannot imagine might be possible.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
401
Arrays and structures can be allocated statically as you seem to be aware, but they can also be allocated and used dynamically.
Agreed array and Structures can also be created dynamically. But As @BobTPH said, a dynamically created tasks are not suitable for embedded systems. I think because of memory fragments. So Static memory is better option.

Array is used to store multiple vaule with same data type while structure is used to store value of multiple data type in c language.

If I am designing preemptive scheduler. Should I use statically arrays or should I use the statically structure. What would you recommend?
 

djsfantasi

Joined Apr 11, 2010
7,913
There isn’t a yes/no answer to this question. There are multiple answers, all which are dependent on the environment. For example, you’ve dismissed one solution because it might fragment memory. But you don’t know how long a defragmenter / garbage collection would take and how often it would need to be called. And if your “time budget” would allow the use of one?
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
401
There isn’t a yes/no answer to this question. There are multiple answers, all which are dependent on the environment.
I don't know what you mean by environment are you asking me about compilers and hardware. I use ESP32 and Arduino IDE.

For example, you’ve dismissed one solution because it might fragment memory. But you don’t know how long a defragmenter / garbage collection would take and how often it would need to be called. And if your “time budget” would allow the use of one?
I chose static memory because previous members advised me to use static memory instead of dynamic. What's your point

I have also read in many posts and articles that it is not useful to use dynamic memory for embedded systems.
 

Papabravo

Joined Feb 24, 2006
17,240
Everything should be made as simple as possible, but no simpler.”
-- A. Einstien

So it is in physics, thus it should be in software systems.
 

djsfantasi

Joined Apr 11, 2010
7,913
I don't know what you mean by environment are you asking me about compilers and hardware. I use ESP32 and Arduino IDE.


I chose static memory because previous members advised me to use static memory instead of dynamic. What's your point

I have also read in many posts and articles that it is not useful to use dynamic memory for embedded systems.
By environment, I mean the application, why it needs to schedule tasks and which scheduling method is appropriate for the application, the requirements, as in memory per task, timing constraints, time budget (I.e., if you have 10 tasks which must execute each second and each task takes 10 seconds, is this possible?).

I am NOT talking about compilers and hardware. I shouldn’t have used the word environment, because as you correctly pointed out, it is used for compilers/hardware
 

nsaspook

Joined Aug 27, 2009
8,898
In your view it seems that if we want to design preemptive scheduler then tasks have to be created statically.

I think Arrays or structures is a better way to create tasks statically.
https://www.embeddedcomputing.com/t...ramming/dynamic-memory-allocation-just-say-no
However, despite their validity, the problems may not always be as significant as they seem:

  • The function returns a NULL pointer if allocation failure occurs. This is easy to check, and action may be taken.
  • In many applications, all memory allocation and deallocation is performed in a single task. This renders reentrancy unnecessary.
  • Not all embedded systems are real time, so determinism may not be required.
It's common to use a malloc like function to create the needed memory elements of the program in an init section to handle boot configuration switch settings selecting various modes and devices during startup but to 'never' deallocate those elements later. On a 32-bit processor with an MMU it's possible to have VM address translation and paging to eliminate fragmented memory pools but virtual memory is the enemy of Real-Time. So even on systems like Linux with a full VM-paged-swapped memory sub-system 'Real-time' tasks resources are locked into memory.

PIC32MZ MMU used by OS
https://github.com/sergev/LiteBSD
LiteBSD is a variant of 4.4BSD operating system adapted for microcontrollers. Currently, only the Microchip PIC32MZ family is supported as a target. It is equipped with MMU with paging support, and 512kbytes of on-chip RAM. These resources are enough to build a compact networked embedded system.
 
Last edited:

Thread Starter

Pushkar1

Joined Apr 5, 2021
401
By environment, I mean the application, why it needs to schedule tasks and which scheduling method is appropriate for the application,
Which Schenduling Algorithm is appropriate, it will be known only after knowing all the algorithms. I just tried to learn Prempative Sachenduling

the requirements, as in memory per task, timing constraints, time budget (I.e., if you have 10 tasks which must execute each second and each task takes 10 seconds, is this possible?).
i don't think it is possible
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
401
It should be of no concern to the scheduler whether the tasks are statically or dynamically allocated.
I agree and in my first post I have clarified that it depends on the memory space. If you have a large memory then you can use dynamic memory. If you have less memory you can use static memory. I have given two examples for the memory 8 bit micro for small memory and 32 bit micro for large
 
Top