thread线程栈size及局部变量最大可分配size【转】
转自:http://blog.csdn.net/sunny04/article/details/46805261
版权声明:本文为博主原创文章,未经博主允许不得转载。
进程是操作系统的最小资源管理单元, 线程是操作系统最小的执行单元。 一个进程可以有多个线程, 也就是说多个线程分享进程的资源,包括栈区,堆区,代码区,数据区等。
- sundh@linhaoIPTV:~$ 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) 31675
- 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) 31675
- virtual memory (kbytes, -v) unlimited
- file locks (-x) unlimited
32bit x86机器上面,执行ulimit -a的命令, 可以看到
stack size (kbytes, -s) 8192 这是否说明在线程中可以分配8M的局部变量(或者说分配7M的局部变量,还有1M的空间存储其他的局部变量或者寄存器状态信息(例如bp等)或者函数压栈信息等)
写代码验证如下:
- //test2.cpp
- #include <iostream>
- #include <string.h>
- #include <pthread.h>
- #include <stdio.h>
- using namespace std;
- void* func(void*a)
- {
- cout << "enter func" << endl;
- cout << "enter func" << endl;
- int b[1024*1024*2] = {0};
- }
- int main()
- {
- int a[1024*1024*3/2]={0};
- pthread_t pthread ;
- pthread_create(&pthread, NULL, func, NULL);
- cout << "This is a test" << endl;
- //pthread_join(pthread, NULL);
- return 0;
- }
g++ -g -o test2 test2.cpp -lpthread
sundh@linux:~$ gdb test2
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/sundh/test2...done.
(gdb) r
Starting program: /home/sundh/test2
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
[New Thread 0xb7cc1b40 (LWP 10313)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7cc1b40 (LWP 10313)]
func (a=0x0) at test2.cpp:10
10 cout << "enter func" << endl;
(gdb)
GDB调试结果如上。 main() 函数中分配1.5M的局部变量,是不会报错的, 但子线程中分配2M的局部变量,就会coredump。 可见,只能分配不大于2M的局部变量,但ulimit -a 查询到的stack size 为8M。
猜想: 线程只能分配不大于 1/4 大小的 stack size 给 局部变量, 这应该是操作系统的规定。(没在网络上面找到相关的文档说明)
那我们现在开始验证我们的猜想:
通过 ulimit -s命令修改默认的栈大小, 下面程序分配5M的局部变量, 也就是说线程栈的大小要 > 20M(5M*4)
- //test1.cpp
- #include <iostream>
- #include <string.h>
- #include <pthread.h>
- #include <stdio.h>
- using namespace std;
- void* func(void*a)
- {
- cout << "enter func" << endl;
- cout << "enter func" << endl;
- int b[1024*1024*5] = {0};
- }
- int main()
- {
- int a[1024*1024*5]={0};
- pthread_t pthread ;
- pthread_create(&pthread, NULL, func, NULL);
- cout << "This is a test" << endl;
- //pthread_join(pthread, NULL);
- return 0;
- }
ulimit -s 21504 (也就是21M) , 把默认的stack size设置为21M
sundh@linux:~$ ulimit -s 21504
sundh@linux:~$ g++ -g -o test2 test2.cpp -lpthread
sundh@linux:~$ ./test2
This is a testenter func
enter func
可以成功运行, 验证了我们的猜想。
ulimit -s 修改 stack size, 也可以通过 pthread_attr_setstacksize() 来修改。 使用ulimit的一个后果就是它会影响到同一环境(同一shell或者终端)下后续启动的所有程序,如果修改成启动时设置的话就会影响到整个系统。 所以大部分情况下还是使用pthread_attr_setstacksize()
代码如下:
- #include <pthread.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <ctype.h>
- #define handle_error_en(en, msg) \
- do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
- #define handle_error(msg) \
- do { perror(msg); exit(EXIT_FAILURE); } while (0)
- struct thread_info { /* Used as argument to thread_start() */
- pthread_t thread_id; /* ID returned by pthread_create() */
- int thread_num; /* Application-defined thread # */
- char *argv_string; /* From command-line argument */
- };
- /* Thread start function: display address near top of our stack,
- and return upper-cased copy of argv_string */
- static void *
- thread_start(void *arg)
- {
- struct thread_info *tinfo = arg;
- char *uargv, *p;
- <span style="color:#FF0000;">int a[1024*1024*5] = {0};</span>
- printf("Thread %d: top of stack near %p; argv_string=%s\n",
- tinfo->thread_num, &p, tinfo->argv_string);
- uargv = strdup(tinfo->argv_string);
- if (uargv == NULL)
- handle_error("strdup");
- for (p = uargv; *p != '\0'; p++)
- *p = toupper(*p);
- return uargv;
- }
- int
- main(int argc, char *argv[])
- {
- int s, tnum, opt, num_threads;
- struct thread_info *tinfo;
- pthread_attr_t attr;
- int stack_size;
- void *res;
- /* The "-s" option specifies a stack size for our threads */
- stack_size = -1;
- while ((opt = getopt(argc, argv, "s:")) != -1) {
- switch (opt) {
- case 's':
- stack_size = strtoul(optarg, NULL, 0);
- break;
- default:
- fprintf(stderr, "Usage: %s [-s stack-size] arg...\n",
- argv[0]);
- exit(EXIT_FAILURE);
- }
- }
- num_threads = argc - optind;
- /* Initialize thread creation attributes */
- s = pthread_attr_init(&attr);
- if (s != 0)
- handle_error_en(s, "pthread_attr_init");
- if (stack_size > 0) {
- s = <span style="color:#FF0000;">pthread_attr_setstacksize</span>(&attr, stack_size);
- if (s != 0)
- handle_error_en(s, "pthread_attr_setstacksize");
- }
- /* Allocate memory for pthread_create() arguments */
- tinfo = calloc(num_threads, sizeof(struct thread_info));
- if (tinfo == NULL)
- handle_error("calloc");
- /* Create one thread for each command-line argument */
- for (tnum = 0; tnum < num_threads; tnum++) {
- tinfo[tnum].thread_num = tnum + 1;
- tinfo[tnum].argv_string = argv[optind + tnum];
- /* The pthread_create() call stores the thread ID into
- corresponding element of tinfo[] */
- s = pthread_create(&tinfo[tnum].thread_id, &attr,
- &thread_start, &tinfo[tnum]);
- if (s != 0)
- handle_error_en(s, "pthread_create");
- }
- /* Destroy the thread attributes object, since it is no
- longer needed */
- s = pthread_attr_destroy(&attr);
- if (s != 0)
- handle_error_en(s, "pthread_attr_destroy");
- /* Now join with each thread, and display its returned value */
- for (tnum = 0; tnum < num_threads; tnum++) {
- s = pthread_join(tinfo[tnum].thread_id, &res);
- if (s != 0)
- handle_error_en(s, "pthread_join");
- printf("Joined with thread %d; returned value was %s\n",
- tinfo[tnum].thread_num, (char *) res);
- free(res); /* Free memory allocated by thread */
- }
- free(tinfo);
- exit(EXIT_SUCCESS);
- }
$ ./a.out -s 0x1600000 hola salut servus (0x1500000 == 20M,0x1600000==21M
)
Thread 1: top of stack near 0xb7d723b8; argv_string=hola
Thread 2: top of stack near 0xb7c713b8; argv_string=salut
Thread 3: top of stack near 0xb7b703b8; argv_string=servus
Joined with thread 1; returned value was HOLA
Joined with thread 2; returned value was SALUT
Joined with thread 3; returned value was SERVUS
程序可以正常运行。 这里需要注意的一点是,主线程还是使用系统默认的stack size,也即8M, 不能在main()
里面声明超过2M的局部变量,创建子线程的时候调用了pthread_attr_setstacksize(), 修改stack size为21M,然后就能在子线程中声明5M的局部变量了。
thread线程栈size及局部变量最大可分配size【转】的更多相关文章
- C# 类型、对象、线程栈和托管堆在运行时的关系
我们将讨论类型.对象.线程栈和托管堆在运行时的相互关系,假定有以下两个类定义: internal class Employee { public int GetYearsEmplo ...
- 关于Linux x64 Oracle JDK7u60 64-bit HotSpot VM 线程栈默认大小问题的整理
JVM线程的栈默认大小,oracle官网有简单描述: In Java SE 6, the default on Sparc is 512k in the 32-bit VM, and 1024k in ...
- WINDOWS操作系统中可以允许最大的线程数(线程栈预留1M空间)(56篇Windows博客值得一看)
WINDOWS操作系统中可以允许最大的线程数 默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来 ...
- 关于java的volatile关键字与线程栈的内容以及单例的DCL
用volatile修饰的变量,线程在每次使用变量的时候,都会读取变量修改后的最新的值.volatile很容易被误用,用来进行原子性操作. package com.guangshan.test; pub ...
- Linux虚拟地址空间布局以及进程栈和线程栈总结【转】
转自:http://www.cnblogs.com/xzzzh/p/6596982.html 原文链接:http://blog.csdn.net/freeelinux/article/details/ ...
- Linux虚拟地址空间布局以及进程栈和线程栈总结
原文链接:http://blog.csdn.net/freeelinux/article/details/53782986[侵删] 本文转自多个博客,以及最后有我的总结.我没有单独从头到尾写一个总结的 ...
- 【摘】Linux虚拟地址空间布局以及进程栈和线程栈总结
在CSDN上看到的一篇文章,讲的还是满好的. 原文地址:Linux虚拟地址空间布局以及进程栈和线程栈总结 一:Linux虚拟地址空间布局 (转自:Linux虚拟地址空间布局) 在多任务操作系统中,每个 ...
- JAVA笔记 之 Thread线程
线程是一个程序的多个执行路径,执行调度的单位,依托于进程存在. 线程不仅可以共享进程的内存,而且还拥有一个属于自己的内存空间,这段内存空间也叫做线程栈,是在建立线程时由系统分配的,主要用来保存线程内部 ...
- 第16章 Windows线程栈
16.1 线程栈及工作原理 (1)线程栈简介 ①系统在创建线程时,会为线程预订一块地址空间(即每个线程私有的栈空间),并调拨一些物理存储器.默认情况下,预订1MB的地址空间并调拨两个页面的存储器. ② ...
随机推荐
- Spring MVC实践
MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...
- asp.net MVC4在Action间跳转 RedirectToAction 传值参数问题
return RedirectToAction("Test", new { cw = cw, firstdirectoryid = firstdirectoryid }); 上式中 ...
- RT-thread内核之线程调度算法
一个操作系统如果只是具备了高优先级任务能够“立即”获得处理器并得到执行的特点,那么它仍然不算是实时操作系统.因为这个查找最高优先级线程的过程决定了调度时间是否具有确定性,例如一个包含n个就绪任务的系统 ...
- 【bzoj1616】[Usaco2008 Mar]Cow Travelling游荡的奶牛 bfs
题目描述 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John在某个时刻看见 ...
- Qt——设计颜色编辑选取对话框
Qt中已经有一些封装好的对话框,比如QMessageBox.QColorDialog等,使用起来快捷方便,但缺点是我们无法为它们自定义样式,所以可能难以“融入”我们的项目.既然如此,那就自己做一个把. ...
- prototype的本质
在<关于思维方式的思绪>那篇文章里提到了, 原型的本质就是一种委托关系. 即我这里没有,就到我的原型里去看看,一旦找到就当成我的用. 本文详细说一下这个事情. 比如某女买东西,钱都是她老公 ...
- linux系统过一两分钟就断开的时间更改
vi /etc/ssh/sshd_config LoginGraceTime 参考man sshd_config LoginGraceTime The server disconnects after ...
- zoj 1298 Domino Effect (最短路径)
Domino Effect Time Limit: 2 Seconds Memory Limit: 65536 KB Did you know that you can use domino ...
- thread-wait/sleep
对于sleep()方法,我们首先要知道该方法是属于Thread类中的.而wait()方法,则是属于Object类中的. sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监 ...
- [CEOI2017]Mousetrap
P4654 [CEOI2017]Mousetrap 博弈论既视感 身临其境感受耗子和管理的心理历程. 以陷阱为根考虑.就要把耗子赶到根部. 首先一定有解. 作为耗子,为了拖延时间,必然会找到一个子树往 ...