Linux中每个进程的最大线程数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/344203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:46:44  来源:igfitidea点击:

Maximum number of threads per process in Linux?

linuxmultithreading

提问by

What is the maximum number of threads that can be created by a process under Linux?

Linux下一个进程最多可以创建多少个线程?

How (if possible) can this value be modified?

如何(如果可能)修改此值?

采纳答案by Robert Gamble

Linux doesn't have a separate threads per process limit, just a limit on the total number of processes on the system (threads are essentially just processes with a shared address space on Linux) which you can view like this:

Linux 没有为每个进程设置单独的线程限制,只是对系统上进程总数的限制(线程本质上只是在 Linux 上具有共享地址空间的进程),您可以这样查看:

cat /proc/sys/kernel/threads-max

The default is the number of memory pages/4. You can increase this like:

默认为内存页数/4。你可以增加这个:

echo 100000 > /proc/sys/kernel/threads-max

There is also a limit on the number of processes (and hence threads) that a single user may create, see ulimit/getrlimitfor details regarding these limits.

单个用户可以创建的进程(以及线程)数量也有限制,ulimit/getrlimit有关这些限制的详细信息,请参阅。

回答by Vincent Van Den Berghe

To retrieve it:

要检索它:

cat /proc/sys/kernel/threads-max

To set it:

要设置它:

echo 123456789 > /proc/sys/kernel/threads-max

123456789 = # of threads

123456789 = 线程数

回答by jalf

In practical terms, the limit is usually determined by stack space. If each thread gets a 1MB stack (I can't remember if that is the default on Linux), then you a 32-bit system will run out of address space after 3000 threads (assuming that the last gb is reserved to the kernel).

实际上,限制通常由堆栈空间决定。如果每个线程获得 1MB 堆栈(我不记得这是不是 Linux 上的默认值),那么 32 位系统将在 3000 个线程后耗尽地址空间(假设最后一个 gb 保留给内核) .

However, you'll most likely experience terrible performance if you use more than a few dozen threads. Sooner or later, you get too much context-switching overhead, too much overhead in the scheduler, and so on. (Creating a large number of threads does little more than eat a lot of memory. But a lot of threads with actual workto do is going to slow you down as they're fighting for the available CPU time)

但是,如果您使用超过几十个线程,则很可能会遇到糟糕的性能。迟早,你会得到太多的上下文切换开销,调度程序中的太多开销,等等。(创建大量线程只会消耗大量内存。但是,大量有实际工作要做的线程会减慢您的速度,因为它们正在争夺可用的 CPU 时间)

What are you doing where this limit is even relevant?

在此限制甚至相关的情况下,您在做什么?

回答by twk

It probably shouldn't matter. You are going to get much better performance designing your algorithm to use a fixed number of threads (eg, 4 or 8 if you have 4 or 8 processors). You can do this with work queues, asynchronous IO, or something like libevent.

应该没什么关系吧。将算法设计为使用固定数量的线程(例如,如果您有 4 或 8 个处理器,则为 4 或 8 个),您将获得更好的性能。您可以使用工作队列、异步 IO 或 libevent 之类的东西来做到这一点。

回答by wefeqfw

Use nbionon-blocking i/o library or whatever, if you need more threads for doing I/O calls that block

使用nbio非阻塞 I/O 库或其他任何东西,如果您需要更多线程来执行该阻塞的 I/O 调用

回答by codersofthedark

This is WRONG to say that LINUX doesn't have a separate threads per process limit.

说 LINUX 没有每个进程限制的单独线程是错误的。

Linux implements max number of threads per process indirectly!!

Linux 间接实现了每个进程的最大线程数!!

number of threads = total virtual memory / (stack size*1024*1024)

Thus, the number of threads per process can be increased by increasing total virtual memory or by decreasing stack size. But, decreasing stack size too much can lead to code failure due to stack overflow while max virtual memory is equals to the swap memory.

因此,可以通过增加总虚拟内存或减少堆栈大小来增加每个进程的线程数。但是,当最大虚拟内存等于交换内存时,过多地减少堆栈大小会由于堆栈溢出而导致代码失败。

Check you machine:

检查你的机器:

Total Virtual Memory: ulimit -v(default is unlimited, thus you need to increase swap memory to increase this)

总虚拟内存:(ulimit -v默认是无限的,因此你需要增加交换内存来增加这个)

Total Stack Size: ulimit -s(default is 8Mb)

总堆栈大小:(ulimit -s默认为 8Mb)

Command to increase these values:

增加这些值的命令:

ulimit -s newvalue

ulimit -v newvalue

*Replace new value with the value you want to put as limit.

*用您想作为限制的值替换新值。

References:

参考:

http://dustycodes.wordpress.com/2012/02/09/increasing-number-of-threads-per-process/

http://dustycodes.wordpress.com/2012/02/09/increasing-number-of-threads-per-process/

回答by resultsway

Depends on your system, just write a sample program [ by creating processes in a loop ] and check using ps axo pid,ppid,rss,vsz,nlwp,cmd. When it can no more create threads check nlwp count [ nlwp is the number threads ] voila you got your fool proof answer instead of going thru books

取决于您的系统,只需编写一个示例程序 [通过在循环中创建进程] 并使用 ps axo pid,ppid,rss,vsz,nlwp,cmd 检查。当它不能再创建线程时,请检查 nlwp 计数 [nlwp 是线程数] 瞧,您得到了万无一失的答案,而不是翻阅书籍

回答by c4f4t0r

@dragosrsupercool

@dragosrsupercool

Linux doesn't use the virtual memory to calculate the maximum of thread, but the physical ram installed on the system

Linux 不使用虚拟内存来计算线程的最大值,而是使用系统上安装的物理内存

 max_threads = totalram_pages / (8 * 8192 / 4096);

http://kavassalis.com/2011/03/linux-and-the-maximum-number-of-processes-threads/

http://kavassalis.com/2011/03/linux-and-the-maximum-number-of-processes-threads/

kernel/fork.c

内核/fork.c

/* The default maximum number of threads is set to a safe
 * value: the thread structures can take up at most half
 * of memory.
 */
max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);

So thread max is different between every system, because the ram installed can be from different sizes, I know Linux doesn't need to increase the virtual memory, because on 32 bit we got 3 GB for user space and 1 GB for the kernel, on 64 bit we got 128 TB of virtual memory, that happen on Solaris, if you want increase the virtual memory you need to add swap space.

所以每个系统的线程最大值是不同的,因为安装的内存可以有不同的大小,我知道 Linux 不需要增加虚拟内存,因为在 32 位上我们有 3 GB 的用户空间和 1 GB 的内核,在 64 位上,我们有 128 TB 的虚拟内存,这发生在 Solaris 上,如果您想增加虚拟内存,则需要添加交换空间。

回答by Albert Kong

Thread count limit:

线程数限制:

$ cat /proc/sys/kernel/threads-max 

How it is calculated:

计算方法:

max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);

and: x86_64 page size (PAGE_SIZE) is 4K; Like all other architectures, x86_64 has a kernel stack for every active thread. These thread stacks are THREAD_SIZE (2*PAGE_SIZE) big;

并且:x86_64 页面大小(PAGE_SIZE)为 4K;像所有其他架构一样,x86_64 为每个活动线程都有一个内核堆栈。这些线程栈是 THREAD_SIZE (2*PAGE_SIZE) 大;

for mempages :

对于 mempage:

cat /proc/zoneinfo | grep spanned | awk '{totalpages=totalpages+} END {print totalpages}';

so actually the number is not related with limitation of thread memory stack size (ulimit -s).

所以实际上这个数字与线程内存堆栈大小(ulimit -s)的限制无关。

P.S: thread memory stack limitation is 10M in my rhel VM, and for 1.5G memory, this VM can only afford 150 threads?

PS:我的rhel VM中线程内存栈限制是10M,1.5G内存,这个VM只能承受150个线程?

回答by Vladimir Kunschikov

proper 100k threads on linux:

linux 上正确的 100k 线程:

ulimit -s  256
ulimit -i  120000
echo 120000 > /proc/sys/kernel/threads-max
echo 600000 > /proc/sys/vm/max_map_count
echo 200000 > /proc/sys/kernel/pid_max 

 ./100k-pthread-create-app

2018 update from @Thomas, on systemd systems:

@Thomas 的 2018 年更新,在 systemd 系统上:

/etc/systemd/logind.conf: UserTasksMax=100000