-
ulimit -s 之间有什么关系<value
> 以及 Linux 实现(或任何操作系统)中的堆栈大小(在线程级别)?
Is <number of threads
> * <each thread stack size
> 必须小于< stack size assigned by ulimit command
> 合理的理由?
在下面的程序中 - 每个线程分配 char [PTHREAD_STACK_MIN] 并创建 10 个线程。但当 ulimit 设置为 10 * PTHREAD_STACK_MIN 时,它不会因中止而进行 coredump。对于 stacksize 的某个随机值(远小于 10 * PTHREAD_STACK_MIN),它会进行核心转储。为什么这样?
我的理解是 stacksize 代表进程中所有线程占用的堆栈总和。
线程功能
#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);
}
主功能
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-generic #48-Ubuntu SMP 11 月 24 日星期三 10:14:11 UTC 2010 x86_64 GNU/Linux