Linux 栈空间限制 (ulimit -a指令查看限制)(我的ubuntu下每个线程最多只能申请8M栈空间)
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 栈空间限制 (ulimit -a指令查看限制)(我的ubuntu下每个线程最多只能申请8M栈空间)相关的知识,希望对你有一定的参考价值。
我的测试代码:
开启两个线程,每个线程每秒申请1M的栈空间
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void testStackSize(int index)
char localValArr[1024*1024] = 0;
index++;
printf("%s %d num = %d!\\n", __FUNCTION__, __LINE__, index);
sleep(1);
testStackSize(index);
void *threadEntry(void *arg)
printf("%s %d!\\n", __FUNCTION__, __LINE__);
printf("New process: PID: %d,TID: %lu.\\n",getpid(),pthread_self());
testStackSize(0);
pthread_exit(NULL);
int main(int argc, char **argv)
pthread_t thread1;
pthread_t thread2;
pthread_create(&thread1, NULL, threadEntry, NULL);
pthread_create(&thread2, NULL, threadEntry, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
ubuntu上编译运行:
[root@ubuntu /arnold_test/test]37# gcc test_stack.c -lpthread
[root@ubuntu /arnold_test/test]38#
[root@ubuntu /arnold_test/test]38# ./a.out
threadEntry 17!
New process: PID: 35664,TID: 140456138036992.
threadEntry 17!
New process: PID: 35664,TID: 140456129644288.
testStackSize 10 num = 1!
testStackSize 10 num = 1!
testStackSize 10 num = 2!
testStackSize 10 num = 2!
testStackSize 10 num = 3!
testStackSize 10 num = 3!
testStackSize 10 num = 4!
testStackSize 10 num = 4!
testStackSize 10 num = 5!
testStackSize 10 num = 5!
testStackSize 10 num = 6!
testStackSize 10 num = 6!
testStackSize 10 num = 7!
testStackSize 10 num = 7!
段错误 (核心已转储)
测试发现,当每个线程申请到8M(以先申请到的为准)栈空间的时候,就会core dump(吐核)
用ulimit -a
查看我系统栈空间的限制:发现栈空间限制为8192kb,就是说每个线程最多只能申请8M的栈空间
[root@ubuntu /arnold_test/test]40# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 15445
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 15445
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
以上是关于Linux 栈空间限制 (ulimit -a指令查看限制)(我的ubuntu下每个线程最多只能申请8M栈空间)的主要内容,如果未能解决你的问题,请参考以下文章
Linux内核性能优化ulimit(nprocnofile)