免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1806 | 回复: 3
打印 上一主题 下一主题

[Linux] 单进程能开多少线程测试,以及疑惑请教。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-04-26 15:33 |只看该作者 |倒序浏览
测试系统是CentOS-7-x86_64-DVD-1511
代码如下:
  1.   1 #include <stdio.h>
  2.   2 #include <pthread.h>
  3.   3 #include <errno.h>
  4.   4 extern int  errno;
  5.   5 void* foo(void* x)
  6.   6 { sleep(3600); }
  7.   7 int main()
  8.   8 {
  9.   9      int i = 0,err;
  10. 10      pthread_t thread;
  11. 11      pthread_attr_t attr;
  12. 12      pthread_attr_init(&attr);
  13. 13      pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
  14. 14     //pthread_attr_setstacksize(&attr,50*1024);
  15. 15      while (++i) {
  16. 16         if ((err=pthread_create(&thread, &attr, foo, NULL)) != 0) {
  17. 17            printf("threadnum=%d ,err=%d, errno=%d\n",i-1,err,errno);
  18. 18            return 1;
  19. 19         }
  20. 20       }
  21. 21 }
复制代码
当前环境为:
$ 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) 3797
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) 3797
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

程序运行结果为:
$ maxthread
threadnum=3640 ,err=11, errno=11

缩小线程栈空间
ulimit -s 1000
再次运行程序:
maxthread
threadnum=3640 ,err=11, errno=11
结果依然相同。

通过代码方式修改栈空间:
把第14行注释取消,设置50k的空间,重编译后,再跑
$ maxthread
threadnum=3640 ,err=11, errno=11
结果还是相同。

我比较奇怪,3640是最大的单进程线程数吗?不管线程栈空间怎么改变,线程数量总是这么多个。
请教了!

论坛徽章:
0
2 [报告]
发表于 2016-04-27 11:06 |只看该作者
限制这个的地方有多处,纠结这个意义不是很大。

论坛徽章:
0
3 [报告]
发表于 2016-04-27 20:10 |只看该作者
IanFang 发表于 2016-04-27 11:06
限制这个的地方有多处,纠结这个意义不是很大。

能具体说下是哪些地方吗?

论坛徽章:
0
4 [报告]
发表于 2016-05-05 16:27 |只看该作者
有很多地方制约线程数,比如:

/proc/sys/kernel/threads-max
/proc/sys/kernel/pid_max
/proc/sys/vm/max_map_count
还有 ulimit 打印出来的那个 max user processes

查一下这几个值,哪个可能成为瓶颈,修改它试一试。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP