Unix Programming - how much memory a pthread can define a variable in a stack?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > how much memory a pthread can define a variable in a stack?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how much memory a pthread can define a variable in a stack?
Jaguk Ku

2004-01-23, 4:57 pm

Hi there,

I have an simple application which define a struct variable in a function
which size is about 3 Megabyte.
It works.

But when it is defined in a pthread program. it just died.

My computer is Sun E450, Solaris 8, memory is 1024 Mega, swap 3 Giga.

What should i do?
Do i have to allocate a struct variable in a heap? or is there any runtime
option for a thread to get more stack memory?
what is the maximum amout of memory to define a variable in a stack usually?

Thanks in advance,

Jaguk Ku


DINH Viet Hoa

2004-01-23, 4:57 pm

Jaguk Ku wrote :
quote:

> Do i have to allocate a struct variable in a heap? or is there any runtime
> option for a thread to get more stack memory?
> what is the maximum amout of memory to define a variable in a stack usually?



create the attribute collection :

int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);

set the attribute :

int pthread_attr_setstacksize(pthread_attr_t
*attr, size_t stacksize);

and use the created attr in :

int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);

--
DINH V. Hoa,

"s/^\(\(\\.\|[^\[]\|\[\(\^.\|[^^]\)[^]]*\]\)*\)\(\\[^\*[]\)/\1\\\4/"
-- Stéphane CHAZELAS

DINH Viet Hoa

2004-01-23, 4:57 pm

Jaguk Ku wrote :
quote:

> Do i have to allocate a struct variable in a heap? or is there any runtime
> option for a thread to get more stack memory?
> what is the maximum amout of memory to define a variable in a stack usually?



create the attribute collection :

int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);

set the attribute :

int pthread_attr_setstacksize(pthread_attr_t
*attr, size_t stacksize);

and use the created attr in :

int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);

--
DINH V. Hoa,

"s/^\(\(\\.\|[^\[]\|\[\(\^.\|[^^]\)[^]]*\]\)*\)\(\\[^\*[]\)/\1\\\4/"
-- Stéphane CHAZELAS

David Schwartz

2004-01-23, 4:59 pm


"Jaguk Ku" <jkku@kynax.com> wrote in message
news:bpjrvi$7lo$1@news1.kornet.net...
quote:

> I have an simple application which define a struct variable in a function
> which size is about 3 Megabyte.
> It works.



This is very, *VERY* bad practice. What do you think the system should
do if there's less than 3 megabytes available?
quote:

> But when it is defined in a pthread program. it just died.



It's not guaranteed to work under any circumstances.
quote:

> My computer is Sun E450, Solaris 8, memory is 1024 Mega, swap 3 Giga.
>
> What should i do?



Use 'malloc' or 'new' or some appropriate memory allocation mechanism.
In addition, get in the habit of scanning your code regularly for excessive
stack consumers.
quote:

> Do i have to allocate a struct variable in a heap? or is there any runtime
> option for a thread to get more stack memory?



There is, but that's not a very good solution. Generally, the option
just reserves the address space, and you leave the system with no way to
tell you later that the memory isn't available.
quote:

> what is the maximum amout of memory to define a variable in a stack


usually?

It depends upon the function's intended use. It's okay to use more stack
in a function that's not going to be deep in a function call stack. I
recommend 8Kb maximum for a library function and 32Kb as an absolute
maximum.

DS


David Schwartz

2004-01-23, 4:59 pm


"Jaguk Ku" <jkku@kynax.com> wrote in message
news:bpjrvi$7lo$1@news1.kornet.net...
quote:

> I have an simple application which define a struct variable in a function
> which size is about 3 Megabyte.
> It works.



This is very, *VERY* bad practice. What do you think the system should
do if there's less than 3 megabytes available?
quote:

> But when it is defined in a pthread program. it just died.



It's not guaranteed to work under any circumstances.
quote:

> My computer is Sun E450, Solaris 8, memory is 1024 Mega, swap 3 Giga.
>
> What should i do?



Use 'malloc' or 'new' or some appropriate memory allocation mechanism.
In addition, get in the habit of scanning your code regularly for excessive
stack consumers.
quote:

> Do i have to allocate a struct variable in a heap? or is there any runtime
> option for a thread to get more stack memory?



There is, but that's not a very good solution. Generally, the option
just reserves the address space, and you leave the system with no way to
tell you later that the memory isn't available.
quote:

> what is the maximum amout of memory to define a variable in a stack


usually?

It depends upon the function's intended use. It's okay to use more stack
in a function that's not going to be deep in a function call stack. I
recommend 8Kb maximum for a library function and 32Kb as an absolute
maximum.

DS


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com