The relationship between stack limit and threads

  • What is the relationship between ulimit -s <value> and the stack size (at the thread level) in the Linux implementation (or, for that matter, some OS)?

    Is <number of threads> * <each thread stack size> there must be less < stack size assigned by ulimit command> a valid rationale?

  • In the program below, each thread allocates char [PTHREAD_STACK_MIN] and 10 threads are created. But when ulimit is set to 10 * PTHREAD_STACK_MIN, it does not shut down due to interruption. For some random stacksize value (much less than 10 * PTHREAD_STACK_MIN), these are kernel dumps. Why is that?

My understanding is that stacksize represents a stack occupied by all threads in the summation for a process.

Theme function

#include <cstdio>  
#include <error.h>  
#include <unistd.h>  
#include <sys/select.h>  
#include <sys/time.h>  
#include <sys/resource.h>  
using namespace std;        
#include <pthread.h>  
#include <bits/local_lim.h>  

const unsigned int nrOfThreads = 10;  
pthread_t  ntid[nrOfThreads];


void* thr_fn(void* argv)
{
    size_t _stackSz;  
    pthread_attr_t _attr;  
    int err;

    err = pthread_attr_getstacksize(&_attr,&_stackSz);
    if( 0 != err)
    {
        perror("pthread_getstacksize");
    }

    printf("Stack size - %lu, Thread ID - %llu, Process Id - %llu \n", static_cast<long unsigned int> (_stackSz), static_cast<long long unsigned int> (pthread_self()), static_cast<long long unsigned int> (getpid()) );


    //check the stack size by actual allocation - equal to 1 + PTHREAD_STACK_MIN
    char a[PTHREAD_STACK_MIN ] = {'0'};

    struct timeval tm;
    tm.tv_sec = 1;
    while (1)
        select(0,0,0,0,&tm);

    return ( (void*) NULL);
} 

Main function

int main(int argc, char *argv[])

{  

    struct rlimit rlim;
    int err;

    err = getrlimit(RLIMIT_STACK,&rlim);
    if( 0 != err)
    {
        perror("pthread_create ");
        return -1;
    }

    printf("Stacksize hard limit - %ld, Softlimit - %ld\n", static_cast <long unsigned int> (rlim.rlim_max) , 
            static_cast <long unsigned int> (rlim.rlim_cur));

    for(unsigned int j = 0; j < nrOfThreads; j++)
    {
        err = pthread_create(&ntid[j],NULL,thr_fn,NULL);
        if( 0 != err)
        {
            perror("pthread_create ");
            return -1;
        }
    }

    for(unsigned int j = 0; j < nrOfThreads; j++)
    {
        err = pthread_join(ntid[j],NULL);
        if( 0 != err)
        {
            perror("pthread_join ");
            return -1;
        }
    }

    perror("Join thread success");

    return 0;
}

PS:
Ubuntu 10.04 LTS, .
Linux- 2.6.32-26-generi# 48-Ubuntu SMP Wed Nov 24 10:14:11 UTC 2010 x86_64 GNU/Linux

+5
2

UNIX/Linux getrlimit(RLIMIT_STACK) . OpenGroup , " ":

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

Linux , , RLIMIT_STACK ( NPTL):

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

, ( ), / , , " " ". RLIMIT_AS.
, , sysconf(PTHREAD_THREADS_MAX), , , sysconf(PTHREAD_STACK_MIN).

, :

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);

.. default - pthread , .

+5

( ) , RLIMIT_STACK , .

-1

All Articles