简体   繁体   English

如何在Linux下为可执行文件分配堆栈?

[英]How is the stack allocated for an executable under Linux?

When a binary (C/C++) is executed under Linux, 在Linux下执行二进制(C / C ++)时

  1. How is the stack initialized for the process? 如何为进程初始化堆栈?
  2. How does the stack grow and up to what limit? 堆栈如何增长并达到什么限制?
  3. Using 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 ,我可以修改它,但是达到了什么限制,我该如何确定它?
  4. Is the same stack size allocated for all executing processes? 是否为所有正在执行的进程分配相同的堆栈大小?

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)
  1. Where did these approximately 8 MB come from? 这些大约8 MB来自哪里?
  1. Stack is one of the various memory region that is associated to a process at startup time and may vary during runtime. 堆栈是在启动时与进程关联的各种内存区域之一,并且可能在运行时期间发生变化。 Others can be text/code, heap, static/bss, etc. 其他可以是文本/代码,堆,静态/ bss等。
  2. Each time you call a function the stack grows. 每次调用函数时,堆栈都会增长。 A stack frame is added on top of it. 在其上添加堆栈帧。 A stack frame is what is necessary to a given function to be executed (parameters, return value, local variables). 堆栈帧是给定函数执行所必需的(参数,返回值,局部变量)。 Each time you return from a function, the stack shrinks by the same amount it grew. 每次从函数返回时,堆栈的缩小量都会增加。
  3. You can try to estimate how deep you function call tree will be ( f calls g which in turn calls h , depth is 3 calls, so 3 stack frames). 您可以尝试估计调用树的功能有多深( f调用g ,其中调用h ,深度为3调用,因此3个堆栈帧)。
  4. Yes there is a default value that was estimated by OS designers. 是的,OS设计师估计有一个默认值。 That size is in general sufficient. 这个大小通常就足够了。
  5. This is a default constant associated to your OS. 这是与您的操作系统关联的默认常量。
  1. 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中分配一些虚拟内存,并将堆栈指针寄存器设置为指向它的顶部。

  1. 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". 这通常被称为“功能序言”。

  1. 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为单位)。

  1. Does same stack size is allocated for all executing process? 是否为所有正在执行的进程分配相同的堆栈大小?

See previous answer. 见前一个答案。

Related: 有关:

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

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