天天看點

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 庫

繼續閱讀