首页 何问起 前端特效 htbtn-4 闪电 使用方法

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

查看最大线程数:

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是相应的限制值。

系统限制默认值

[root@flyinweb ~]# 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) 32764 
max locked memory (kbytes, -l) 32 
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) 10240 
cpu time (seconds, -t) unlimited 
max user processes (-u) 32764 
virtual memory (kbytes, -v) unlimited 
file locks (-x) unlimited
[root@flyinweb ~]# lsb_release -a 
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch 
Distributor ID: CentOS 
Description: CentOS release 5.2 (Final) 
Release: 5.2 
Codename: Final

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 还需要一个管理线程
http://www.cnblogs.com/roucheng/
为了突破内存的限制,可以有两种方法

1) 用 ulimit -s 1024 减小默认的栈大小
2) 调用 pthread_create 的时候用 pthread_attr_getstacksize 设置一个较小的栈大小

要注意的是,即使这样的也无法突破 1024 个线程的硬限制,除非重新编译 C 库

相关内容:

一、2.4内核与2.6内核的主要区别 
在 2.4内核的典型系统上(AS3/RH9),线程是用轻量进程实现的,每个线程要占用一个进程ID,在服务器程序上,如果遇到高点击率访问,会造成进程表 溢出,系统为了维护溢出的进程表,会有间歇的暂停服务现象,而2.6内核就不会发生由于大量线程的创建和销毁导致进程表溢出的问题
二、线程结束必须释放线程堆栈 
就 是说,线程函数必须调用pthread_exit()结束,否则直到主进程函数退出才释放,特别是2.6内核环境,线程创建速度飞快,一不小心立刻内存被 吃光,这一点反倒是2.4内核环境好,因为2.4内核创建的是进程,而且线程创建速度比2.6内核慢几个数量级。特别提醒,在64位CPU,2.6内核创 建线程的速度更加疯狂,要是太快的话,加上usleep ()暂停一点点时间比较好
三、不要编需要锁的线程应用 
只 有那些不需要互斥量的程序才能最大限度的利用线程编程带来的好处,否则只会更慢,2.6内核是抢占式内核,线程间共享冲突发生的几率远比2.4内核环境 高,尤其要注意线程安全,否则就算是单CPU也会发生莫名其妙的内存不同步(CPU的高速缓存和主存内容不一致),Intel的新CPU为了性能使用 NUMA架构,在线程编程中一定要注意扬长避短。
四、单进程服务器最大并发线程数与内存 
很有趣,在默认的ulimit参数下,不修改内核头文件 
AS3 512M内存最多1000并发持续连接 
CentOS4.3 512M内存最多300并发持续连接 
似 乎是CentOS不如AS3,这里主要原因是ulimit的配置造成,两个系统默认的配置差距很大,要想单进程维持更多线程接收并发连接,就要尽量缩小 ulimit -s的参数,插更多的内存条,单进程服务器上2000并发一点都不难,POSIX默认的限制是每进程64线程,但NTPL并非纯正POSIX,不必理会这 个限制,2.6内核下真正的限制是内存条的插槽数目(也许还有买内存的钱数)
最 近几天的编程中,注意到在32位x86平台上2.6内核单进程创建最大线程数=VIRT上限/stack,与总内存数关系不大,32位x86系统默认的 VIRT上限是3G(内存分配的3G+1G方式),默认 stack大小是10240K,因此单进程创建线程默认上限也就300(3072M / 10240K),用ulimit -s 修改stack到1024K则使上限升到大约3050。我手头没有64位系统,不知道2.6内核在64位上单进程创建线程上限(实际上是本人懒得在同事的 机器上装fc4_x86_64)。
前些天买了一套廉价的64位x86系统(64位赛杨+杂牌915主板),安装了CentOS4.3的x86_64版本,跑了一遍下面的小程序,得到的结果是:在ulimit -s 4096的情况下,单进程最大线程数在16000多一点,用top看 
VIRT 的上限是64G,也就是36位, cat /proc/cpuinfo的结果是:address sizes : 36 bits physical, 48 bits virtual, 和我想象的标准64位系统不同, 我一直以为64位系统的内存空间也是64位的 
附注1: 
单 位里某BSD FANS用AMD64笔记本跑小程序测试线程创建速度(线程创建后立即phread_detach()然后紧跟着pthread_exit(),共计 100万个线程),同样源码OpenBSD竟然比FreeBSD快了3倍,什么时候OpenBSD也变得疯狂起来了?
posted @ 2013-12-11 23:13  roucheng  阅读(12650)  评论(0编辑  收藏  举报