HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Can multiple tasks in an RTOS share one stack to save memory?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
canstackrtossaveonetasksmultiplesharememory

Problem

Many small embedded systems have a limited amount of RAM, 10k or less. I know you can run an RTOS on such systems, however, a realistic number of tasks in such a system is very low, mainly because every task needs its own stack area.

Is it possible to share the space needed for the stack of some of these tasks?

I understand that this is possible in a cooperative environment, such that a task can report its willingness to relinquish the processor (and refrain from using its stack). But in an RTOS you don't normally use cooperative multitasking.

But then, maybe it is sufficient to use non-preemptive tasks when sharing stack space, in a certain way that's also "cooperative" in my opinion.

Can it be done in an RTOS? How?

Solution

As long as they do not overlap in execution, the tasks can re-use the same stack space. That is, they can not safely execute in parallel or else one task will corrupt another's stack space.

This corruption may occur due to local variables or parameters stored on the stack, or even due to the occurrence of an ISR.

Hope this helps

Context

StackExchange Computer Science Q#29883, answer score: 4

Revisions (0)

No revisions yet.