使用setrlimit增加Linux中的堆栈大小 [英] Increase stack size in Linux with setrlimit
问题描述
阅读有关如何在编译时增加使用gnu编译的c ++应用程序的堆栈大小的信息,我了解可以在程序开始时使用setrlimit完成此操作.但是,我找不到任何成功的示例来说明如何使用它,以及在程序的哪一部分中应用它以便为c ++程序获得64M的堆栈大小,有人可以帮助我吗?
reading information about how to increase stack size for a c++ application compiled with gnu, at compilation time, I understood that it can be done with setrlimit at the beginning of the program. Nevertheless I could not find any successful example on how to use it and in which part of the program apply it in order to get a 64M stack size for a c++ program, could anybody help me?
感谢
推荐答案
通常,在调用任何其他函数之前,通常应尽早(例如,在main()
的开头)设置堆栈大小.通常,逻辑为:
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:
- 调用
getrlimit
以获得当前堆栈大小 - 如果当前大小<然后需要所需的堆栈大小
- 调用
setrlimit
将堆栈大小增加到所需大小
- call
getrlimit
to get current stack size - if current size < required stack size then
- call
setrlimit
to increase stack size to required size
在C语言中,可能会这样编码:
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\n", result); } } } // ... return 0; }
这篇关于使用setrlimit增加Linux中的堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- call
- 调用