[En-Nut-Discussion] Global variables and threads

Anton Veldhuizen emtech at telkomsa.net
Mon Sep 19 16:10:18 CEST 2005


Hi all, 

My problem: 
My program crashes at random intervals.  In the one instance I allocate a 2k
stack for a thread and the crash happens almost instantly upon startup.  If
I allocate 1k for the same thread's stack the system runs quite happily.  In
both cases NutHeapAvailable() indicate that I have > 10K memory available.
The one thing I realize that could be a potential problem is that I use
large global structs (approx. 3.8K) 

Questions: 
I have read all the documents I could find, but I am still not clear on how
global variables are treated by Nut/OS. 

1) Are global variables copied into a threads stack space when the thread is
started, or do they simply occupy the space in which they were allocated
upon startup for all threads to share? 
2) Can I assign thread stack sizes of any size (up to 28K), permitted the
memory is available? 
3) Does the main thread (that spawns other threads) require a larger stack
than any other individual stack in order to spawn that new threads stack? 
4) I use fgetc() to capture uart0 and uart1 in two seperate threads, each
having a stack of 1K, each stack accesses global arrays of 350K+350K in
total.  I have read that when an interrupt occurs the interrupt will use the
threads stack space.  Does this apply to fgetc()?  Does this mean that if
the interrupt occurs at a time when the I am in a small thread such as Idle
(384 byte stack) that I will face a memory overrun? 

P.S. 
I open my uart as follow: 

NutRegisterDevice(&devUsartAvr0, 0, 0); Usart0FilePtr = fopen("uart0",
"w+"); _ioctl(_fileno(Usart0FilePtr), UART_SETSPEED, &commsBaud);
_ioctl(_fileno(Usart0FilePtr), UART_SETCOOKEDMODE, &cookedMode); 

The above code transmits one char, apparently a zero.  Any idea on how to
stop this from happening (I set my baud to 9600)? 

Best regards,
Anton Veldhuizen 




More information about the En-Nut-Discussion mailing list