简体   繁体   English

如何在C中增加正在运行的程序的堆栈大小

[英]how to increase Stack Size of a running program in C

I am trying to identify any way if we can increase the stack size of my running program after getting SIGSEGV. 我正在尝试找到任何方法来获取SIGSEGV后是否可以增加正在运行的程序的堆栈大小。 I know we can increase the size of stack by ulimit -c but, that did not solve this problem. 我知道我们可以通过ulimit -c增加堆栈的大小,但这并不能解决此问题。 Because my process is already dead. 因为我的过程已经死了。 I want to handle this situation where my process will not get killed even after segfault. 我想处理这种情况,即使经过段错误,我的进程也不会被杀死。 setrlimit() is one way which can be used for exceed stack size statically. setrlimit()是可用于静态超出堆栈大小的一种方法。 But I don't want to block more memory than I need. 但是我不想阻塞更多的内存。

Under Linux, setting a higher limit for the stack size does not commit this memory to your process. 在Linux下,为堆栈大小设置更高的限制不会将此内存提交给您的进程。 The memory will be paged in as required. 内存将根据需要分页。

The default stack size in quite large already. 默认堆栈大小已经很大。 You should run under the debugger or produce a core upon SIGSEGV to analyze what is really going on. 您应该在调试器下运行,或者在SIGSEGV上生成一个内核来分析实际发生​​的情况。 You might have a very deep recursion, or an inordinate amount of local variable space, possibly via VLAs allocated by mistake. 您可能有很深的递归,或者局部变量空间过大,可能是由于错误分配的VLA引起的。 Increasing the stack space may hide the problem for a while, but it is not a reliable solution. 增加堆栈空间可能会暂时隐藏该问题,但这不是可靠的解决方案。

A recent OS will not automatically reserve the memory for the stack, but just add pages as required. 最新的操作系统不会自动为堆栈保留内存,而是仅根据需要添加页面。 The ulimit is just an upper bound you allow it to use up. ulimit只是您允许其用完的上限。 So increasing the stack size statically should be no problem and exactly what you want. 因此,静态增加堆栈大小应该没问题,这正是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM