Solaris 2.5.1 - Setting Stack Size


REFERENCE: man ulimit, Advanced Programming in the UNIX Environment by W. Richard Stevens, Addison Wesley, 1992

A Brief Memory Review


At the bottom we have the program instructions, also called the text segment. This is probably a read only segment of memory (so that the program instructions can't overwrite themselves).

Above the text segment is the static data segment. (static refers to variables that are declared to be of a static size during the compile) Historically this comes in two blocks - initialized data and uninitialized data. Both contain the addresses for variables defined outside of any function. (This reflects the C programming language, by the way) So a line like ...

	int a = 10;
would create a spot for variable a in the initialized data segment. A line like ...
	int a[100];
would create a spot for 100 integers in the uninitialized data segment. This data may (or may not) be initialized when the program begins execution.

Above the static memory segment is the dynamic memory segment, or heap. If you need to declare a block of memory of a size that is determined at run time, you will probably use some form of the "malloc" function when programming in C. The heap grows upward toward the stack.

Above the heap is the stack. The stack "is where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to [sic], and certain information about the caller's environment (such as some of the machine registers) is saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. By utilizing a stack in this fashion, C functions can be recursive." (Stevens 167-168)

The final segment is where your environment variables are stored. These are things like "DISPLAY" or "PRINTER" or "PATH". You can see them all with the "env" command. Also stored here are the command line arguments - things you pass to the program from the command line. Usually these are referred to as the "argv[]" variables. There are argc of them.



Ok, so suppose you get a seg fault in a highly recursive program. Using your debugger you discover the memory problem to be a line that just should not be having a memory problem. Diagnosis. The stack ran into the heap and things are just not going to work. What can you do?

Increasing the Size of the Stack

On a Solaris system you can see the stack size via ...
	ulimit -Ss
The default limit on Solaris is only 8k.

You can reset the stack size (if you have root permission) to anything less than the hardware limit. To find out the hardware limit ...

	ulimit -Hs
To change the current stack size:
	ulimit -Ss size
The size has to be a multiple of eight, or maybe only four.

You can test your stack setting with the following program. If you double your stack size, say from 8192 to 16384 you should notice the count of the program will roughly double. I wouldn't run this program with a stack much larger than 16384 as it may take a while before you get the seg fault.

void a(int c)
{
  c++;
  printf("c=%d\n",c);
  a(c);
}

main ()
{
  a(0);
}
On Linux systems the default limit of the stack is supposedly 2MB, but my installations did not have the ulimit command.

Return to Gene's Home Page
Return to Gene's Random Unix Crap