查看最大線程數:
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 還需要一個管理線程
為了突破內存的限制,可以有兩種方法
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也變得瘋狂起來了?
附注2:
測試單進程創建線程上限C源碼(test.c):
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> void * thread_null(void); int main(int argc, char*argv[]) { unsigned int i; int rc; pthread_t pool_id[65536]; //線程ID sleep(1); //創建線程 for(i = 0; i < 65536; i++) { rc = pthread_create(pool_id + i, 0, (void*)thread_null, NULL); if (0 != rc) { fprintf(stderr, "pthread_create() failure\r\nMax pthread num is %d\r\n", i); exit(-1); } } fprintf(stdout, "Max pthread num is 65536\r\nYour system is power_full\r\n"); exit(0); } void * thread_null(void) { pthread_detach(pthread_self()); sleep(60); pthread_exit(NULL); }
編譯:
[root@localhost test]# gcc test.c -o test -lpthread
[root@localhost test]# ulimit -s 10240
[root@localhost test]# ./test
pthread_create() failure
Max pthread num is 305
[root@localhost test]# ulimit -s 1024
[root@localhost test]# ./test
pthread_create() failure
Max pthread num is 3054
以上結果在 CentOS release 5.2 (32Bit)系統上測試得到