LINUX最大线程数及最大进程数

root@server conf]# ulimit -a 
 
 

查看最大线程数:

cat /proc/sys/kernel/threads-max

 

ulimit
User limits - limit the use of system-wide resources.
Syntax
      ulimit [-acdfHlmnpsStuv] [limit]
Options
   -S   Change and report the soft limit associated with a resource. 
   -H   Change and report the hard limit associated with a resource. 
   -a   All current limits are reported. 
   -c   The maximum size of core files created. 
   -d   The maximum size of a process's data segment. 
   -f   The maximum size of files created by the shell(default option) 
   -l   The maximum size that may be locked into memory. 
   -m   The maximum resident set size. 
   -n   The maximum number of open file descriptors. 
   -p   The pipe buffer size. 
   -s   The maximum stack size. 
   -t   The maximum amount of cpu time in seconds. 
   -u   The maximum number of processes available to a single user. 
   -v   The maximum amount of virtual memory available to the process. 
ulimit provides control over the resources available to the shell and to processes started by it, on systems that allow such control.
If limit is given, it is the new value of the specified resource. Otherwise, the current value of the soft limit for the specified resource is printed, unless the `-H' option is supplied.
When setting new limits, if neither `-H' nor `-S' is supplied, both the hard and soft limits are set.
Values are in 1024-byte increments, except for `-t', which is in seconds, `-p', which is in units of 512-byte blocks, and `-n' and `-u', which are unscaled values.
The return status is zero unless an invalid option is supplied, a non-numeric argument other than unlimited is supplied as a limit, or an error occurs while setting a new limit.
ulimit is a bash built in command.

 

 

Ulimit命令
设置限制     可以把命令加到profile文件里,也可以在/etc/security/limits.conf文件中定义
限制。
命令参数
-a      显示所有限制
-c      core文件大小的上限
-d      进程数据段大小的上限
-f      shell所能创建的文件大小的上限
-m     驻留内存大小的上限
-s      堆栈大小的上限
-t      每秒可占用的CPU时间上限
-p     管道大小
-n     打开文件数的上限
-u     进程数的上限
-v     虚拟内存的上限
除可用Ulimit命令设置外,也可以在/etc/security/limits.conf文件中定义限制。
domino    type    item    value
domino是以符号@开头的用户名或组名,*表示所有用户,type设置为hard or soft。item指
定想限制的资源。如cpu,core nproc or maxlogins
。value是相应的限制值。

 

linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_THREADS_MAX
这个限制可以在 /usr/include/bits/local_lim.h 中查看
对 linuxthreads 这个值一般是 1024,对于 nptl 则没有硬性的限制,仅仅受限于系统的资源
这个系统的资源主要就是线程的 stack 所占用的内存,用 ulimit -s 可以查看默认的线程栈大小,一般情况下,这个值是 8M
可以写一段简单的代码验证最多可以创建多少个线程
               int main()
                {
                        int i = 0;
                        pthread_t thread;
                        while (1) {
                            if (pthread_create(&thread, NULL, foo, NULL) != 0)
                                return;
                        i ++;printf("i = %d\n", i);
                }
试验显示,在 linuxthreads 上最多可以创建 381 个线程,之后就会返回 EAGAIN
在 nptl 上最多可以创建 382 个线程,之后就会返回 ENOMEM
这个值和理论完全相符,因为 32 位 linux 下的进程用户空间是 3G 的大小,也就是 3072M,用 3072M 除以 8M 得 384,但是实际上代码段和数据段等还要占用一些空间,这个值应该向下取整到 383,再减去主线程,得到 382。
那为什么 linuxthreads 上还要少一个线程呢?这可太对了,因为 linuxthreads 还需要一个管理线程
为了突破内存的限制,可以有两种方法
1) 用 ulimit -s 1024 减小默认的栈大小
2) 调用 pthread_create 的时候用 pthread_attr_getstacksize 设置一个较小的栈大小
要注意的是,即使这样的也无法突破 1024 个线程的硬限制,除非重新编译 C 库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值