0% found this document useful (0 votes)
29 views9 pages

Task Stacks: Neethu Anie Saji Roll No:13 S2Pg

Each task requires its own stack space, which can be allocated statically or dynamically. Statically allocated stacks are declared outside functions using the OS_STK data type. Dynamically allocated stacks use malloc() but may cause fragmentation over time. The stack pointer passed to task creation depends on whether the stack grows up or down. Portable code can check the stack growth setting. Proper stack sizing accounts for function nesting, variables, interrupts, and registers.

Uploaded by

Neena Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views9 pages

Task Stacks: Neethu Anie Saji Roll No:13 S2Pg

Each task requires its own stack space, which can be allocated statically or dynamically. Statically allocated stacks are declared outside functions using the OS_STK data type. Dynamically allocated stacks use malloc() but may cause fragmentation over time. The stack pointer passed to task creation depends on whether the stack grows up or down. Portable code can check the stack growth setting. Proper stack sizing accounts for function nesting, variables, interrupts, and registers.

Uploaded by

Neena Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

TASK STACKS

NEETHU ANIE SAJI


ROLL NO:13
S2PG
Task Stacks
Each task must have its own stack
space.
A stack must be declared as being of
type OS_STK and must consist of
contiguous memory locations
Can either allocate stack space
statically or dynamically.
Both of these declarations are made
outside a function.
A static stack declaration looks as shown
below:

static OS_STK MyTaskStack[stack_size];


Or
OS_STK MyTaskStack[stack_size];
Can allocate stack space dynamically by
using the C compilers malloc() function
If you create and delete tasks then,
eventually, your memory allocator may not
be able to return a stack for your task(s)
because the heap gets fragmented.

OS_STK *pstk;
pstk = (OS_STK *)malloc(stack_size);
If (pstk != (OS_STK *)0) {
Create the task;
}
Fragmentation:

Figure illustrates a heap containing 3 Kbytes or


available memory that can be allocated with
malloc()
C/OS-II supports processors that have stacks that grow
from either high memory to low memory or from low
memory to high memory.
When OS_STK_GROWTH is set to 0 in OS_CPU.H, you
need to pass the lowest memory location of the stack to the
task create function.

OS_STK TaskStack[TASK_STACK_SIZE];
OSTaskCreate(task, pdata, &TaskStack[0], prio);

When OS_STK_GROWTH is set to 1 in OS_CPU.H, you


need to pass the highest memory location of the stack to
the task create function

OS_STK TaskStack[TASK_STACK_SIZE];
OSTaskCreate(task, pdata,
&TaskStack[TASK_STACK_SIZE-1], prio);
This requirement affects code portability.
If you need to port your code from a processor
architecture that supports a downward growing
stack to an upward growing stack ie,Supporting
stacks which grow in either direction.

OS_STK TaskStack[TASK_STACK_SIZE];
#if OS_STK_GROWTH == 0
OSTaskCreate(task, pdata, &TaskStack[0], prio);
#else
OSTaskCreate(task, pdata, &TaskStack[TASK_STACK_SIZE1], prio);
#endif
The size of the stack needed by your task
is application specific.
When sizing the stack, however, you
must account for
Nesting of all the functions called by your task,
The number of local variables that will be
allocated by all functions called by your task
and,
The stack requirements for all nested interrupt
service routines.
In addition, your stack must be able to
store all CPU registers.
THANK YOU

You might also like