Relation between stack limit and threads

后端 未结 2 1828
温柔的废话
温柔的废话 2021-01-12 09:39
  1. What is the relationship between ulimit -s > and the stack size (at thread level) in the Linux implementation (or for that matter any OS)?

相关标签:
2条回答
  • 2021-01-12 09:57

    On UNIX/Linux, getrlimit(RLIMIT_STACK) is only guaranteed to give the size of the main thread's stack. The OpenGroup's reference is explicit on that, "initial thread's stack":

    http://www.opengroup.org/onlinepubs/009695399/functions/getrlimit.html

    For Linux, there's a reference which indicates that RLIMIT_STACK is what will be used by default for any thread stack (for NPTL threading):

    http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_create.3.html

    Generally, since the programmer can decide (by using nonstandard attributes when creating the thread) where to put the stack and/or how much stack to use for a new thread, there is no such thing as a "cumulative process stack limit". It rather comes out of the total RLIMIT_AS address space size.
    But you do have a limit on the number of threads you can create,sysconf(PTHREAD_THREADS_MAX), and you do have a lower limit for the minimum size a thread stack must have,sysconf(PTHREAD_STACK_MIN).

    Also, you can query the default stacksize for new threads:

    pthread_attr_t attr;
    size_t stacksize;
    if (!pthread_attr_init(&attr) && !pthread_attr_getstacksize(&attr, &stacksize))
        printf("default stacksize for a new thread: %ld\n", stacksize);
    

    I.e. default-initialize a set of pthread attributes and ask for what stacksize the system gave you.

    0 讨论(0)
  • 2021-01-12 09:57

    In a threaded program, stacks for all threads (except the initial one) are allocated out of the heap, so RLIMIT_STACK has little or no relation to how much stack space you can use for your threads.

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