Page 1 of 1

Amount of stack space reserved for an applitacion program?

Posted: Wed Mar 17, 2004 12:00 am
by pepito
Hello OS developers:

I am programming the EXEC service of my OS, this service load an executable file into the memory, create a process (TSS, LDT, etc.) and start the execution of the process (including it into a round-robin).

I complie all the 'application programs' as 'plain binary format' with a very basic header, and I realize that the result file have just the .text and .data sections, but not the .bss section. Then I must reserve some memory for this section dynamically.

My question is about the STACK space:

Who must determinate the amount of stack space reserved for an applitacion program?

1) The programmer: When the application is created/compiled.
2) The user of the OS: Sending the stack size as parameter when the EXEC service is called.
3) The OS itself: Reserving an arbitrary amount of stack space for each process.

Some body have resolved this problem yet?
Any idea?

Thank you very much,

pepito

RE:Amount of stack space reserved for an applitacion program

Posted: Wed Mar 17, 2004 12:00 am
by gaf
Hi pepito,
from you're post I assume you're using paging because if you were using segments you'd probably not ask that question. You're processes will probably have a fixed size memory area for their own - most likely 2^32 bytes.
The application itself should be loaded at the very beginning of the processes address space, right after it starts the heap and the stack starts at the very top of the address space.

o-----------------------------------------------------------------------------o
|CODE/DATA | heap               <unmapped>                              stack |
o-----------------------------------------------------------------------------o

In between the heap and the stack is a big area of unused space which can be used by both of them. The heap will grow to the upwards und the stack downwards - so they may (in theory) meet somewhere in the middle. Note that the array between the heap and the stack is not mapped and therfore doesn't consume any real memory.

regards,
gaf

RE:Amount of stack space reserved for an applitacion program

Posted: Thu Mar 18, 2004 12:00 am
by Gandalf
hi pepito,

I agree with what gaf says. Actually from implementation point of view your heap is in a separate space and stack is in a separate space.

So it is upto u to decide how much space is necessary. In our Dynacube OS we give a initial stack space of 200KB(Yea it is bit too much - but its ok for the time being).

U could give some space say (4k * X) space at the start. Eventually if ur application needs more space (i.e. if it crosses your stack limit in ur Stack descriptor - then it will surely result in a fault). So u could handle that fault and give more space to the stack (I haven't done this - so its just an idea).

Hope it helps.

rgds
Gandalf

RE:Amount of stack space reserved for an applitacion program

Posted: Thu Mar 18, 2004 12:00 am
by common
I do this in my OS.  I give 8kB intially for a stack, then handle the fault by marking the new page as read/write/present and allocating memory for it.  After that time, I return back to the application and it continues happily as normal.  This is known as "Automatic Stack Expansion."

RE:Amount of stack space reserved for an applitacion program

Posted: Thu Mar 18, 2004 12:00 am
by Adek336
Having 2048 Mib reserved for the app space and having a demand-paging mechanism, I merrily allocate 1Mib for each thread's stack (a process may have plenty of them). The last page is a trap to crash the app. Well, usually the app tries to access 4-12 Kib which get allocated through lazy paging.

Cheers