What does “ulimit -s unlimited” do?

后端 未结 3 1661
遇见更好的自我
遇见更好的自我 2020-12-02 15:08

There are understandably many related questions on stack allocation

What and where are the stack and heap?

Why is there a limit on the stack size?

Si

相关标签:
3条回答
  • 2020-12-02 15:54

    When you call a function, a new "namespace" is allocated on the stack. That's how functions can have local variables. As functions call functions, which in turn call functions, we keep allocating more and more space on the stack to maintain this deep hierarchy of namespaces.

    To curb programs using massive amounts of stack space, a limit is usually put in place via ulimit -s. If we remove that limit via ulimit -s unlimited, our programs will be able to keep gobbling up RAM for their evergrowing stack until eventually the system runs out of memory entirely.

    int eat_stack_space(void) { return eat_stack_space(); }
    // If we compile this with no optimization and run it, our computer could crash.
    

    Usually, using a ton of stack space is accidental or a symptom of very deep recursion that probably should not be relying so much on the stack. Thus the stack limit.

    Impact on performace is minor but does exist. Using the time command, I found that eliminating the stack limit increased performance by a few fractions of a second (at least on 64bit Ubuntu).

    0 讨论(0)
  • 2020-12-02 15:59

    stack size can indeed be unlimited. _STK_LIM is the default, _STK_LIM_MAX is something that differs per architecture, as can be seen from include/asm-generic/resource.h:

    /*
     * RLIMIT_STACK default maximum - some architectures override it:
     */
    #ifndef _STK_LIM_MAX
    # define _STK_LIM_MAX           RLIM_INFINITY
    #endif
    

    As can be seen from this example generic value is infinite, where RLIM_INFINITY is, again, in generic case defined as:

    /*
     * SuS says limits have to be unsigned.
     * Which makes a ton more sense anyway.
     *
     * Some architectures override this (for compatibility reasons):
     */
    #ifndef RLIM_INFINITY
    # define RLIM_INFINITY          (~0UL)
    #endif
    

    So I guess the real answer is - stack size CAN be limited by some architecture, then unlimited stack trace will mean whatever _STK_LIM_MAX is defined to, and in case it's infinity - it is infinite. For details on what it means to set it to infinite and what implications it might have, refer to the other answer, it's way better than mine.

    0 讨论(0)
  • 2020-12-02 16:00

    ulimit -s unlimited lets the stack grow unlimited.

    This may prevent your program from crashing if you write programs by recursion, especially if your programs are not tail recursive (compilers can "optimize" those), and the depth of recursion is large.

    0 讨论(0)
提交回复
热议问题