Re: Stack Size of a thread

Kenneth Porter <[email protected]> Sun, 19 Oct 2003 13:47:23 -0700
Newsgroups gmane.linux.redhat.devel
Message-ID <7092548.1066571243@[10.0.0.221]>
--On Sunday, October 19, 2003 12:35 PM +0200 sting sting 
<[email protected]> wrote:

> As I know there is a default stack size for a pthread; but it grows and
> shrinks during run time.
> can I know the size of this stack during run time ?

I don't know about Linux, but under another OS (OS/2) the stack address 
space is allocated (ie. maximum size set but physical pages not assigned) 
when the thread is created. A "guard" page is created at the top of the 
stack, which initially is the first page. This is a hardware thing that 
causes a special page fault when an access is made, causing the kernel to 
assign a real page and make the next page a guard page. Eventually all the 
address space is used up and the stack can't grow any more. So the question 
becomes how to know how many actual pages are assigned.

If you just want to know how much stack is in use, you can at least read 
the stack pointer of another thread using debugger API's. (Reading a 
thread's own stack pointer is trivial.) I haven't done this but that's 
where I'd start.