So we have a program on Solaris which was running out of stack space.
While investigating this, I had a brief look at what ulimit was for the stack:
user@solaris-box:~$ ulimit -a
...
stack size (kbytes, -s) 8192
So the stack size limit is 8 megabytes. But is this the limit for the whole process?
What if my process has 10 threads, are they are only allowed 819k per thread? (or some mix thereof, up to 8MiB?)
I can't find any doco on this.
Best Answer
SUMMARY
For the main thread, you have to call
setrlimit()
(perhaps by usingulimit
) before the process is started in order to ensure the larger stack size is effective.For threads started by the process, you need to use
pthread_attr_setstacksize()
as the thread stacksize is not affected by thestack size
resource limit fromsetrlimit()
/getrlimit()
at all.The code needs to look something like this:
You can get the thread stack size from the current stack size limit:
(Note that if you're using a library that creates its own threads, that library may have its own documented method of setting thread stack size, such as OpenMPI.)
DETAILED ANSWER
Resource limits are set from the command line by the
ulimit
utility.If you run
truss -f -a -vall -o /tmp/truss.out /usr/bin/ulimit -a
, you'll seeAnd if you look into
/tmp/truss.out
, you'll seeWe see that
ulimit
uses thegetrlimit()
(andsetrlimit()
) library functions to get/set resource limits.Per the
getrlimit()
/setrlimit()
man
page (note the bolded parts):So, the stacksize for threads created by the process that are not the main thread is not affected by the process's
RLIMIT_STACK
resource limit. And you have to callsetrlimit()
BEFORE the process starts (in the parent process) in order to ensure that any larger stack size limit is actually effective.Per the
pthread_create()
man page: