Normally you would set the stack size early on, e,g, at the start of main()
, before calling any other functions. Typically the logic would be:
- call
getrlimit
to get current stack size
- if current size < required stack size then
- call
setrlimit
to increase stack size to required size
In C that might be coded something like this:
#include <sys/resource.h>
#include <stdio.h>
int main (int argc, char **argv)
{
const rlim_t kStackSize = 64L * 1024L * 1024L; // min stack size = 64 Mb
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
fprintf(stderr, "setrlimit returned result = %d
", result);
}
}
}
// ...
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…