ulimit -s
<value
>とLinux実装のスタックサイズ(スレッドレベル)との関係はどのようなものですか?スタック制限とスレッドの関係<number of threads
> *<each thread stack size
>有効な正当な理由はありませんか?以下のプログラムでは、各スレッドがchar [PTHREAD_STACK_MIN]を割り当て、10個のスレッドが作成されます。しかし、ulimitが10 * PTHREAD_STACK_MINに設定されている場合は、打ち切りのためにコアダンプしません。 stacksize(10 * PTHREAD_STACK_MINよりもはるかに小さい)のランダムな値に対しては、コアダンプが発生します。なぜそうなのか?
My Understandingは、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-一般的な#48-UbuntuのSMP水曜日11月24日午前10時14分11秒UTC 2010 x86_64のGNU/Linuxの
[Stack Clash](http://www.openwall.com/lists/oss-security/2017/06/19/1)修正後に 'rlimit_stack'を設定しようとすると、失敗や関連する問題が発生する可能性があります。 Red Hat [Issue 1463241](https://bugzilla.redhat.com/show_bug.cgi?id=1463241)も参照してください。 – jww