[英]How is the stack allocated for an executable under Linux?
When a binary (C/C++) is executed under Linux, 在Linux下执行二进制(C / C ++)时
ulimit
, I can have a limit number and by using setrlimit
, I can modify it, but up to what limit, how can I determine it? 使用ulimit
,我可以有一个限制数,并且通过使用setrlimit
,我可以修改它,但是达到了什么限制,我该如何确定它? As you can see in the code below, I have recursively called func() for push operation only, and the stack grew up to around approximately 8 MB. 正如您在下面的代码中看到的那样,我只是递归调用func()进行推送操作,并且堆栈增长到大约8 MB左右。 And it crashed (stack overflow!). 它崩溃了(堆栈溢出!)。
void func()
{
static int i=0;
int arr[1024]={0};
printf("%d KB pushed on stack!\n",++i*sizeof(int));
func();
}
int main()
{
func();
return 0;
}
output snippet:
8108 KB pushed on stack!
8112 KB pushed on stack!
8116 KB pushed on stack!
8120 KB pushed on stack!
Segmentation fault (core dumped)
f
calls g
which in turn calls h
, depth is 3 calls, so 3 stack frames). 您可以尝试估计调用树的功能有多深( f
调用g
,其中调用h
,深度为3调用,因此3个堆栈帧)。
- How stack is initialized for its process? 如何为其进程初始化堆栈?
It depends on the architecture, but in general, the kernel allocates some virtual memory in your process's VM, and sets the stack pointer register to point to the top of it. 它取决于体系结构,但通常,内核在进程的VM中分配一些虚拟内存,并将堆栈指针寄存器设置为指向它的顶部。
- How stack grows and up to what limit? 堆栈如何增长并达到什么限制?
Every function call reserves more space on the stack using an architecturally defined procedures. 每个函数调用都使用体系结构定义的过程在堆栈上保留更多空间。 This is typically referred to as a "function prologue". 这通常被称为“功能序言”。
- Using ulimit, I can have limit number and using setrlimit, I can modify it but up to what limit, how can I determine it? 使用ulimit,我可以有限制数量并使用setrlimit,我可以修改它但是达到了什么限制,我该如何确定?
ulimit -s
will tell you the maximum stack size (in KB) for the current process (and all child processes which will inherit this value, unless overridden). ulimit -s
将告诉您当前进程(以及将继承此值的所有子进程,除非被覆盖)的最大堆栈大小(以KB为单位)。
- Does same stack size is allocated for all executing process? 是否为所有正在执行的进程分配相同的堆栈大小?
See previous answer. 见前一个答案。
Related: 有关:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.